1375. Number of Times Binary String Is Prefix-Aligned Medium

@problem@discussion
#Array



1/**
2 * [1375] Number of Times Binary String Is Prefix-Aligned
3 *
4 * 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 i^th step.
5 * A binary string is prefix-aligned if, after the i^th step, all the bits in the inclusive range [1, i] are ones and all the other bits are zeros.
6 * Return the number of times the binary string is prefix-aligned during the flipping process.
7 *  
8 * Example 1:
9 * 
10 * Input: flips = [3,2,4,1,5]
11 * Output: 2
12 * Explanation: The binary string is initially "00000".
13 * After applying step 1: The string becomes "00100", which is not prefix-aligned.
14 * After applying step 2: The string becomes "01100", which is not prefix-aligned.
15 * After applying step 3: The string becomes "01110", which is not prefix-aligned.
16 * After applying step 4: The string becomes "11110", which is prefix-aligned.
17 * After applying step 5: The string becomes "11111", which is prefix-aligned.
18 * We can see that the string was prefix-aligned 2 times, so we return 2.
19 * 
20 * Example 2:
21 * 
22 * Input: flips = [4,1,2,3]
23 * Output: 1
24 * Explanation: The binary string is initially "0000".
25 * After applying step 1: The string becomes "0001", which is not prefix-aligned.
26 * After applying step 2: The string becomes "1001", which is not prefix-aligned.
27 * After applying step 3: The string becomes "1101", which is not prefix-aligned.
28 * After applying step 4: The string becomes "1111", which is prefix-aligned.
29 * We can see that the string was prefix-aligned 1 time, so we return 1.
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	n == flips.length
35 * 	1 <= n <= 5 * 10^4
36 * 	flips is a permutation of the integers in the range [1, n].
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/number-of-times-binary-string-is-prefix-aligned/
42// discuss: https://leetcode.com/problems/number-of-times-binary-string-is-prefix-aligned/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn num_times_all_blue(flips: Vec<i32>) -> i32 {
48        0
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_1375() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.