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.length
1 <= m <= n <= 105
1 <= arr[i] <= n
arr
are distinct.This problem asks to find the latest step at which there exists a contiguous group of '1's of length m
in a binary string, where bits are set to '1' according to the order specified in the input array arr
.
This approach utilizes the Union-Find data structure to efficiently track contiguous groups of '1's.
Initialization:
parent
array p
and a size
array to represent the Union-Find structure. Initially, each element is its own parent (p[i] = i
), and each group has size 1 (size[i] = 1
).vis
array tracks which bits have been set to '1'.Iteration:
arr
array. For each element v
, it sets the corresponding bit in the implicit binary string to '1' (vis[v-1] = True
).v
. If a neighbor is already set to '1', it performs a union operation using union(v, v-1)
or union(v, v+1)
. The union
function merges the groups represented by v
and its neighbor, updating the parent
and size
arrays accordingly.m
. If it is, the current step i
is recorded as a potential answer (ans
).Return Value: The function returns the latest step ans
at which a group of size m
was found. If no such group was found, it returns -1.
Time Complexity: O(N log N) in the worst case, where N is the length of arr
, due to the Union-Find operations. The find
operation, which is used in union
, can have logarithmic time complexity depending on the implementation.
Space Complexity: O(N) to store the Union-Find data structures (p
, size
, vis
).
This approach leverages interval merging techniques.
Initialization:
cnt
array of size n+2
is used to store the length of contiguous sequences of 1's. cnt[i]
denotes the length of the sequence ending at position i
. The extra space handles boundary cases.Iteration:
arr
. For each element v
, it updates the cnt
array to reflect the newly set bit.l
and r
, which represents the lengths of the sequences of 1's to the left and right of v
respectively.m
: If either l
or r
is equal to m
, it means a group of size m
is found at this step. The index i
is stored in ans
.l
and r
are merged to form a new sequence of length l + r + 1
. The cnt
array is updated accordingly.Return Value: The function returns the latest step (ans
) where a group of size m
was found, or -1 if no such group was found.
Time Complexity: O(N), where N is the length of arr
. The code iterates through the array once, and the operations within the loop are constant time.
Space Complexity: O(N) for the cnt
array.
The second approach (interval merging) is generally more efficient due to its linear time complexity. The Union-Find approach is a more general technique that can handle more complex scenarios involving disjoint set operations. However, for this specific problem, the interval merging approach provides a simpler and more efficient solution.