mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
47 lines
2.2 KiB
HTML
47 lines
2.2 KiB
HTML
|
<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>'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> </p>
|
||
|
<p><strong>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: "00<u>1</u>00", groups: ["1"]
|
||
|
Step 2: "0010<u>1</u>", groups: ["1", "1"]
|
||
|
Step 3: "<u>1</u>0101", groups: ["1", "1", "1"]
|
||
|
Step 4: "1<u>1</u>101", groups: ["111", "1"]
|
||
|
Step 5: "111<u>1</u>1", groups: ["11111"]
|
||
|
The latest step at which there exists a group of size 1 is step 4.
|
||
|
</pre>
|
||
|
|
||
|
<p><strong>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: "00<u>1</u>00", groups: ["1"]
|
||
|
Step 2: "<u>1</u>0100", groups: ["1", "1"]
|
||
|
Step 3: "1010<u>1</u>", groups: ["1", "1", "1"]
|
||
|
Step 4: "101<u>1</u>1", groups: ["1", "111"]
|
||
|
Step 5: "1<u>1</u>111", groups: ["11111"]
|
||
|
No group of size 2 exists during any step.
|
||
|
</pre>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Constraints:</strong></p>
|
||
|
|
||
|
<ul>
|
||
|
<li><code>n == arr.length</code></li>
|
||
|
<li><code>1 <= m <= n <= 10<sup>5</sup></code></li>
|
||
|
<li><code>1 <= arr[i] <= n</code></li>
|
||
|
<li>All integers in <code>arr</code> are <strong>distinct</strong>.</li>
|
||
|
</ul>
|