Minimum Rotations to Dial a Number I - Solution & Explanation
Problem Statement
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$.
Code
Python
Java
C++
Go
TypeScript
Detailed 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 |
Video Solutions (8)
Leetcode 4071 | Minimum Rotations to Dial a Number II | Leetcode weekly contest 522
Frequently Asked Questions
Is Minimum Rotations to Dial a Number I easy or hard?
Minimum Rotations to Dial a Number I Python solution?
How to solve Minimum Rotations to Dial a Number I in O(n)?
What is the best approach for Minimum Rotations to Dial a Number I?
What data structure is used in Minimum Rotations to Dial a Number I?
What is the time complexity of Minimum Rotations to Dial a Number I?
Is Minimum Rotations to Dial a Number I asked at Google, Amazon, or Meta?
Ready to solve this problem?
Practice Minimum Rotations to Dial a Number I with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor