Find Latest Group of Size M Problem
Description
LeetCode Problem 1562.
Given an array arr that represents a permutation of numbers from 1 to n.
You have a binary string of size n that initially has all its bits set to zero. At each step i (assuming both the binary string and arr are 1-indexed) from 1 to n, the bit at position arr[i] is set to 1.
You are also given an integer m. Find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1's such that it cannot be extended in either direction.
Return the latest step at which there exists a group of ones of length exactly m. If no such group exists, return -1.
Example 1:
Input: arr = [3,5,1,2,4], m = 1
Output: 4
Explanation:
Step 1: "00100", groups: ["1"]
Step 2: "00101", groups: ["1", "1"]
Step 3: "10101", groups: ["1", "1", "1"]
Step 4: "11101", groups: ["111", "1"]
Step 5: "11111", groups: ["11111"]
The latest step at which there exists a group of size 1 is step 4.
Example 2:
Input: arr = [3,1,5,4,2], m = 2
Output: -1
Explanation:
Step 1: "00100", groups: ["1"]
Step 2: "10100", groups: ["1", "1"]
Step 3: "10101", groups: ["1", "1", "1"]
Step 4: "10111", groups: ["1", "111"]
Step 5: "11111", groups: ["11111"]
No group of size 2 exists during any step.
Constraints:
n == arr.length1 <= m <= n <= 10^51 <= arr[i] <= n- All integers in
arrare distinct.
Solution
Approach: Track Group Lengths at Their Ends
When a bit is set to 1, it joins the group of ones on its left (if any) and the group on its right (if any) into one group. We only need to know the lengths of those neighbor groups, and a group's length only needs to be stored at its two end positions, because those are the only cells a new bit can touch.
Keep length[p], the length of the group that ends at position p (with padding at 0 and n + 1). When bit p is set:
left = length[p - 1]andright = length[p + 1]are the neighbor groups (0if there are none).- The merged group runs from
p - lefttop + right; storeleft + right + 1at both ends.
Now the trick for the answer: a group of size exactly m stops existing only when a new bit merges it into something bigger. So if left == m or right == m, a group of size m existed right up to the previous step, and that step is a candidate. With 0-based step, the previous step's 1-based number is exactly step. Keep the latest such value.
One case is missed this way: a group that is never merged. That only happens to the final group, which is the whole string of length n. So when m == n the answer is n; otherwise it is the latest candidate, or -1 if there was none.
Complexity: O(n) time and O(n) extra space.
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
if m == n:
return n # the final string is one group of length n
# length[p]: length of the group of ones that has position p as one of its two ends
length = [0] * (n + 2)
latest = -1
for step, p in enumerate(arr): # step is 0-based, so it equals the 1-based number of the previous step
left, right = length[p - 1], length[p + 1]
if left == m or right == m:
latest = step # a group of length m existed until now; this step merges it away
length[p - left] = length[p + right] = left + right + 1
return latest