java.lang.Object
g1301_1400.s1375_number_of_times_binary_string_is_prefix_aligned.Solution

public class Solution extends Object
1375 - Number of Times Binary String Is Prefix-Aligned.<p>Medium</p> <p>You have a <strong>1-indexed</strong> binary string of length <code>n</code> where all the bits are <code>0</code> initially. We will flip all the bits of this binary string (i.e., change them from <code>0</code> to <code>1</code>) one by one. You are given a <strong>1-indexed</strong> integer array <code>flips</code> where <code>flips[i]</code> indicates that the bit at index <code>i</code> will be flipped in the <code>i<sup>th</sup></code> step.</p> <p>A binary string is <strong>prefix-aligned</strong> if, after the <code>i<sup>th</sup></code> step, all the bits in the <strong>inclusive</strong> range <code>[1, i]</code> are ones and all the other bits are zeros.</p> <p>Return <em>the number of times the binary string is <strong>prefix-aligned</strong> during the flipping process</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> flips = [3,2,4,1,5]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> The binary string is initially &ldquo;00000&rdquo;.</p> <p>After applying step 1: The string becomes &ldquo;00100&rdquo;, which is not prefix-aligned.</p> <p>After applying step 2: The string becomes &ldquo;01100&rdquo;, which is not prefix-aligned.</p> <p>After applying step 3: The string becomes &ldquo;01110&rdquo;, which is not prefix-aligned.</p> <p>After applying step 4: The string becomes &ldquo;11110&rdquo;, which is prefix-aligned.</p> <p>After applying step 5: The string becomes &ldquo;11111&rdquo;, which is prefix-aligned.</p> <p>We can see that the string was prefix-aligned 2 times, so we return 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> flips = [4,1,2,3]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> The binary string is initially &ldquo;0000&rdquo;.</p> <p>After applying step 1: The string becomes &ldquo;0001&rdquo;, which is not prefix-aligned.</p> <p>After applying step 2: The string becomes &ldquo;1001&rdquo;, which is not prefix-aligned.</p> <p>After applying step 3: The string becomes &ldquo;1101&rdquo;, which is not prefix-aligned.</p> <p>After applying step 4: The string becomes &ldquo;1111&rdquo;, which is prefix-aligned.</p> <p>We can see that the string was prefix-aligned 1 time, so we return 1.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == flips.length</code></li> <li><code>1 <= n <= 5 * 10<sup>4</sup></code></li> <li><code>flips</code> is a permutation of the integers in the range <code>[1, n]</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numTimesAllBlue

      public int numTimesAllBlue(int[] flips)