File tree Expand file tree Collapse file tree 2 files changed +12
-40
lines changed
프로그래머스/2/148653. 마법의 엘리베이터 Expand file tree Collapse file tree 2 files changed +12
-40
lines changed Original file line number Diff line number Diff line change 44
55### 성능 요약
66
7- 메모리: 75 MB, 시간: 0.04 ms
7+ 메모리: 84 MB, 시간: 0.02 ms
88
99### 구분
1010
1616
1717### 제출 일자
1818
19- 2025년 08월 15일 10:53:53
19+ 2025년 08월 15일 11:03:38
2020
2121### 문제 설명
2222
Original file line number Diff line number Diff line change 11class 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 ;
You can’t perform that action at this time.
0 commit comments