Rotated Digits

An integer x is good if rotating each of its digits by 180 degrees gives a valid number that is different from x. 0, 1 and 8 rotate to themselves, 2 and 5 rotate to each other, 6 and 9 rotate to each other, and 3, 4 and 7 cannot be rotated. Given n, return how many integers in [1, n] are good.

30:00

Rotated Digits
medium
Free written solution
Topics
Companies

An integer x is good if rotating each of its digits by 180 degrees gives a valid number that is different from x. 0, 1 and 8 rotate to themselves, 2 and 5 rotate to each other, 6 and 9 rotate to each other, and 3, 4 and 7 cannot be rotated. Given n, return how many integers in [1, n] are good.

Example 1:
Input: {"n":10}
Output: 4
Constraints:
  • 1≤n≤1041 \leq n \leq 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":10}

Class each digit: 0, 1, 8 rotate to themselves; 2, 5, 6, 9 rotate to a different digit; 3, 4, 7 break the number.

kind[d]: what each digit becomes after a 180 degree turn

0

0

1

1

2

5

3

x

4

x

5

2

6

9

7

x

8

8

9

6

changes (2)

same (1)

invalid (0)

dp[x] laid out as row x // 10, column x % 10

0

1

2

3

4

5

6

7

8

9

0

0
1
2
3
4
5
6
7
8
9

1

10

x = 0 / 10

good count = 0

Variables
VariableValue
n10
x0
count0
DepthFunction Call
Stack empty
0/13