Skip to content

Commit 71ae1dc

Browse files
authored
Create 4070. Minimum Rotations to Dial a Number I.java
1 parent 5824441 commit 71ae1dc

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// O(n)
2+
3+
class Solution {
4+
public int minRotations(String s) {
5+
s = "0" + s;
6+
int rotation = 0;
7+
for (int i = 0; i<s.length()-1; i++) {
8+
int from = s.charAt(i) - '0';
9+
int to = s.charAt(i+1) - '0';
10+
11+
int right = Math.abs(to - from);
12+
int left = 10-Math.max(to, from) + Math.min(to, from);
13+
rotation += right<left? right: left;
14+
}
15+
return rotation;
16+
}
17+
}

0 commit comments

Comments
 (0)