Skip to content

Commit 269690e

Browse files
committed
[level 2] Title: 마법의 엘리베이터, Time: 0.02 ms, Memory: 84 MB -BaekjoonHub
1 parent 42ca542 commit 269690e

File tree

2 files changed

+12
-40
lines changed

2 files changed

+12
-40
lines changed

프로그래머스/2/148653. 마법의 엘리베이터/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### 성능 요약
66

7-
메모리: 75 MB, 시간: 0.04 ms
7+
메모리: 84 MB, 시간: 0.02 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2025년 08월 15일 10:53:53
19+
2025년 08월 15일 11:03:38
2020

2121
### 문제 설명
2222

프로그래머스/2/148653. 마법의 엘리베이터/마법의 엘리베이터.java

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,17 @@
11
class Solution {
2-
public int solution(int storey) {
3-
String str = String.valueOf(storey);
4-
int len = str.length();
2+
public int solution(int storey) {
53
int cnt = 0;
4+
while (storey > 0) {
5+
int digit = storey % 10; // 현재 자리
6+
int nextDigit = (storey % 100) / 10; // 다음 자리
67

7-
int[] storeys = new int[str.length()];
8-
for (int i = 0; i < len; i++) {
9-
storeys[i] = str.charAt(i) - '0';
10-
}
11-
12-
13-
for (int i = len - 1; i > 0; i--) {
14-
int num = storeys[i];
15-
16-
if (num < 5) { // 내림
17-
cnt += num;
18-
} else if (num > 5) { // 올림
19-
if(num == 10){
20-
storeys[i-1]++;
21-
continue;
22-
}
23-
cnt += 10 - num;
24-
storeys[i - 1]++; // 올림 처리
25-
} else {
26-
if (storeys[i - 1] >= 5) { // 다음 자릿수가 5 이상이면 올림
27-
storeys[i - 1]++; // 올림 처리
28-
}
29-
// 4이하면 내림 처리
30-
cnt += 5;
31-
}
32-
}
33-
34-
int num = storeys[0]; // 맨 앞자리
35-
if (num <= 5) { // 내림
36-
cnt += num;
37-
} else { // 올림
38-
if(num == 10){
39-
cnt ++ ;
40-
}else{
41-
cnt += 10 - num + 1;
8+
if (digit > 5 || digit == 5 && nextDigit >= 5) { // 올림
9+
cnt += 10 - digit;
10+
storey += 10;
11+
} else { // 내림
12+
cnt += digit;
4213
}
14+
storey /= 10;
4315
}
4416

4517
return cnt;

0 commit comments

Comments
 (0)