Skip to content

Commit 3d771bb

Browse files
committed
4070. Minimum Rotations to Dial a Number I
1 parent 036597b commit 3d771bb

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
3+
1. 아이디어 : 다이얼 크기 10 -> 시계방향, 반시계방향 회전의 경우중 더 짧은것 선택
4+
5+
경우의 수 (a->b)
6+
1. |a-b|
7+
2. 9 - |a-b|
8+
9+
2. 시간복잡도 : O(s 문자열 길이)
10+
11+
3. 자료구조/알고리즘 : 그리디
12+
13+
*/
14+
15+
class Solution {
16+
public int minRotations(String s) {
17+
18+
int ans = 0;
19+
int pos = 0;
20+
21+
for(int i=0; i<s.length(); i++) {
22+
int num = s.charAt(i)-'0';
23+
24+
// 회전해서 갈 경우
25+
// 바로 갈 경우
26+
27+
int cal = Math.abs(num-pos);
28+
29+
ans+=Math.min(cal, 10-cal);
30+
31+
pos = num;
32+
}
33+
34+
return ans;
35+
}
36+
}

0 commit comments

Comments
 (0)