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 lengthexactlym. If no such group exists, return-1.
We need to efficiently track contiguous groups of 1s and their sizes as we flip bits in the order given by arr. By using an array to track the length of each group and a counter for groups of size m, we can update group sizes in constant time per step.
classSolution {
public:int findLatestStep(vector<int>& arr, int m) {
int n = arr.size(), res =-1, cnt =0;
vector<int> length(n+2);
vector<int> count(n+1);
for (int i =0; i < n; ++i) {
int pos = arr[i];
int left = length[pos-1], right = length[pos+1];
int total = left + right +1;
length[pos-left] = length[pos+right] = total;
if (left >0) count[left]--;
if (right >0) count[right]--;
count[total]++;
if (count[m] >0) res = i+1;
}
return res;
}
};
classSolution {
publicintfindLatestStep(int[] arr, int m) {
int n = arr.length, res =-1;
int[] length =newint[n+2];
int[] count =newint[n+1];
for (int i = 0; i < n; i++) {
int pos = arr[i];
int left = length[pos-1], right = length[pos+1];
int total = left + right + 1;
length[pos-left]= length[pos+right]= total;
if (left > 0) count[left]--;
if (right > 0) count[right]--;
count[total]++;
if (count[m]> 0) res = i+1;
}
return res;
}
}
classSolution {
funfindLatestStep(arr: IntArray, m: Int): Int {
val n = arr.size
val length = IntArray(n+2)
val count = IntArray(n+1)
var res = -1for (i in arr.indices) {
val pos = arr[i]
val left = length[pos-1]
val right = length[pos+1]
val total = left + right + 1 length[pos-left] = total
length[pos+right] = total
if (left > 0) count[left]--if (right > 0) count[right]-- count[total]++if (count[m] > 0) res = i+1 }
return res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
deffindLatestStep(self, arr: list[int], m: int) -> int:
n = len(arr)
length = [0] * (n+2)
count = [0] * (n+1)
res =-1for i, pos in enumerate(arr):
left, right = length[pos-1], length[pos+1]
total = left + right +1 length[pos-left] = length[pos+right] = total
if left >0:
count[left] -=1if right >0:
count[right] -=1 count[total] +=1if count[m] >0:
res = i+1return res
impl Solution {
pubfnfind_latest_step(arr: Vec<i32>, m: i32) -> i32 {
let n = arr.len();
letmut length =vec![0; n+2];
letmut count =vec![0; n+1];
letmut res =-1;
for (i, &pos) in arr.iter().enumerate() {
let pos = pos asusize;
let left = length[pos-1];
let right = length[pos+1];
let total = left + right +1;
length[pos-left] = total;
length[pos+right] = total;
if left >0 { count[left] -=1; }
if right >0 { count[right] -=1; }
count[total] +=1;
if count[m asusize] >0 { res = (i+1) asi32; }
}
res
}
}