Minimum Rotations to Dial a Number I solution and explanation
7 more videos on this problem
Leetcode 4071 | Minimum Rotations to Dial a Number II | Leetcode weekly contest 522CodeWithMeGuys · 14:18 · 590 views
LeetCode 4071 | Weekly Contest 522 | Minimum Rotations to Dial a Number II | Easy O(n) | C++CodeSprint · 14:57 · 279 views
Minimum Rotations to Dial a Number II | Leetcode 4071 | Weekly Contest 522 | Dry RunVijay Algorithms · 24:36 · 144 views
Minimum Rotations to Dial a Number I | Leetcode 4070 | Weekly Contest 522 | Dry RunVijay Algorithms · 16:16 · 138 views
LeetCode Weekly Contest #522 Q1. Minimum Rotations to Dial a Number IADevOpsEngineer · 2:32 · 109 views
4071. Minimum Rotations to Dial a Number II ~LeetCode Weekly 522 Greedy SolutionBEASTCODES · 18:56 · 98 views
LeetCode Weekly Contest 522 | 4070 & 4071 Explained in Java | First 2 ProblemsProof2Code · 21:08 · 85 views
LeetCode 4070 & 4071 | Minimum Rotations to Dial a Number I & II | C++Logic Mode · 42:14 · 79 views
The problem
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'
Approach overview
Problem Overview: You have a circular dial with digits 0 through 9. The pointer starts at digit 0, and you must dial each digit of the target number in order. Each rotation moves the pointer one step clockwise or counterclockwise, and you want the minimum total rotations.
Approach 1: Step-by-Step Simulation (O(n * 10) time, O(1) space)
Track the current pointer position and move it one step at a time toward each target digit. For each digit, keep rotating in one direction until you reach the target. This works, but it does unnecessary work because the dial has only 10 positions and each digit can require up to 5 steps in one direction. Use it to confirm your understanding of the circular movement before optimizing.
Approach 2: Circular Distance Per Digit (O(n) time, O(1) space)
For each consecutive pair of positions, compute the absolute difference d = |current - target|. On a circle of 10 digits, you can reach the target either by going forward d steps or backward 10 - d steps. Take min(d, 10 - d) and add it to the total. Update the current position to the target and move on. Each digit is handled with a constant amount of arithmetic, so there is no inner loop. This is the approach you should write in an interview. Use the array traversal and simple arithmetic here, with no extra data structures needed.
Approach 3: Precomputed Distance Table (O(n) time, O(1) space)
You can build a 10 x 10 table of minimum circular distances once and look up each transition in constant time. This is functionally identical to Approach 2 and only helps if the distance logic is more complex than a single formula. For this problem, the direct formula is shorter and less error-prone.
Recommended for interviews: Interviewers expect the simulation idea first, because it shows you understand how the circular dial works. The circular distance formula min(d, 10 - d) is what they want to see as the final answer. Explaining why both directions must be considered is what separates a strong answer from a weak one. Mention the O(n) time and O(1) space bounds explicitly when you present the solution. Related reading: simulation and math.
Solution
Thinking
Each step only depends on the current pointer position and the next digit to dial, so we can process the string greedily from left to right.
If we move from digit
ato digitb, the clockwise and counterclockwise distances add up to10. Letd=|a-b|, then the minimum cost of this move ismin(d, 10-d).Starting from digit
0, we sum this minimum cost for every transition.
Let pre be the previous digit under the pointer and cur be the next target digit. Because the dial is circular, the shortest distance between them is
$
min\bigl(|cur-pre|, 10-|cur-pre|\bigr).
We iterate through the string, add this value to the answer, and update pre to cur.
The time complexity is O(n), and the space complexity is O(1). Here, 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 ansclass Solution {
public int minRotations(String s) {
int ans = 0;
int pre = 0;
for (int i = 0; i < s.length(); ++i) {
int cur = s.charAt(i) - '0';
int diff = Math.abs(cur - pre);
ans += Math.min(diff, 10 - diff);
pre = cur;
}
return ans;
}
}class Solution {
public:
int minRotations(string s) {
int ans = 0;
int pre = 0;
for (int i = 0; i < s.size(); ++i) {
int cur = s[i] - '0';
int diff = abs(cur - pre);
ans += min(diff, 10 - diff);
pre = cur;
}
return ans;
}
};function minRotations(s: string): number {
let ans = 0;
let pre = 0;
for (let i = 0; i < s.length; ++i) {
const cur = s.charCodeAt(i) - 48;
const diff = Math.abs(cur - pre);
ans += Math.min(diff, 10 - diff);
pre = cur;
}
return ans;
}func minRotations(s string) int {
ans := 0
pre := 0
for i := 0; i < len(s); i++ {
cur := int(s[i] - '0')
diff := abs(cur - pre)
ans += min(diff, 10-diff)
pre = cur
}
return ans
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Step-by-Step Simulation | O(n * 10) | O(1) | Useful for understanding the problem, not for submission |
| Circular Distance Per Digit | O(n) | O(1) | General case and interview answer |
| Precomputed Distance Table | O(n) | O(1) extra (100 entries) | When transition costs are irregular |
Frequently asked questions
Is Minimum Rotations to Dial a Number I easy or hard?
It is rated Easy with a 75.8% acceptance rate. The main challenge is recognizing that the dial wraps around, so you must consider both directions. Once you see the min(d, 10 - d) pattern, the implementation takes only a few lines.
Minimum Rotations to Dial a Number I Python solution?
In Python, convert each character to an integer, keep a variable for the current position starting at 0, and for each digit compute d = abs(target - current), then add min(d, 10 - d) to the total. Return the total after the loop. The code is short and runs in O(n) time with O(1) extra space.
How to solve Minimum Rotations to Dial a Number I in O(n)?
Start the pointer at digit 0 and iterate through the target digits in order. For each digit, compute the absolute difference from the current position, take the smaller of that value and 10 minus that value, add it to the total, and set the current position to the target digit.
What is the best approach for Minimum Rotations to Dial a Number I?
The best approach is to compute the circular distance between each consecutive pair of digits using min(d, 10 - d), where d is the absolute difference. Sum these values while walking through the number once. This runs in O(n) time and O(1) space.
What data structure is used in Minimum Rotations to Dial a Number I?
No special data structure is required. The solution uses a few integer variables to track the current digit position and the running total. Simple iteration over the input string or digit array is enough.
What is the time complexity of Minimum Rotations to Dial a Number I?
The optimal solution runs in O(n) time, where n is the number of digits to dial, because each digit requires only constant-time arithmetic. The space complexity is O(1) since you only store the current position and a running total.
Is Minimum Rotations to Dial a Number I asked at Google, Amazon, or Meta?
This problem is tagged as General on FleetCode and does not have a specific company tag in our data. Check recent interview reports on company-specific sites before assuming it appears at a particular firm. The underlying skill, modular circular distance, is useful across many interview problems.
