Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 flips[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.
Example 1:
Input: flips = [3,2,4,1,5] Output: 2 Explanation: The binary string is initially "00000". After applying step 1: The string becomes "00100", which is not prefix-aligned. After applying step 2: The string becomes "01100", which is not prefix-aligned. After applying step 3: The string becomes "01110", which is not prefix-aligned. After applying step 4: The string becomes "11110", which is prefix-aligned. After applying step 5: The string becomes "11111", which is prefix-aligned. We can see that the string was prefix-aligned 2 times, so we return 2.
Example 2:
Input: flips = [4,1,2,3] Output: 1 Explanation: The binary string is initially "0000". After applying step 1: The string becomes "0001", which is not prefix-aligned. After applying step 2: The string becomes "1001", which is not prefix-aligned. After applying step 3: The string becomes "1101", which is not prefix-aligned. After applying step 4: The string becomes "1111", which is prefix-aligned. We can see that the string was prefix-aligned 1 time, so we return 1.
Constraints:
n == flips.length1 <= n <= 5 * 104flips is a permutation of the integers in the range [1, n].Problem summary: 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 flips[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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[3,2,4,1,5]
[4,1,2,3]
bulb-switcher)bulb-switcher-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1375: Number of Times Binary String Is Prefix-Aligned
class Solution {
public int numTimesAllBlue(int[] flips) {
int ans = 0, mx = 0;
for (int i = 1; i <= flips.length; ++i) {
mx = Math.max(mx, flips[i - 1]);
if (mx == i) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1375: Number of Times Binary String Is Prefix-Aligned
func numTimesAllBlue(flips []int) (ans int) {
mx := 0
for i, x := range flips {
mx = max(mx, x)
if mx == i+1 {
ans++
}
}
return
}
# Accepted solution for LeetCode #1375: Number of Times Binary String Is Prefix-Aligned
class Solution:
def numTimesAllBlue(self, flips: List[int]) -> int:
ans = mx = 0
for i, x in enumerate(flips, 1):
mx = max(mx, x)
ans += mx == i
return ans
// Accepted solution for LeetCode #1375: Number of Times Binary String Is Prefix-Aligned
struct Solution;
impl Solution {
fn num_times_all_blue(light: Vec<i32>) -> i32 {
let n = light.len();
let mut res = 0;
let mut max = std::usize::MIN;
for i in 0..n {
max = max.max((light[i] - 1) as usize);
if max == i {
res += 1;
}
}
res
}
}
#[test]
fn test() {
let light = vec![2, 1, 3, 5, 4];
let res = 3;
assert_eq!(Solution::num_times_all_blue(light), res);
let light = vec![3, 2, 4, 1, 5];
let res = 2;
assert_eq!(Solution::num_times_all_blue(light), res);
let light = vec![4, 1, 2, 3];
let res = 1;
assert_eq!(Solution::num_times_all_blue(light), res);
let light = vec![2, 1, 4, 3, 6, 5];
let res = 3;
assert_eq!(Solution::num_times_all_blue(light), res);
let light = vec![1, 2, 3, 4, 5, 6];
let res = 6;
assert_eq!(Solution::num_times_all_blue(light), res);
}
// Accepted solution for LeetCode #1375: Number of Times Binary String Is Prefix-Aligned
function numTimesAllBlue(flips: number[]): number {
let ans = 0;
let mx = 0;
for (let i = 1; i <= flips.length; ++i) {
mx = Math.max(mx, flips[i - 1]);
if (mx === i) {
++ans;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.