Minimum Rotations to Dial a Number I Problem
Description
LeetCode Problem 4070.
You are given a string s of length 10 consisting of digits.
The dial contains the digits 0 through 9 in order and is circular, so 0 and 9 are adjacent. The pointer initially points to 0.
To dial each digit of s in order, rotate the pointer until it points to that digit. Each rotation moves the pointer to an adjacent digit, and you may rotate in either direction. Dialing a digit that the pointer already points to requires no rotations.
Return the minimum total number of rotations needed to dial every digit of s.
Example 1:
Input: s = "0192837465"
Output: 25
Explanation:
Step From To Rotations
1 0 0 0
2 0 1 1
3 1 9 2
4 9 2 3
5 2 8 4
6 8 3 5
7 3 7 4
8 7 4 3
9 4 6 2
10 6 5 1
The total is 0 + 1 + 2 + 3 + 4 + 5 + 4 + 3 + 2 + 1 = 25, which is the minimum total number of rotations.
Example 2:
Input: s = "1200210200"
Output: 12
Explanation:
Step From To Rotations
1 0 1 1
2 1 2 1
3 2 0 2
4 0 0 0
5 0 2 2
6 2 1 1
7 1 0 1
8 0 2 2
9 2 0 2
10 0 0 0
The total is 1 + 1 + 2 + 0 + 2 + 1 + 1 + 2 + 2 + 0 = 12, which is the minimum total number of rotations.
Constraints:
s.length == 10sconsists only of digits'0'to'9'
Solution
Approach: Circular distance
Keep the digit the pointer is on (pre, initially 0) and the next digit to dial (cur). On a circular dial of 10 digits, the fewest rotations between them is the smaller of going one way, |cur - pre|, or the other way, 10 - |cur - pre|.
Walk through s, add that distance for every digit, and move pre to cur.
Complexity: O(n) time and O(1) extra space, where n is the length of s.
class Solution:
def minRotations(self, s: str) -> int:
ans = pre = 0
for cur in map(int, s):
diff = abs(cur - pre)
ans += min(diff, 10 - diff)
pre = cur
return ans