<p>Given an array <code>arr</code> that represents a permutation of numbers from <code>1</code> to <code>n</code>.</p>

<p>You have a binary string of size <code>n</code> that initially has all its bits set to zero. At each step <code>i</code> (assuming both the binary string and <code>arr</code> are 1-indexed) from <code>1</code> to <code>n</code>, the bit at position <code>arr[i]</code> is set to <code>1</code>.</p>

<p>You are also given an integer <code>m</code>. Find the latest step at which there exists a group of ones of length <code>m</code>. A group of ones is a contiguous substring of <code>1</code>&#39;s such that it cannot be extended in either direction.</p>

<p>Return <em>the latest step at which there exists a group of ones of length <strong>exactly</strong></em> <code>m</code>. <em>If no such group exists, return</em> <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [3,5,1,2,4], m = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> 
Step 1: &quot;00<u>1</u>00&quot;, groups: [&quot;1&quot;]
Step 2: &quot;0010<u>1</u>&quot;, groups: [&quot;1&quot;, &quot;1&quot;]
Step 3: &quot;<u>1</u>0101&quot;, groups: [&quot;1&quot;, &quot;1&quot;, &quot;1&quot;]
Step 4: &quot;1<u>1</u>101&quot;, groups: [&quot;111&quot;, &quot;1&quot;]
Step 5: &quot;111<u>1</u>1&quot;, groups: [&quot;11111&quot;]
The latest step at which there exists a group of size 1 is step 4.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [3,1,5,4,2], m = 2
<strong>Output:</strong> -1
<strong>Explanation:</strong> 
Step 1: &quot;00<u>1</u>00&quot;, groups: [&quot;1&quot;]
Step 2: &quot;<u>1</u>0100&quot;, groups: [&quot;1&quot;, &quot;1&quot;]
Step 3: &quot;1010<u>1</u>&quot;, groups: [&quot;1&quot;, &quot;1&quot;, &quot;1&quot;]
Step 4: &quot;101<u>1</u>1&quot;, groups: [&quot;1&quot;, &quot;111&quot;]
Step 5: &quot;1<u>1</u>111&quot;, groups: [&quot;11111&quot;]
No group of size 2 exists during any step.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == arr.length</code></li>
	<li><code>1 &lt;= m &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= arr[i] &lt;= n</code></li>
	<li>All integers in <code>arr</code> are <strong>distinct</strong>.</li>
</ul>

from collections import defaultdict

class Solution:
    def findLatestStep(self, arr: List[int], m: int) -> int:
        
        size = len(arr)
        
        if size == m:
            
            # Quick response for simple case
            # total m flips and m is equal to array size
            
            return size
        
        
        
        # key: length of substring with contiguous 1s
        # value: the count of substring with contiguous 1s with specified length, initialized as 0
        counter_of_len = defaultdict(lambda :0)
        
        
        # index: i
        # value: length of substring with contiguous 1s, which covers index i
        # +1 padding on head, +1 padding on tail, total + 2 to avoid index out-of-boundary
        len_of_cont_1s = [0] * ( size + 2 )
        
        
        last_good_step = -1
        
        # flip cur_idx to 1, and update information of length of 1s as well as count of 1s
        for step, cur_idx in enumerate(arr, 1):
            
            # get original length of 1s on left border and right border
            left_1s_length = len_of_cont_1s[cur_idx - 1]
            right_1s_length = len_of_cont_1s[cur_idx + 1]
            
            
            # compute the new length of 1s after flipping
            combined_1s_length = left_1s_length + 1 + right_1s_length
            
            
            # update new length of 1s on left borader and right border
            len_of_cont_1s[cur_idx - left_1s_length] = combined_1s_length
            len_of_cont_1s[cur_idx + right_1s_length] = combined_1s_length
            
            
            # update counter_of_len dictionary after flipping
            counter_of_len[combined_1s_length] += 1
            counter_of_len[left_1s_length] -= 1
            counter_of_len[right_1s_length] -= 1
                
                
            if counter_of_len[m] > 0:
                # now we still have substring of contiguous 1s with length exactly equal to m
                last_good_step = step
        
        
        return last_good_step
738