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, andGiven 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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to this problem involves checking every number within the given range. For each number, we'll determine if it's a good number, meaning it becomes a different valid number when rotated 180 degrees.
Here's how the algorithm would work step-by-step:
def rotated_digits(number_limit):
good_numbers_count = 0
for current_number in range(1, number_limit + 1):
number_string = str(current_number)
is_good_number = True
rotated_number_string = ""
for digit_char in number_string:
if digit_char in ('3', '4', '7'):
is_good_number = False
break
# Build rotated number string based on digits
if digit_char == '0':
rotated_number_string += '0'
elif digit_char == '1':
rotated_number_string += '1'
elif digit_char == '2':
rotated_number_string += '5'
elif digit_char == '5':
rotated_number_string += '2'
elif digit_char == '6':
rotated_number_string += '9'
elif digit_char == '8':
rotated_number_string += '8'
elif digit_char == '9':
rotated_number_string += '6'
if is_good_number:
# Check to see rotated version is different
if number_string != rotated_number_string:
good_numbers_count += 1
return good_numbers_count
We need to count numbers that become a different valid number when rotated 180 degrees. The clever approach is to build valid rotated numbers digit by digit, avoiding unnecessary checks. This saves time compared to checking every single number individually.
Here's how the algorithm would work step-by-step:
def rotated_digits(number):
count_valid_rotated = 0
string_number = str(number)
number_length = len(string_number)
def is_good_number(index, is_different, is_smaller):
if index == number_length:
return is_different
upper_bound = int(string_number[index]) if is_smaller else 9
total_numbers = 0
for digit in range(upper_bound + 1):
if digit in [3, 4, 7]:
continue
new_is_different = is_different or digit in [2, 5, 6, 9]
new_is_smaller = is_smaller or digit < int(string_number[index])
total_numbers += is_good_number(index + 1, new_is_different, new_is_smaller)
return total_numbers
count_valid_rotated = is_good_number(0, False, True)
return count_valid_rotated
Case | How to Handle |
---|---|
Input N is zero | Return 0, as no numbers less than or equal to 0 can be considered 'good'. |
Input N is a single digit number (e.g., 2, 5, 6, 9) | Manually check if the number is a good number and return 1 or 0. |
N is a power of 10 (e.g., 10, 100, 1000) | Ensure the loop iterates correctly and calculates the 'good' numbers accurately up to the power of 10. |
N is very large (approaching integer limits) | The solution should be efficient enough to handle large values of N without exceeding time limits or causing integer overflow (consider using int vs long). |
Numbers containing only 'good' digits (2, 5, 6, 9) | Numbers like 259 should be correctly identified as 'good'. |
Numbers containing no 'good' digits but containing valid digits (0, 1, 8) | Numbers like 108 should be correctly identified as not 'good'. |
Numbers containing both 'good' and invalid digits (3, 4, 7) | Numbers like 32 should be correctly identified as not 'good'. |
N contains leading zeros (invalid input but might occur) | The problem statement implies that N is a positive integer, so you should assume there will be no leading zeros or the input will be handled to remove them. |