2025-11-18 Daily Challenge

Today I have done leetcode's November LeetCoding Challenge with cpp.

November LeetCoding Challenge 18

Description

1-bit and 2-bit Characters

We have two special characters:

  • The first character can be represented by one bit 0.
  • The second character can be represented by two bits (10 or 11).

Given a binary array bits that ends with 0, return true if the last character must be a one-bit character.

 

Example 1:

Input: bits = [1,0,0]
Output: true
Explanation: The only way to decode it is two-bit character and one-bit character.
So the last character is one-bit character.

Example 2:

Input: bits = [1,1,1,0]
Output: false
Explanation: The only way to decode it is two-bit character and two-bit character.
So the last character is not one-bit character.

 

Constraints:

  • 1 <= bits.length <= 1000
  • bits[i] is either 0 or 1.

Solution

class Solution {
public:
  bool isOneBitCharacter(vector<int>& bits) {
    int len = bits.size();
    for(int i = 0; i < len;) {
      if(bits[i]) {
        i += 2;
        if(i == len) return false;
      } else {
        i += 1;
        if(i == len) return true;
      }
    }
    return false;
  }
};

// Accepted
// 93/93 cases passed (0 ms)
// Your runtime beats 100 % of cpp submissions
// Your memory usage beats 8.09 % of cpp submissions (13.2 MB)