The trick here is to name the state correctly, then let the implementation follow. For 788. Rotated Digits, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are isGood, rotatedDigits.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. Each digit must be rotated - we cannot choose to leave it alone.
03
04A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number and become invalid.
05
06Now given a positive number N, how many numbers X from 1 to N are good?
07
08Example:
09Input: 10
10Output: 4
11Explanation:
12There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
13Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
14Note:
15
16N will be in range [1, 10000].
17**/
18
19//Runtime: 8 ms, faster than 57.61% of C++ online submissions for Rotated Digits.
20//Memory Usage: 8.4 MB, less than 70.37% of C++ online submissions for Rotated Digits.
21
22class Solution {
23public:
24 bool isGood(int n){
25 bool isDifferent = false;
26 while(n>0){
27 int last = n%10;
28
29 if(last==2 || last==5 || last==6 || last==9){
30 isDifferent = true;
31 }else if(last==0 || last==1 || last==8){
32 //pass
33 }else{
34 return false;
35 }
36 n/=10;
37 }
38 return isDifferent;
39 }
40 int rotatedDigits(int N) {
41 int ans = 0;
42
43 for(int n = 1; n <=N ; n++){
44 if(isGood(n)){
45 ans++;
46 }
47 }
48 return ans;
49 }
50};
Cost