[LeetCode] Number of Times Binary String Is Prefix-Aligned

1375. Number of Times Binary String Is Prefix-Aligned

You have a 1-indexed binary string of length n where all the bits are 0 initially. We will flip all the bits of this binary string (i.e., change them from 0 to 1) one by one. You are given a 1-indexed integer array flips where flips[i] indicates that the bit at index i will be flipped in the ith step.

A binary string is prefix-aligned if, after the ith step, all the bits in the inclusive range [1, i] are ones and all the other bits are zeros.

Return the number of times the binary string is prefix-aligned during the flipping process.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int numTimesAllBlue(vector<int>& flips) {
int ma = 0, c = 0, res = 0;
for(auto& f : flips) {
ma = max(ma, f);
c++;
if(ma == c) res++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/20/PS/LeetCode/number-of-times-binary-string-is-prefix-aligned/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.