Skip to content

Commit 2d7d377

Browse files
chore: add LeetCode daily solution
1 parent 6d4f4ea commit 2d7d377

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 1-bit and 2-bit Characters (Easy)
2+
3+
**Problem ID:** 717
4+
**Date:** 2025-11-18
5+
**Link:** https://leetcode.com/problems/1-bit-and-2-bit-characters/
6+
7+
## Approach
8+
9+
To solve the problem of determining whether the last character in a binary array can be a one-bit character, we can use a straightforward approach by iterating through the array from the end towards the beginning. The key points of the solution are as follows:
10+
11+
### Main Idea:
12+
1. **Understanding Character Representation**: We know that:
13+
- A one-bit character is represented by `0`.
14+
- A two-bit character is represented by either `10` or `11`.
15+
- The array always ends with `0`, which means the last character is guaranteed to be a one-bit character if it is not preceded by a valid two-bit character.
16+
17+
2. **Iterative Backtracking**: Starting from the second last element of the array (since the last element is always `0`):
18+
- If we encounter a `1`, it indicates the start of a two-bit character. We then skip the next bit (i.e., move back two positions).
19+
- If we encounter a `0`, it indicates the end of a one-bit character, and we move back one position.
20+
- Continue this process until we either reach the beginning of the array or determine the nature of the last character.
21+
22+
3. **Decision Point**: If we finish the iteration and the pointer is at a position that allows for a one-bit character (i.e., we are at the start of the array or have only a `0` left), we can conclude that the last character must be a one-bit character.
23+
24+
### Data Structures:
25+
- We primarily use a simple integer variable to track our position in the array, and no additional data structures are required.
26+
27+
### Complexity:
28+
- **Time Complexity**: O(n), where n is the length of the input array. In the worst case, we might need to traverse the entire array once.
29+
- **Space Complexity**: O(1), as we only use a constant amount of extra space for our pointer.
30+
31+
This approach efficiently determines whether the last character is a one-bit character by leveraging the properties of the binary representation and iterating through the array in a linear fashion.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean isOneBitCharacter(int[] bits) {
3+
int n = bits.length;
4+
int i = 0;
5+
6+
while (i < n - 1) {
7+
if (bits[i] == 1) {
8+
i += 2; // Skip the next bit as it's part of a two-bit character
9+
} else {
10+
i++; // Move to the next bit
11+
}
12+
}
13+
14+
return i == n - 1; // Check if we ended on a one-bit character
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var isOneBitCharacter = function(bits) {
2+
let i = 0;
3+
while (i < bits.length - 1) {
4+
i += bits[i] === 1 ? 2 : 1;
5+
}
6+
return i === bits.length - 1;
7+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def isOneBitCharacter(self, bits: List[int]) -> bool:
3+
n = len(bits)
4+
i = 0
5+
6+
while i < n - 1:
7+
if bits[i] == 1:
8+
i += 2
9+
else:
10+
i += 1
11+
12+
return i == n - 1

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
282282
- 2025-11-15 — [Count the Number of Substrings With Dominant Ones](https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/) (Medium) → `Medium/2025-11-15-3234-Count-the-Number-of-Substrings-With-Dominant-Ones`
283283
- 2025-11-16 — [Number of Substrings With Only 1s](https://leetcode.com/problems/number-of-substrings-with-only-1s/) (Medium) → `Medium/2025-11-16-1513-Number-of-Substrings-With-Only-1s`
284284
- 2025-11-17 — [Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/) (Easy) → `Easy/2025-11-17-1437-Check-If-All-1-s-Are-at-Least-Length-K-Places-Away`
285+
- 2025-11-18 — [1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/) (Easy) → `Easy/2025-11-18-717-1-bit-and-2-bit-Characters`

0 commit comments

Comments
 (0)