Skip to content

Commit f12f451

Browse files
chore: add LeetCode daily solution
1 parent c10fd7b commit f12f451

5 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Rotated Digits (Medium)
2+
3+
**Problem ID:** 788
4+
**Date:** 2026-05-02
5+
**Link:** https://leetcode.com/problems/rotated-digits/
6+
7+
## Approach
8+
9+
To solve the "Rotated Digits" problem, we need to determine how many integers in the range [1, n] are "good" based on the rotation rules provided. A number is classified as "good" if, after rotating each of its digits by 180 degrees, it results in a valid number that is different from the original number.
10+
11+
### Approach:
12+
13+
1. **Understanding Digit Rotation**:
14+
- We first identify which digits are valid after rotation:
15+
- Valid digits: 0, 1, 8 (remain the same)
16+
- Valid pairs: 2 ↔ 5, 6 ↔ 9
17+
- Invalid digits: 3, 4, 7 (become invalid)
18+
- A "good" number must contain at least one of the digits that change (2, 5, 6, or 9) and must not consist solely of valid digits that remain unchanged (0, 1, 8).
19+
20+
2. **Iterate Through the Range**:
21+
- We will iterate through each number from 1 to n and check its digits.
22+
- For each number, we will convert it to its string representation to easily access each digit.
23+
24+
3. **Check Each Digit**:
25+
- For each digit in the number:
26+
- If any digit is invalid (3, 4, 7), we can skip to the next number.
27+
- If we encounter a digit that changes (2, 5, 6, or 9), we mark this number as "good".
28+
29+
4. **Count Good Numbers**:
30+
- Maintain a counter to keep track of how many "good" numbers we find during our iteration.
31+
32+
### Data Structures:
33+
- We primarily use a simple integer counter to track the number of good numbers.
34+
- String representation of numbers allows for easy digit access and checking.
35+
36+
### Complexity:
37+
- **Time Complexity**: O(n * d), where n is the input number and d is the number of digits in n. In the worst case, d is log10(n), making the complexity effectively O(n) for the upper limit of n (10,000).
38+
- **Space Complexity**: O(1), as we only use a few variables to store counts and check digits.
39+
40+
By following this structured approach, we can efficiently determine the count of good integers in the specified range.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int rotatedDigits(int n) {
3+
int count = 0;
4+
for (int i = 1; i <= n; i++) {
5+
if (isGood(i)) {
6+
count++;
7+
}
8+
}
9+
return count;
10+
}
11+
12+
private boolean isGood(int num) {
13+
boolean good = false;
14+
while (num > 0) {
15+
int digit = num % 10;
16+
if (digit == 3 || digit == 4 || digit == 7) {
17+
return false;
18+
}
19+
if (digit == 2 || digit == 5 || digit == 6 || digit == 9) {
20+
good = true;
21+
}
22+
num /= 10;
23+
}
24+
return good;
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var rotatedDigits = function(n) {
2+
const goodDigits = new Set(['2', '5', '6', '9']);
3+
const validDigits = new Set(['0', '1', '8', '2', '5', '6', '9']);
4+
5+
let count = 0;
6+
7+
for (let i = 1; i <= n; i++) {
8+
const strNum = i.toString();
9+
let isGood = false;
10+
let isValid = true;
11+
12+
for (const char of strNum) {
13+
if (!validDigits.has(char)) {
14+
isValid = false;
15+
break;
16+
}
17+
if (goodDigits.has(char)) {
18+
isGood = true;
19+
}
20+
}
21+
22+
if (isValid && isGood) {
23+
count++;
24+
}
25+
}
26+
27+
return count;
28+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def rotatedDigits(self, n: int) -> int:
3+
good_count = 0
4+
for i in range(1, n + 1):
5+
s = str(i)
6+
if all(c in '0125689' for c in s) and any(c in '2569' for c in s):
7+
good_count += 1
8+
return good_count

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
441441
- 2026-04-29 — [Maximum Score From Grid Operations](https://leetcode.com/problems/maximum-score-from-grid-operations/) (Hard) → `Hard/2026-04-29-3225-Maximum-Score-From-Grid-Operations`
442442
- 2026-04-30 — [Maximum Path Score in a Grid](https://leetcode.com/problems/maximum-path-score-in-a-grid/) (Medium) → `Medium/2026-04-30-3742-Maximum-Path-Score-in-a-Grid`
443443
- 2026-05-01 — [Rotate Function](https://leetcode.com/problems/rotate-function/) (Medium) → `Medium/2026-05-01-396-Rotate-Function`
444+
- 2026-05-02 — [Rotated Digits](https://leetcode.com/problems/rotated-digits/) (Medium) → `Medium/2026-05-02-788-Rotated-Digits`

0 commit comments

Comments
 (0)