Algorithm


Problem Name: 788. Rotated Digits

Problem Link:  https://leetcode.com/problems/rotated-digits/

An integer x is a good 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.

A number is valid if each digit remains a digit after rotation. For example:

  • 0, 1, and 8 rotate to themselves,
  • 2 and 5 rotate to each other (in this case they are rotated in a different direction, in other words, 2 or 5 gets mirrored),
  • 6 and 9 rotate to each other, and
  • the rest of the numbers do not rotate to any other number and become invalid.

Given an integer n, return the number of good integers in the range [1, n].

 

Example 1:

Input: n = 10
Output: 4
Explanation: There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.

Example 2:

Input: n = 1
Output: 0

Example 3:

Input: n = 2
Output: 1

 

Constraints:

  • 1 <= n <= 104

 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int rotatedDigits(int n) {
    int[] dp = new int[n + 1];
    int result = 0;
    for (int i = 0; i  < = Math.min(n, 9); i++) {
      if (i == 0 || i == 1 || i == 8) {
        dp[i] = 1;
      } else if (i == 2 || i == 5 || i == 6 || i == 9) {
        dp[i] = 2;
        result++;
      }
    }
    for (int i = 10; i  < = n; i++) {
      int factor = dp[i / 10];
      int remainder = dp[i % 10];
      if (factor == 1 && remainder == 1) {
        dp[i] = 1;
      } else if (factor >= 1 && remainder >= 1) {
        dp[i] = 2;
        result++;
      }
    }
    return result;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 10

Output

x
+
cmd
4

#2 Code Example with Javascript Programming

Code - Javascript Programming


const rotatedDigits = function(n) {
   const dp = new Array(n + 1).fill(0);
    let count = 0;
    for(let i = 0; i  < = n; i++){
      if(i < 10){
        if(i == 0 || i == 1 || i == 8) dp[i] = 1;
        else if(i == 2 || i == 5 || i == 6 || i == 9){
          dp[i] = 2;
          count++;
        }
      } else {
        let a = dp[~~(i / 10)], b = dp[i % 10];
        if(a == 1 && b == 1> dp[i] = 1;
        else if(a >= 1 && b >= 1){
          dp[i] = 2;
          count++;
        }
      }
    }
    return count; 
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 10

Output

x
+
cmd
4

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def rotatedDigits(self, N):
        """
        :type N: int
        :rtype: int
        """
        res = 0
        for i in range(1, N + 1):
            i = str(i)
            tmp = []
            check = True
            for char in i:
                if char in ("3", "4", "7"):
                    check = False
                    break
                if char in ("0", "1", "8"):
                    tmp.append(char)
                if char == "2":
                    tmp.append("5")
                if char == "5":
                    tmp.append("2")
                if char == "6":
                    tmp.append("9")
                if char == "9":
                    tmp.append("6")
            if check and i != "".join(tmp): res += 1
        return res
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 1

#4 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;

namespace LeetCode
{
    public class _0788_RotatedDigits
    {
        public int RotatedDigits(int N)
        {
            var num = N.ToString();
            var length = num.Length;

            var memo = new Dictionary < (int length, bool equality_flag, bool involution_flag), int>();
            return DP(0, true, false, num, length, memo);
        }

        public int DP(int k, bool equality_flag, bool involution_flag, string num, int length, IDictionary < (int length, bool equality_flag, bool involution_flag), int> memo)
        {
            if (k == length) return involution_flag ? 1 : 0;
            if (!memo.ContainsKey((k, equality_flag, involution_flag)))
            {
                var current = 0;
                for (char ch = '0'; ch  < = (equality_flag ? num[k] : '9'); ch++)
                {
                    if (ch == '3' || ch == '4' || ch == '7') continue;
                    current += DP(k + 1, equality_flag && ch == num[k], involution_flag || (ch == '2' || ch == '5' || ch == '6' || ch == '9'), num, length, memo);
                }

                memo[(k, equality_flag, involution_flag)] = current;
            }

            return memo[(k, equality_flag, involution_flag)];
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 1
Advertisements

Demonstration


Previous
#787 Leetcode Cheapest Flights Within K Stops Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#789 Leetcode Escape The Ghosts Solution in C, C++, Java, JavaScript, Python, C# Leetcode