LeetCode Entry
717. 1-bit and 2-bit Characters
Is '0' last from characters '0','10','11' concatenation
717. 1-bit and 2-bit Characters easy blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1177
Problem TLDR
Is ‘0’ last from characters ‘0’,’10’,’11’ concatenation #easy
Intuition
I had to start with DP: in each i position if b[i] zero go next, otherwise go next->next. Then it is just a linear solution without a choice.
Approach
- optimization: go from back
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
// 0ms
fun isOneBitCharacter(b: IntArray): Boolean {
var i = b.size - 2
while (i >= 0 && b[i] > 0) i -= b[i]
return (b.size - i) % 2 < 1
}
// 0ms
pub fn is_one_bit_character(b: Vec<i32>) -> bool {
b[..b.len()-1].split(|&x|x<1).last().unwrap().len()%2<1
}