description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 2 \cdot 10^{5}$
$1 ≤ A_{i} ≤ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | t = int(input())
for i in range(t):
n = int(input())
l = list(map(int, input().split()))
if n == 1:
print(0)
elif len(set(l)) == len(l):
print(-1)
else:
y = 0
d = {}
for i in l:
try:
d[i] += 1
if d[i] > y:
y = d[i]
except KeyError:
d[i] = 1
if n == y:
print(0)
else:
print(n - y + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 2 \cdot 10^{5}$
$1 ≤ A_{i} ≤ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
d = {}
if n == 1:
print(0)
else:
for i in a:
if i in d:
d[i] += 1
else:
d[i] = 1
m = m1 = 0
for i in d.keys():
if m < d[i]:
m = d[i]
m1 = i
if n - m + 1 == n:
print(-1)
elif n - m + 1 == 1:
print(0)
else:
print(n - m + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 2 \cdot 10^{5}$
$1 ≤ A_{i} ≤ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | import sys
T = int(sys.stdin.readline())
for i in range(T):
N = int(sys.stdin.readline())
myList = list(map(int, sys.stdin.readline().split()))
myDict = {}
maxCounter = 0
for i in myList:
if i in myDict:
myDict[i] += 1
else:
myDict[i] = 1
if myDict[i] > maxCounter:
maxCounter = myDict[i]
if maxCounter == 1 and N > 1:
print(-1)
elif N == 1:
print(0)
elif N == maxCounter:
print(0)
else:
print(N - maxCounter + 1) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 2 \cdot 10^{5}$
$1 ≤ A_{i} ≤ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | t = int(input())
for u in range(t):
n = int(input())
l = list(map(int, input().split()))
d, f = {}, False
m, ans = -(2**32), 0
for i in l:
if i in d:
d[i] += 1
f = True
else:
d[i] = 1
m = max(m, d[i])
if f:
if m == n:
print(0)
else:
ans = n - m
print(ans + 1)
elif n == 1:
print(0)
else:
print(-1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR DICT NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 2 \cdot 10^{5}$
$1 ≤ A_{i} ≤ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
freq = {}
for item in l:
if item in freq:
freq[item] += 1
else:
freq[item] = 1
maxi = max(freq.values())
if len(l) == 1:
print(0)
elif maxi == 1:
print(-1)
elif maxi == len(l):
print(0)
else:
print(len(l) - maxi + 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 2 \cdot 10^{5}$
$1 ≤ A_{i} ≤ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | try:
t = input()
for i in range(int(t)):
n = input()
a = input()
l = list(map(int, a.split(" ")))
mx = 1
c = 1
l.sort()
for k in range(1, int(n)):
if l[k] == l[k - 1]:
c += 1
mx = max(mx, c)
else:
c = 1
if int(n) == mx:
print(0)
elif mx == 1:
print(-1)
else:
print(int(n) - mx + 1)
except EOFError as e:
print() | ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 2 \cdot 10^{5}$
$1 ≤ A_{i} ≤ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | for _ in range(int(input())):
n = map(int, input().split())
l = list(map(int, input().split()))
if len(l) == 1:
print(0)
elif len(l) == len(set(l)):
print(-1)
else:
d = dict()
for x in l:
if x not in d.keys():
d[x] = 1
else:
d[x] += 1
malele = max(d.values())
if malele == len(l):
print(0)
else:
ans = len(l) - malele + 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why? | class Solution:
def search(self, nums, target):
l, r = 0, len(nums) - 1
while l <= r:
m = l + (r - l) // 2
if nums[m] == target:
return True
while nums[m] == nums[l] and l < m:
l += 1
if nums[l] <= nums[m]:
if nums[l] <= target < nums[m]:
r = m - 1
else:
l = m + 1
elif nums[m] < target <= nums[r]:
l = m + 1
else:
r = m - 1
return False | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why? | class Solution:
def recursive(self, nums, left, right, target):
print((left, right))
if left > right:
return False
if left == right:
return nums[left] == target
if left + 1 == right:
return nums[left] == target or nums[right] == target
mid = (left + right) // 2
if nums[mid] == target:
return True
if nums[mid] == nums[left] and nums[mid] == nums[right]:
return self.recursive(nums, mid, right, target) or self.recursive(
nums, left, mid, target
)
elif nums[mid] < target:
if (
nums[right] >= target
or nums[left] <= nums[mid]
and nums[mid] >= nums[right]
):
return self.recursive(nums, mid, right, target)
else:
return self.recursive(nums, left, mid, target)
elif (
nums[left] <= target or nums[right] >= nums[mid] and nums[mid] <= nums[left]
):
return self.recursive(nums, left, mid, target)
else:
return self.recursive(nums, mid, right, target)
def search(self, nums, target):
return self.recursive(nums, 0, len(nums) - 1, target) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR VAR IF BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why? | class Solution:
def search(self, nums, target):
if not nums:
return False
i = 0
while i + 1 < len(nums) and nums[i] == nums[i + 1]:
i += 1
if i + 1 == len(nums):
return nums[0] == target
l, r = i, len(nums) - 1
while l + 1 < r:
mid = l + (r - l) // 2
if nums[i] < nums[mid]:
l = mid
else:
r = mid - 1
highest_pos = l if nums[l] > nums[r] else r
if target >= nums[0]:
return self._binarySearch(nums, 0, highest_pos, target)
else:
return self._binarySearch(nums, highest_pos + 1, len(nums) - 1, target)
def _binarySearch(self, nums, l, r, target):
while l <= r:
mid = l + (r - l) // 2
if nums[mid] < target:
l = mid + 1
elif nums[mid] > target:
r = mid - 1
else:
return True
return False | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why? | class Solution:
def search(self, nums, target):
if not nums:
return False
l, r = 0, len(nums) - 1
while l < r:
mid = (l + r) // 2
if nums[mid] == target:
return True
if nums[mid] < nums[r]:
if nums[mid] < target <= nums[r]:
l = mid + 1
else:
r = mid - 1
elif nums[mid] > nums[r]:
if nums[l] <= target < nums[mid]:
r = mid - 1
else:
l = mid + 1
else:
r -= 1
return nums[l] == target | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why? | class Solution:
res = False
def searchR(self, nums, target, start, end):
if start > end or self.res == True:
return self.res
else:
mid = int((start + end) / 2)
print("Mid index: %d, Mid val: %d, Target: %d" % (mid, nums[mid], target))
if nums[mid] == target:
self.res = True
elif nums[start] <= nums[mid]:
if nums[mid] == nums[end]:
self.searchR(nums, target, start, mid - 1)
self.searchR(nums, target, mid + 1, end)
elif target >= nums[start] and target <= nums[mid]:
self.searchR(nums, target, start, mid - 1)
else:
self.searchR(nums, target, mid + 1, end)
else:
print("Here")
if target >= nums[mid] and target <= nums[end]:
self.searchR(nums, target, mid + 1, end)
else:
self.searchR(nums, target, start, mid - 1)
return self.res
def search(self, nums, target):
res = self.searchR(nums, target, 0, len(nums) - 1)
return res | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why? | class Solution:
def search(self, nums, target):
start, end = 0, len(nums) - 1
while start <= end:
mid = start + (end - start) // 2
if nums[mid] == target:
return True
while start <= end and nums[start] == nums[end]:
if nums[start] == target:
return True
start += 1
end -= 1
if start > end:
return False
if nums[start] < nums[end]:
if target < nums[mid]:
end = mid - 1
else:
start = mid + 1
elif nums[mid] < nums[start]:
if target < nums[mid] or target >= nums[start]:
end = mid - 1
else:
start = mid + 1
elif target > nums[mid] or target <= nums[end]:
start = mid + 1
else:
end = mid - 1
return False | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER WHILE VAR VAR VAR VAR VAR VAR IF VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why? | class Solution:
def search(self, nums, target):
if len(nums) == 0:
return False
def binary_search(leftIndex, rightIndex):
midIndex = int((rightIndex + leftIndex) / 2)
if nums[midIndex] == target:
return True
if leftIndex > rightIndex:
return False
return binary_search(leftIndex, midIndex - 1) or binary_search(
midIndex + 1, rightIndex
)
return binary_search(0, len(nums) - 1) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why? | class Solution:
def search(self, nums, target):
if not nums:
return False
l = 0
r = len(nums) - 1
print((">", l, r))
while l < r and nums[l] == nums[r]:
l = l + 1
while l <= r:
mid = int((l + r + 1) / 2)
if target == nums[mid]:
return True
if target < nums[mid]:
if target == nums[l]:
return True
elif target > nums[l]:
r = mid - 1
elif target < nums[l]:
if nums[l] <= nums[mid]:
l = mid + 1
elif nums[l] > nums[mid]:
r = mid - 1
if target > nums[mid]:
if target == nums[r]:
return True
elif target > nums[r]:
if nums[mid] >= nums[l]:
l = mid + 1
elif nums[mid] < nums[l]:
r = mid - 1
elif target < nums[r]:
l = mid + 1
return False | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR VAR WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER |
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why? | class Solution:
def search(self, nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return True
elif nums[mid] > nums[left]:
if target >= nums[left] and target < nums[mid]:
right = mid
else:
left = mid + 1
elif nums[mid] < nums[left]:
if target > nums[mid] and target <= nums[right]:
left = mid + 1
else:
right = mid
else:
left = left + 1
return False | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER |
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5 | class Solution:
def findMedianSortedArrays(self, nums1, nums2):
nums = sorted(nums1 + nums2)
n = len(nums)
return (
nums[int(n / 2)]
if n % 2 == 1
else (nums[int(n / 2)] + nums[int(n / 2) - 1]) / 2.0
) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER |
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5 | class Solution:
def findMedianSortedArrays(self, nums1, nums2):
nums = nums1 + nums2
nums.sort()
if len(nums) % 2 == 1:
return float(nums[len(nums) // 2])
return (nums[len(nums) // 2 - 1] + nums[len(nums) // 2]) / 2 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER |
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5 | class Solution:
def findMedianSortedArrays(self, nums1, nums2):
nums1 = nums1 + nums2
nums1.sort()
len_nums1 = len(nums1)
if len_nums1 % 2 == 0:
return float(
(nums1[int(len_nums1 / 2) - 1] + nums1[int(len_nums1 / 2)]) / 2
)
else:
return float(nums1[int(len_nums1 / 2)]) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5 | class Solution:
def findMedianSortedArrays(self, nums1, nums2):
nums1.extend(nums2)
nums1.sort()
size = len(nums1)
if size % 2:
return nums1[size // 2]
else:
return (nums1[size // 2] + nums1[size // 2 - 1]) / 2 | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class UnionFind:
def __init__(self, N):
self.par = list(range(N))
self.rank = [0] * N
self.size = [0] * N
def find(self, x):
if self.par[x] != x:
self.par[x] = self.find(self.par[x])
return self.par[x]
def union(self, x, y):
px = self.find(x)
py = self.find(y)
if px == py:
return
if self.rank[px] < self.rank[py]:
self.par[px] = py
self.size[py] += self.size[px]
elif self.rank[px] > self.rank[py]:
self.par[py] = px
self.size[px] += self.size[py]
else:
self.par[py] = px
self.size[px] += self.size[py]
self.rank[px] += 1
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
arr = [0] + arr
N = len(arr)
uf = UnionFind(N)
res = []
seen = set()
matched = set()
for i in range(1, N):
seen.add(arr[i])
matched.add(arr[i])
uf.size[arr[i]] = 1
if arr[i] - 1 >= 0 and arr[i] - 1 in seen:
uf.union(arr[i], arr[i] - 1)
if arr[i] + 1 < N and arr[i] + 1 in seen:
uf.union(arr[i], arr[i] + 1)
for j in list(matched):
idx = uf.find(j)
if uf.size[idx] != m:
matched.remove(j)
if matched:
res.append(i)
return res[-1] if res else -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR NUMBER NUMBER VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class UnionFind:
def __init__(self, values: int):
self.values = list(range(values))
self.connectedComponents = [1] * values
def root(self, key):
root = self.values[key]
while root != self.values[root]:
root = self.values[root]
return root
def union(self, keyA, keyB):
rootA = self.root(keyA)
rootB = self.root(keyB)
self.values[rootB] = rootA
self.connectedComponents[rootA] += self.connectedComponents[rootB]
self.connectedComponents[rootB] = 0
def find(self, keyA, keyB):
return self.root(keyA) == self.root(keyB)
class Solution:
def findLatestStep(self, arr: list, m: int) -> int:
uf = UnionFind(len(arr))
step = -1
groupM = set()
binaryString = [0] * len(arr)
for i, val in enumerate(arr):
if i == 9:
print("")
val = val - 1
binaryString[val] = 1
if val != 0 and binaryString[val - 1] == 1:
if not uf.find(val, val - 1):
root = uf.root(val - 1)
uf.union(val, val - 1)
if root in groupM:
groupM.remove(root)
if val != len(arr) - 1 and binaryString[val + 1] == 1:
if not uf.find(val, val + 1):
root = uf.root(val + 1)
uf.union(val, val + 1)
if root in groupM:
groupM.remove(root)
if uf.connectedComponents[val] == m:
groupM.add(val)
elif uf.connectedComponents[val] != m and val in groupM:
groupM.remove(val)
if len(groupM):
step = i + 1
return step | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class UnionFind:
def __init__(self, n):
self.roots = [i for i in range(n)]
self.sizes = [(1) for i in range(n)]
def root(self, a):
c = a
while self.roots[c] != c:
c = self.roots[c]
self.roots[a] = c
return c
def add(self, a, b):
a = self.root(a)
b = self.root(b)
if self.sizes[a] < self.sizes[b]:
a, b = b, a
self.roots[b] = a
self.sizes[a] += self.sizes[b]
class Solution:
def findLatestStep(self, arr: List[int], M: int) -> int:
uf = UnionFind(len(arr))
m = [(0) for i in range(len(arr))]
good = set()
day = 1
result = -1
for a in arr:
a -= 1
m[a] = 1
if a > 0 and m[a - 1] == 1:
if uf.root(a - 1) in good:
good.remove(uf.root(a - 1))
uf.add(a, a - 1)
if a < len(arr) - 1 and m[a + 1] == 1:
if uf.root(a + 1) in good:
good.remove(uf.root(a + 1))
uf.add(a, a + 1)
if uf.sizes[uf.root(a)] == M:
good.add(uf.root(a))
if good:
result = day
day += 1
return result | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
parent = [i for i in range(n)]
cnt = [0] * n
self.m_cnt = 0
def find(node):
if parent[node] != node:
parent[node] = find(parent[node])
return parent[node]
def union(n1, n2):
old = (cnt[find(n1)] == m) + (cnt[find(n2)] == m)
cnt[find(n2)] += cnt[find(n1)]
parent[find(n1)] = find(n2)
new = cnt[find(n2)] == m
self.m_cnt += new - old
string = [0] * n
ans = -1
for i in range(n):
j = arr[i] - 1
string[j] = 1
cnt[j] = 1
if m == 1:
self.m_cnt += 1
if j > 0 and string[j - 1] == 1:
union(j - 1, j)
if j < n - 1 and string[j + 1] == 1:
union(j, j + 1)
if self.m_cnt > 0:
ans = i + 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
last_step = -1
n = len(arr)
left, right = [0] * n, [0] * n
tmp_arr = [0] * n
memo = collections.defaultdict(lambda: 0)
for i, num in enumerate(arr):
tmp_arr[num - 1] = 1
left[num - 1] = 1 + (left[num - 2] if num >= 2 else 0)
right[num - 1] = 1 + (right[num] if num < n else 0)
if num >= 2 and tmp_arr[num - 2]:
memo[left[num - 2] + right[num - 2] - 1] -= 1
right[num - 2] += right[num - 1]
if num - 1 - (left[num - 1] - 1) != num - 2:
right[num - 1 - (left[num - 1] - 1)] += right[num - 1]
if num < n and tmp_arr[num]:
memo[left[num] + right[num] - 1] -= 1
left[num] += left[num - 1]
if num - 2 + right[num - 1] != num:
left[num - 2 + right[num - 1]] += left[num - 1]
memo[left[num - 1] + right[num - 1] - 1] += 1
if memo[m] > 0:
last_step = i + 1
return last_step | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
count = collections.Counter()
length = collections.defaultdict(int)
ans = -1
for i, a in enumerate(arr):
left, right = length[a - 1], length[a + 1]
length[a] = length[a - left] = length[a + right] = left + right + 1
count[left] -= 1
count[right] -= 1
count[length[a]] += 1
if count[m]:
ans = i + 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
sizes = collections.defaultdict(int)
has = [0] * len(arr)
ends = {}
out = 0
for i, a in enumerate(arr):
has[a - 1] += 1
if a - 1 in ends and a + 1 in ends:
length1 = a - 1 - ends[a - 1] + 1
length2 = ends[a + 1] + 1 - (a + 1)
temp1 = ends[a + 1]
temp2 = ends[a - 1]
ends[temp2] = temp1
ends[temp1] = temp2
sizes[length1] -= 1
sizes[length2] -= 1
sizes[length1 + 1 + length2] += 1
elif a - 1 in ends:
length1 = a - 1 - ends[a - 1] + 1
ends[a] = ends[a - 1]
ends[ends[a - 1]] = a
sizes[length1] -= 1
sizes[length1 + 1] += 1
elif a + 1 in ends:
length1 = ends[a + 1] - (a + 1) + 1
ends[a] = ends[a + 1]
ends[ends[a + 1]] = a
sizes[length1] -= 1
sizes[length1 + 1] += 1
else:
ends[a] = a
sizes[1] += 1
if sizes[m] > 0:
out = i + 1
return out if out else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR NUMBER VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
uf = {}
seen = [0] * (len(arr) + 1)
def find(x):
uf.setdefault(x, x)
if uf[x] != x:
uf[x] = find(uf[x])
return uf[x]
def union(x, y):
seen[find(y)] += seen[find(x)]
uf[find(x)] = find(y)
ans, n = -1, len(arr)
for i, a in enumerate(arr, 1):
seen[a] = 1
for b in [a - 1, a + 1]:
if 1 <= b <= n and seen[b]:
if seen[find(b)] == m:
ans = i - 1
union(a, b)
if m == n:
ans = n
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
uf = UnionFind(n)
res = [0] * n
ans = -1
for i in range(n):
step = i + 1
index = arr[i] - 1
res[index] = 1
uf.size[index] = 1
uf.count[1] += 1
if index - 1 >= 0 and res[index - 1] == 1:
uf.union(index - 1, index)
if index + 1 < n and res[index + 1] == 1:
uf.union(index, index + 1)
if uf.count[m] > 0:
ans = step
return ans
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
self.size = [0] * n
self.count = Counter()
def union(self, x, y):
rootx = self.find(x)
rooty = self.find(y)
if rootx == rooty:
return False
size_x = self.size[rootx]
size_y = self.size[rooty]
self.count[size_x] -= 1
self.count[size_y] -= 1
new_size = size_x + size_y
self.parent[rooty] = rootx
self.size[rootx] = new_size
self.count[new_size] += 1
return True
def find(self, x):
while x != self.parent[x]:
x = self.parent[x]
return x | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
d = {}
step = 1
ans = -1
s = set()
for a in arr:
if a not in d:
left = d.get(a - 1, [])
right = d.get(a + 1, [])
s = 1
if left:
s += left[0]
if left[0] == m:
ans = max(ans, step - 1)
if right:
s += right[0]
if right[0] == m:
ans = max(ans, step - 1)
if s == m:
ans = max(ans, step)
d[a] = [s, step]
if left:
d[a - left[0]] = [s, step]
if right:
d[a + right[0]] = [s, step]
step += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR NUMBER LIST VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR NUMBER LIST VAR VAR VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class DSU:
def __init__(self, m):
self.p = {}
self.islands = {}
self.m = m
self.hasm = 0
def make_island(self, x):
self.p[x] = x
self.islands[x] = 1
if self.m == 1:
self.hasm += 1
def exist(self, x):
if not x in self.p:
return False
return True
def find(self, x):
if x != self.p[x]:
self.p[x] = self.find(self.p[x])
return self.p[x]
def union(self, x, y):
if not self.exist(x) or not self.exist(y):
return
xr = self.find(x)
yr = self.find(y)
self.p[xr] = yr
if self.islands[yr] == self.m:
self.hasm -= 1
if self.islands[xr] == self.m:
self.hasm -= 1
self.islands[yr] = self.islands[xr] + self.islands[yr]
if self.islands[yr] == self.m:
self.hasm += 1
del self.islands[xr]
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
res = -1
dsu = DSU(m)
for i, x in enumerate(arr):
if not dsu.exist(x):
dsu.make_island(x)
dsu.union(x, x + 1)
dsu.union(x, x - 1)
if dsu.hasm != 0:
res = i + 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
def find(x):
while x != par[x]:
par[x] = par[par[x]]
x = par[x]
return x
def union(x, y):
nonlocal count
px, py = find(x), find(y)
if px == py:
return
if size[px] == m:
count -= 1
if size[py] == m:
count -= 1
if size[px] > size[py]:
par[py] = par[px]
size[px] += size[py]
if size[px] == m:
count += 1
else:
par[px] = par[py]
size[py] += size[px]
if size[py] == m:
count += 1
count = 0
n = len(arr) + 2
par = list(range(n))
size = [0] * n
res = -1
for i, el in enumerate(arr, 1):
size[el] = 1
if m == 1:
count += 1
if size[el - 1]:
union(el, el - 1)
if size[el + 1]:
union(el, el + 1)
if count > 0:
res = i
return res | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
length_at_index = [0] * (len(arr) + 2)
count_of_length = [0] * (len(arr) + 1)
res = -1
for i, index in enumerate(arr):
left_length = length_at_index[index - 1]
right_length = length_at_index[index + 1]
new_length = left_length + right_length + 1
length_at_index[index] = new_length
length_at_index[index - left_length] = new_length
length_at_index[index + right_length] = new_length
count_of_length[left_length] -= 1
count_of_length[right_length] -= 1
count_of_length[new_length] += 1
if count_of_length[m] > 0:
res = i + 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n, res = len(arr), -1
if m == n:
return n
if m == 1 and n == 1:
return 1
string, size, root = [0] * n, [1] * n, [-1] * n
def find(a):
if root[a] != a:
root[a] = find(root[a])
return root[a]
def union(a, b):
find_a, find_b = find(a), find(b)
root[find_a] = find_b
size[find_b] += size[find_a]
for step in range(n):
idx = arr[step] - 1
string[idx] = 1
root[idx] = idx
if idx - 1 >= 0 and string[idx - 1] == 1:
if m == size[find(idx - 1)]:
res = step
if idx + 1 < n and string[idx + 1] == 1:
if m == size[find(idx + 1)]:
res = step
if idx - 1 >= 0 and string[idx - 1] == 1:
union(idx - 1, idx)
if idx + 1 < n and string[idx + 1] == 1:
union(idx + 1, idx)
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class UF:
def __init__(self, N):
self.N = N
self.size = [0] * N
self.stat = [False] * N
self.id = list(range(N))
self.sizes = collections.Counter()
def find(self, x):
if self.id[x] != x:
self.id[x] = self.find(self.id[x])
return self.id[x]
def union(self, x, y):
px = self.find(x)
py = self.find(y)
if px != py:
self.size[px] += self.size[py]
self.id[py] = px
self.sizes[self.size[py]] -= 1
def set(self, x):
self.stat[x] = True
self.size[x] += 1
if x - 1 >= 0 and self.stat[x - 1]:
self.union(x, x - 1)
if x + 1 < self.N and self.stat[x + 1]:
self.union(x, x + 1)
self.sizes[self.size[x]] += 1
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
uf = UF(n)
ans = -1
for step, idx in enumerate(arr, 1):
uf.set(idx - 1)
if uf.sizes[m] > 0:
ans = step
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
N = len(arr)
parent = {}
size = {}
ds_sizes = Counter()
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
def decrement_size(x):
ds_sizes[x] -= 1
if ds_sizes[x] == 0:
del ds_sizes[x]
def union(x, y):
px, py = find(x), find(y)
if px is py:
return
if size[px] < size[py]:
px, py = py, px
parent[py] = px
decrement_size(size[px])
decrement_size(size[py])
size[px] += size[py]
ds_sizes[size[px]] += 1
def make_set(x):
if x in parent:
return
parent[x] = x
size[x] = 1
ds_sizes[1] += 1
steps = 0
last_step = -1
for n in arr:
make_set(n)
for neighbor in (n + 1, n - 1):
if neighbor in parent:
union(n, neighbor)
steps += 1
if m in ds_sizes:
last_step = steps
return last_step | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class union(object):
def __init__(self, n):
self.parent = [-1] * n
self.size = [0] * n
self.count = collections.defaultdict(int)
def find(self, p):
if self.parent[p] == -1:
return -1
if self.parent[p] != p:
self.parent[p] = self.find(self.parent[p])
return self.parent[p]
def union(self, p, q):
if self.find(p) != self.find(q):
pr = self.find(p)
qr = self.find(q)
self.count[self.size[pr]] -= 1
self.count[self.size[qr]] -= 1
self.parent[pr] = qr
self.size[qr] += self.size[pr]
self.count[self.size[qr]] += 1
def add(self, p):
self.parent[p] = p
self.size[p] = 1
self.count[1] += 1
def get_size(self, m):
return self.count[m] > 0
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
if m == len(arr):
return m
uds = union(len(arr) + 1)
ans = -1
for i in range(len(arr)):
uds.add(arr[i])
if arr[i] + 1 <= len(arr) and uds.find(arr[i] + 1) != -1:
uds.union(arr[i], arr[i] + 1)
if arr[i] - 1 >= 1 and uds.find(arr[i] - 1) != -1:
uds.union(arr[i] - 1, arr[i])
if uds.get_size(m):
ans = i + 1
return ans | CLASS_DEF VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER FUNC_DEF RETURN VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def find(self, a):
if self.parent[a] != a:
self.parent[a] = self.find(self.parent[a])
return self.parent[a]
def union(self, a, b):
r1 = self.find(a)
r2 = self.find(b)
if r1 == r2:
return
if self.size[r1] == self.m:
self.num_m -= 1
if self.size[r2] == self.m:
self.num_m -= 1
ns = self.size[r1] + self.size[r2]
if ns == self.m:
self.num_m += 1
if self.rank[r1] > self.rank[r2]:
self.parent[r2] = r1
self.size[r1] = ns
elif self.rank[r2] > self.rank[r1]:
self.parent[r1] = r2
self.size[r2] = ns
else:
self.parent[r1] = r2
self.rank[r2] += 1
self.size[r2] = ns
def findLatestStep(self, arr: List[int], m: int) -> int:
self.parent = {}
self.rank = {}
self.size = {}
self.m = m
self.num_m = 0
ans = -1
for i, v in enumerate(arr):
self.parent[v] = v
self.rank[v] = 1
self.size[v] = 1
if self.size[v] == m:
self.num_m += 1
if v - 1 in self.parent:
self.union(v - 1, v)
if v + 1 in self.parent:
self.union(v + 1, v)
if self.num_m > 0:
ans = i + 1
return ans | CLASS_DEF FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
a = [(x - 1) for x in arr]
n = len(a)
parent = list(range(n))
size = [0] * n
count = Counter()
def find(u):
if parent[u] != u:
parent[u] = find(parent[u])
return parent[u]
def union(u, v):
x, y = find(u), find(v)
if x != y:
parent[y] = x
size[x] += size[y]
res = -1
bits = [0] * n
for i, u in enumerate(a, 1):
bits[u] = 1
size[u] = 1
count[1] += 1
if u > 0 and bits[u - 1]:
count[size[find(u - 1)]] -= 1
union(u - 1, u)
if u + 1 < n and bits[u + 1]:
count[size[find(u + 1)]] -= 1
union(u, u + 1)
if size[find(u)] != 1:
count[1] -= 1
count[size[find(u)]] += 1
if count[m] > 0:
res = i
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
mem = {}
gsizes = 0
inc = False
lastpos = -1
changed = False
for i, num in enumerate(arr):
gsize = 1
if num - 1 in mem:
if mem[num - 1][0] == m:
gsizes -= 1
changed = True
gsize += mem[num - 1][0]
if num + 1 in mem:
if mem[num + 1][0] == m:
gsizes -= 1
changed = True
gsize += mem[num + 1][0]
if gsize == m:
inc = True
changed = True
gsizes += 1
if gsizes == 0 and inc and changed:
changed = False
lastpos = i
if num + 1 not in mem and num - 1 not in mem:
end = num
elif num + 1 in mem:
end = mem[num + 1][1]
else:
end = mem[num - 1][1]
mem[num] = gsize, end
if num - 1 in mem:
old = mem[num - 1][1]
mem[mem[num - 1][1]] = gsize, mem[num + 1][1] if num + 1 in mem else num
if num + 1 in mem:
mem[mem[num + 1][1]] = gsize, old if num - 1 in mem else num
return len(arr) if gsizes else lastpos | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
def func(i):
if d[i] != i:
d[i] = func(d[i])
return d[i]
d, dd, a, s = {}, {}, {}, -1
for p, i in enumerate(arr, 1):
if i - 1 in d:
if i + 1 in d:
j, k = func(i - 1), func(i + 1)
d[k], d[i] = j, j
a[dd[j]] -= 1
a[dd[k]] -= 1
dd[j] += dd[k] + 1
a[dd[j]] = a.get(dd[j], 0) + 1
else:
j = func(i - 1)
d[i] = j
a[dd[j]] -= 1
dd[j] += 1
a[dd[j]] = a.get(dd[j], 0) + 1
elif i + 1 in d:
j = func(i + 1)
d[i] = j
a[dd[j]] -= 1
dd[j] += 1
a[dd[j]] = a.get(dd[j], 0) + 1
else:
d[i] = i
dd[i] = 1
a[1] = a.get(1, 0) + 1
if a.get(m, 0):
s = p
return s | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR VAR DICT DICT DICT NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
if n == m:
return n
arr.insert(0, 0)
day = [0] * (n + 1)
for i in range(1, n + 1):
day[arr[i]] = i
ans = -1
max_q = MaxQueue(m)
for i in range(1, n + 1):
max_q.pop_expired(i)
max_q.push(day[i], i)
if i < m:
continue
left = right = math.inf
if i - m >= 1:
left = day[i - m]
if i + 1 <= n:
right = day[i + 1]
if max_q.max() < (d := min(left, right)):
ans = max(ans, d - 1)
return ans
class MaxQueue:
def __init__(self, size):
self.queue = deque()
self.size = size
def push(self, x, pos):
while self.queue and self.queue[-1][0] < x:
self.queue.pop()
self.queue.append([x, pos])
def pop_expired(self, pos):
if self.queue and pos - self.queue[0][1] >= self.size:
self.queue.popleft()
def max(self):
return self.queue[0][0] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR FUNC_DEF IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_DEF RETURN VAR NUMBER NUMBER |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
if m == len(arr):
return m
ends, starts = {}, {}
last_found = -1
for ind, val in enumerate(arr):
left = val - 1 if val - 1 in ends else None
right = val + 1 if val + 1 in starts else None
if left and right:
length1, length2 = left - ends[left], starts[right] - right
if length1 == m - 1 or length2 == m - 1:
last_found = ind
l, r = ends[left], starts[right]
starts[l], ends[r] = r, l
del ends[left]
del starts[right]
elif left:
length = left - ends[left]
if length == m - 1:
last_found = ind
ends[val] = ends[left]
starts[ends[left]] = val
del ends[left]
elif right:
length = starts[right] - right
if length == m - 1:
last_found = ind
starts[val] = starts[right]
ends[starts[right]] = val
del starts[right]
else:
starts[val] = val
ends[val] = val
return last_found | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR DICT DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NONE ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NONE IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
res, n = -1, len(arr)
length = [0] * (n + 2)
count = [n] + [0] * n
for i, v in enumerate(arr):
left, right = length[v - 1], length[v + 1]
length[v] = length[v - left] = length[v + right] = left + right + 1
count[left] -= 1
count[right] -= 1
count[length[v]] += 1
if count[m]:
res = i + 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
pos = [None] * (len(arr) + 3)
num = 0
ans = -1
for idx in range(1, len(arr) + 1):
l, r, p = [arr[idx - 1]] * 3
if pos[p - 1]:
if pos[p - 1][1] - pos[p - 1][0] + 1 == m:
num -= 1
l = pos[p - 1][0]
if pos[p + 1]:
if pos[p + 1][1] - pos[p + 1][0] + 1 == m:
num -= 1
r = pos[p + 1][1]
pos[l] = pos[r] = l, r
if r - l + 1 == m:
num += 1
if num != 0:
ans = idx
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP LIST VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
class DSU:
def __init__(self, n, m):
self.parent = [i for i in range(n)]
self.size = [0] * n
self.m = m
self.cntm = 0
def add(self, x):
self.size[x] = 1
if self.m == 1:
self.cntm += 1
self.unite(x - 1, x)
self.unite(x, x + 1)
def find(self, x):
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def unite(self, x, y):
if (
x < 0
or self.size[x] == 0
or y == len(self.size)
or self.size[y] == 0
):
return
px, py = self.find(x), self.find(y)
self.cntm -= self.size[px] == self.m
self.cntm -= self.size[py] == self.m
if self.size[px] < self.size[py]:
px, py = py, px
self.size[px] += self.size[py]
self.parent[py] = px
self.cntm += self.size[px] == self.m
n = len(arr)
dsu = DSU(n, m)
latest = -1
for i in range(n):
dsu.add(arr[i] - 1)
if dsu.cntm:
latest = i + 1
return latest | CLASS_DEF FUNC_DEF VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
def root(x):
if x == group[x]:
return x
group[x] = root(group[x])
return group[x]
def same(x, y):
return root(x) == root(y)
def unite(x, y):
nonlocal cnt
x = root(x)
y = root(y)
cnt -= sz[x] == m
if sz[x] < sz[y]:
x, y = y, x
group[y] = x
sz[x] += sz[y]
group = [(-1) for i in range(len(arr) + 1)]
sz = [(0) for i in range(len(arr) + 1)]
ones = [(False) for i in range(len(arr) + 1)]
cnt = 0
latest = -1
for i in range(len(arr)):
index = arr[i]
ones[index] = True
sz[index] = 1
group[index] = arr[i]
if index - 1 >= 1 and ones[index - 1]:
unite(index - 1, index)
if index + 1 <= len(arr) and ones[index + 1]:
unite(index + 1, index)
if sz[root(index)] == m:
cnt += 1
if cnt > 0:
latest = i + 1
return latest | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class DSU:
def __init__(self, count):
self.parent = [i for i in range(count)]
self.size = [(1) for _ in range(count)]
def find(self, x):
root = x
while root != self.parent[root]:
root = self.parent[root]
while x != root:
next_node = self.parent[x]
self.parent[x] = root
x = next_node
return root
def union(self, x, y):
r1, r2 = self.find(x), self.find(y)
if r1 == r2:
return
if self.size[r1] < self.size[r2]:
self.size[r2] += self.size[r1]
self.parent[r1] = r2
else:
self.size[r1] += self.size[r2]
self.parent[r2] = r1
def get_size(self, x):
return self.size[self.find(x)]
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
components = [(0) for _ in range(len(arr))]
size_count = collections.Counter()
dsu = DSU(len(arr))
ans = -1
for i, num in enumerate(arr, 1):
num -= 1
components[num] = 1
for adj in (num - 1, num + 1):
if 0 <= adj < len(arr) and components[adj]:
size_count[dsu.get_size(adj)] -= 1
dsu.union(num, adj)
size_count[dsu.get_size(num)] += 1
if size_count[m] > 0:
ans = i
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF RETURN VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
if m == len(arr):
return m
border = [0] * (len(arr) + 2)
ans = -1
for i in range(len(arr)):
left = right = arr[i]
if border[right + 1] > 0:
right = border[right + 1]
if border[left - 1] > 0:
left = border[left - 1]
border[left], border[right] = right, left
if right - arr[i] == m or arr[i] - left == m:
ans = i
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
bits = [0] * (len(arr) + 2)
answ = -1
num_groups = 0
for step, idx in enumerate(arr):
before = bits[idx - 1]
after = bits[idx + 1]
group = 1
if before + after == 0:
bits[idx] = 1
elif before == 0:
bits[idx] = after + 1
bits[idx + 1] = 0
bits[idx + after] = after + 1
group = after + 1
elif after == 0:
bits[idx] = before + 1
bits[idx - 1] = 0
bits[idx - before] = before + 1
group = before + 1
else:
bits[idx - 1], bits[idx + 1] = 0, 0
bits[idx - before], bits[idx + after] = (
before + after + 1,
before + after + 1,
)
group = before + after + 1
if group == m:
num_groups += 1
if before == m:
num_groups -= 1
if after == m:
num_groups -= 1
if num_groups > 0:
answ = step + 1
return answ | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
a, b, s, t = [0] * (len(arr) + 2), [0] * (len(arr) + 1), -1, 0
for p, i in enumerate(arr, 1):
j, k = a[i - 1], a[i + 1]
a[i] = a[i - j] = a[i + k] = j + k + 1
if a[i] == m:
t += 1
if j == m:
t -= 1
if k == m:
t -= 1
if t:
s = p
return s | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class BinaryIndexedTree:
def __init__(self, n):
self.n = n
self.data = [0] * (n + 1)
for i in range(1, n + 1):
self.data[i] += 1
tmp = i + (i & -i)
if tmp <= n:
self.data[i + (i & -i)] += self.data[i]
def add(self, index, value):
while index <= self.n:
self.data[index] += value
index += index & -index
def prefix(self, index):
res = 0
while index:
res += self.data[index]
index -= index & -index
return res
class Solution:
def findLatestStep(self, arr: List[int], k: int) -> int:
n = len(arr)
if k == n:
return n
bit = BinaryIndexedTree(n)
for no, p in enumerate(arr[::-1]):
bit.add(p, -1)
if p - k >= 1:
s1 = bit.prefix(p - 1)
s2 = bit.prefix(p - k - 1)
if s1 - s2 == k and (p - k - 1 == 0 or bit.prefix(p - k - 2) == s2):
return n - no - 1
if p + k <= n:
s1 = bit.prefix(p)
s2 = bit.prefix(p + k)
if s2 - s1 == k and (p + k == n or bit.prefix(p + k + 1) == s2):
print("b", p, s1, s2)
return n - no - 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
ans = -1
da = {}
db = {}
n = len(arr)
s = [(0) for i in range(len(arr))]
cnts = 0
step = 0
for e in arr:
step += 1
x = e - 1
s[x] = 1
st = x
ed = x
da[x] = x, x
db[x] = x, x
if x > 0 and s[x - 1] == 1:
p = db[x - 1]
if p[1] + 1 - p[0] == m:
cnts -= 1
st = p[0]
da[st] = st, ed
del db[p[1]]
db[ed] = st, ed
del da[x]
if x < n - 1 and s[x + 1] == 1:
q = da[x + 1]
if q[1] + 1 - q[0] == m:
cnts -= 1
ed = q[1]
da[st] = st, ed
del da[q[0]]
db[ed] = st, ed
del db[x]
if ed + 1 - st == m:
cnts += 1
if cnts > 0:
ans = step
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Node:
def __init__(self):
self.left = None
self.right = None
self.length = 0
class Solution:
def findLatestStep(self, arr, m):
def remove(node):
left = node.left
node.right.left = left
left.right = node.right
count = 0
node = {}
M = max(arr)
for i in range(M + 2):
n = Node()
node[i] = n
if i == 0:
continue
node[i - 1].right = n
n.left = node[i - 1]
ans = -1
for step, i in enumerate(arr):
node[i].length = 1
if node[i].left.length > 0:
node[i].length += node[i].left.length
if node[i].left.length == m:
count -= 1
remove(node[i].left)
if node[i].right.length > 0:
if node[i].right.length == m:
count -= 1
node[i].length += node[i].right.length
remove(node[i].right)
if node[i].length == m:
count += 1
if count > 0:
ans = step + 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
if m == n:
print(n)
def dfs(start: int, end: int, step: int, target: int):
if end > len(arr) or end < 1:
return -1
if start > len(arr) or start < 1:
return -1
if end < start:
return -1
if end - start + 1 < target:
return -1
if end - start + 1 == target:
return step
bp = arr[step - 1]
res = -1
if start <= bp <= end:
res = max(
dfs(start, bp - 1, step - 1, target),
dfs(bp + 1, end, step - 1, target),
)
else:
res = max(res, dfs(start, end, step - 1, target))
return res
return dfs(1, n, n, m) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR VAR VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
counter = [0] * (n + 1)
parent = [i for i in range(n + 1)]
size = [(0) for i in range(n + 1)]
def find(u):
if parent[u] == u:
return u
parent[u] = find(parent[u])
return parent[u]
def union(u, v):
ru, rv = find(u), find(v)
if size[ru] < size[rv]:
parent[ru] = rv
size[rv] += size[ru]
else:
parent[rv] = ru
size[ru] += size[rv]
def getSize(p):
return size[find(p)]
res = -1
for i, pos in enumerate(arr):
size[pos] = 1
if pos > 0:
if getSize(pos - 1) > 0:
s_last = getSize(pos - 1)
union(pos - 1, pos)
counter[s_last] -= 1
if pos < n:
if getSize(pos + 1) > 0:
s_next = getSize(pos + 1)
union(pos, pos + 1)
counter[s_next] -= 1
counter[getSize(pos)] += 1
if counter[m] > 0:
res = i + 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
if m == len(arr):
return len(arr)
u = list(range(len(arr) + 1))
size = [0] * (len(arr) + 1)
def find(a):
if u[a] != a:
u[a] = find(u[a])
return u[a]
def union(a, b):
u1 = find(a)
u2 = find(b)
if u1 != u2:
if size[u1] < size[u2]:
u[u1] = u2
size[u2] += size[u1]
else:
u[u2] = u1
size[u1] += size[u2]
ans = -1
for a_idx, a in enumerate(arr):
size[a] = 1
for i in [a - 1, a + 1]:
if 1 <= i <= len(arr):
if size[find(i)] == m:
ans = a_idx
if size[i] > 0:
union(a, i)
for i in range(1, len(arr) + 1):
if size[find(i)] == m:
return len(arr)
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
mask = [0] * (n + 2)
ans = -1
i = 1
count = 0
for index in arr:
total_len = 1 + mask[index - 1] + mask[index + 1]
change_set = {index + mask[index + 1], index - mask[index - 1]}
for ind in change_set:
if mask[ind] == m:
count -= 1
mask[index - mask[index - 1]] = total_len
mask[index + mask[index + 1]] = total_len
if total_len == m:
count += 1
if count > 0:
ans = i
i += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
left = [0] * (n + 2)
right = [0] * (n + 2)
def getlength(x):
return right[x] - x + 1
count = 0
res = -1
for i in range(n):
x = arr[i]
if left[x - 1] and right[x + 1]:
if getlength(left[x - 1]) == m:
count -= 1
if getlength(x + 1) == m:
count -= 1
right[left[x - 1]] = right[x + 1]
left[right[x + 1]] = left[x - 1]
if getlength(left[x - 1]) == m:
count += 1
elif left[x - 1]:
if getlength(left[x - 1]) == m:
count -= 1
right[left[x - 1]] = x
left[x] = left[x - 1]
if getlength(left[x - 1]) == m:
count += 1
elif right[x + 1]:
if getlength(x + 1) == m:
count -= 1
left[right[x + 1]] = x
right[x] = right[x + 1]
if getlength(x) == m:
count += 1
else:
left[x] = x
right[x] = x
if m == 1:
count += 1
if count > 0:
res = i + 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | import sys
input = sys.stdin.readline
class Unionfind:
def __init__(self, n):
self.par = [-1] * n
self.rank = [1] * n
def root(self, x):
r = x
while not self.par[r] < 0:
r = self.par[r]
t = x
while t != r:
tmp = t
t = self.par[t]
self.par[tmp] = r
return r
def unite(self, x, y):
rx = self.root(x)
ry = self.root(y)
if rx == ry:
return
if self.rank[rx] <= self.rank[ry]:
self.par[ry] += self.par[rx]
self.par[rx] = ry
if self.rank[rx] == self.rank[ry]:
self.rank[ry] += 1
else:
self.par[rx] += self.par[ry]
self.par[ry] = rx
def is_same(self, x, y):
return self.root(x) == self.root(y)
def count(self, x):
return -self.par[self.root(x)]
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
arr = list(map(lambda x: x - 1, arr))
n = len(arr)
now = [0] * n
ans = -1
uf = Unionfind(n)
cnt = [0] * (n + 1)
for i in range(n):
p = arr[i]
now[p] = 1
cnt[1] += 1
if p - 1 >= 0 and now[p - 1] == 1:
cnt[uf.count(p)] -= 1
cnt[uf.count(p - 1)] -= 1
uf.unite(p - 1, p)
cnt[uf.count(p)] += 1
if p + 1 < n and now[p + 1] == 1:
cnt[uf.count(p)] -= 1
cnt[uf.count(p + 1)] -= 1
uf.unite(p, p + 1)
cnt[uf.count(p)] += 1
if cnt[m] > 0:
ans = i + 1
return ans | IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
res = -1
e2s = {}
s2e = {}
cnt = {}
for i, v in enumerate(arr):
if v - 1 not in e2s and v + 1 not in s2e:
l, r = v, v
elif v - 1 not in e2s and v + 1 in s2e:
l, r = v, s2e[v + 1]
del s2e[v + 1]
cnt[r - v] -= 1
if not cnt[r - v]:
del cnt[r - v]
elif v - 1 in e2s and v + 1 not in s2e:
l, r = e2s[v - 1], v
del e2s[v - 1]
cnt[v - l] -= 1
if not cnt[v - l]:
del cnt[v - l]
else:
l, r = e2s[v - 1], s2e[v + 1]
del e2s[v - 1]
del s2e[v + 1]
cnt[v - l] -= 1
if not cnt[v - l]:
del cnt[v - l]
cnt[r - v] -= 1
if not cnt[r - v]:
del cnt[r - v]
s2e[l] = r
e2s[r] = l
cnt[r - l + 1] = cnt.get(r - l + 1, 0) + 1
if m in cnt:
res = i + 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
def find_set(x):
if parents[x][0] == x:
return x
else:
return find_set(parents[x][0])
def union_set(x, y):
x_root = find_set(x)
y_root = find_set(y)
parents[y_root][1] += parents[x_root][1]
parents[x_root][0] = y_root
n = len(arr)
parents = [[i, 1] for i in range(n)]
visited = [(False) for i in range(n)]
answer = -1
d = {}
for i in range(n):
num = arr[i] - 1
visited[num] = True
if num > 0 and visited[num - 1]:
d[parents[find_set(num - 1)][1]] -= 1
union_set(num - 1, num)
if num + 1 < n and visited[num + 1]:
d[parents[find_set(num + 1)][1]] -= 1
union_set(num + 1, num)
d[parents[num][1]] = (
1 if parents[num][1] not in d else d[parents[num][1]] + 1
)
if m in d and d[m] > 0:
answer = i + 1
return answer | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
if n == m:
return n
def helper(rec, k):
temp_rec = []
for i, j in rec:
if k < i or k > j:
if j - i + 1 > m:
temp_rec.append([i, j])
continue
if k - i == m or j - k == m:
return True
else:
if k - i > m:
temp_rec.append([i, k - 1])
if j - k > m:
temp_rec.append([k + 1, j])
return temp_rec
rec = [(1, n)]
for ind in range(n, 0, -1):
rec = helper(rec, arr[ind - 1])
if rec == True:
return ind - 1
elif not rec:
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR RETURN NUMBER VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Subset:
def __init__(self, n):
self.parent = [i for i in range(n)]
self.rank = [0] * n
def find(self, i):
if self.parent[i] != i:
self.parent[i] = self.find(self.parent[i])
return self.parent[i]
def union(self, u, v):
if self.rank[u] > self.rank[v]:
self.parent[v] = self.find(u)
if self.rank[v] > self.rank[u]:
self.parent[u] = self.find(v)
if self.rank[u] == self.rank[v]:
self.parent[v] = self.find(u)
self.rank[u] += self.rank[v]
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
subset = Subset(n)
ls = [0] * n
size = [1] * n
res = -1
cnt = 0
for i in range(n):
idx = arr[i] - 1
ls[idx] = 1
sizeMiddle = 1
if idx > 0:
if ls[idx - 1] == 1:
p = subset.find(idx - 1)
sizeLeft = size[p]
subset.union(idx, p)
if sizeLeft == m:
cnt -= 1
sizeMiddle += sizeLeft
if idx < n - 1:
if ls[idx + 1] == 1:
p2 = subset.find(idx + 1)
sizeRight = size[p2]
subset.union(idx, p2)
if sizeRight == m:
cnt -= 1
sizeMiddle += sizeRight
finalP = subset.find(idx)
size[finalP] = sizeMiddle
if sizeMiddle == m:
cnt += 1
if cnt > 0:
res = max(res, i + 1)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
if m == n:
return m
res = -1
length = [(0) for _ in range(n + 2)]
for i, val in enumerate(arr):
left, right = length[val - 1], length[val + 1]
if left == m or right == m:
res = i
length[val - left] = length[val + right] = left + right + 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class UnionFind:
def __init__(self):
self.parent = dict()
self.rank = dict()
self.size = dict()
self.sizes = collections.defaultdict(int)
return
def find(self, x):
if x not in self.parent:
self.parent[x] = x
self.rank[x] = 0
self.size[x] = 1
self.sizes[1] += 1
root = x
while self.parent[root] != root:
root = self.parent[root]
while self.parent[x] != root:
parent = self.parent[x]
self.parent[x] = root
x = parent
return root
def union(self, x, y):
x_root = self.find(x)
y_root = self.find(y)
if x_root == y_root:
return
if self.size[x_root] < self.size[y_root]:
x_root, y_root = y_root, x_root
y_size = self.size[y_root]
x_size = self.size[x_root]
self.sizes[x_size] -= 1
self.sizes[y_size] -= 1
self.parent[y_root] = x_root
self.size[x_root] += self.size[y_root]
self.sizes[self.size[x_root]] += 1
return
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
binary_string = [0] * n
uf = UnionFind()
max_step = -1
step = 1
for i in arr:
index = i - 1
binary_string[index] = 1
root = uf.find(i)
if index - 1 >= 0 and binary_string[index - 1] == 1:
uf.union(root, i - 1)
if index + 1 < len(binary_string) and binary_string[index + 1] == 1:
uf.union(root, i + 1)
if uf.sizes[m] > 0:
max_step = step
step += 1
return max_step | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, A, m):
length = [0] * (len(A) + 2)
count = [0] * (len(A) + 1)
res = -1
for i, a in enumerate(A):
left, right = length[a - 1], length[a + 1]
length[a] = length[a - left] = length[a + right] = left + right + 1
count[left] -= 1
count[right] -= 1
count[length[a]] += 1
if count[m]:
res = i + 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
res = -1
if n == m:
return n
set_range = [0] * (n + 2)
for i in range(n):
set_bit = arr[i]
left_range = set_range[set_bit - 1]
right_range = set_range[set_bit + 1]
set_range[set_bit] = left_range + right_range + 1
set_range[set_bit - left_range] = set_range[set_bit + right_range] = (
set_range[set_bit]
)
if left_range == m or right_range == m:
res = i
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
if m == 1 and len(arr) == 1:
return 1
if m > len(arr):
return -1
Bins = [0] * len(arr)
Gs = {}
Ge = {}
Res = -1
Rescnt = 0
for y in range(len(arr)):
i = arr[y] - 1
if (i == 0 or Bins[i - 1] == 0) and (i == len(arr) - 1 or Bins[i + 1] == 0):
Gs.update({i: 1})
Ge.update({i: i})
Bins[i] = 1
if m == 1:
Res = y + 1
Rescnt = Rescnt + 1
elif (i == 0 or Bins[i - 1] == 0) and (
i < len(arr) - 1 and Bins[i + 1] == 1
):
Gs.update({i: Gs[i + 1] + 1})
Ge[Gs[i + 1] + i] = i
tmp = Gs.pop(i + 1)
Bins[i] = 1
if Gs[i] == m:
Res = y + 1
Rescnt = Rescnt + 1
elif tmp == m:
Rescnt -= 1
elif (i > 0 and Bins[i - 1] == 1) and (
i == len(arr) - 1 or Bins[i + 1] == 0
):
ix = Ge[i - 1]
tmp = Gs[ix]
Gs[ix] = Gs[ix] + 1
tmpe = Ge.pop(i - 1)
Ge.update({i: ix})
Bins[i] = 1
if Gs[ix] == m:
Res = y + 1
Rescnt = Rescnt + 1
elif tmp == m:
Rescnt -= 1
elif (i > 0 and Bins[i - 1] == 1) and (
i < len(arr) - 1 and Bins[i + 1] == 1
):
ix = Ge[i - 1]
tmp0 = Gs[ix]
Gs[ix] = Gs[ix] + Gs[i + 1] + 1
tmp = Gs.pop(i + 1)
tmpe = Ge.pop(i - 1)
Ge[tmp + i] = ix
Bins[i] = 1
if Gs[ix] == m:
Res = y + 1
Rescnt = Rescnt + 1
else:
if tmp == m:
Rescnt -= 1
if tmp0 == m:
Rescnt -= 1
if Rescnt > 0:
Res = y + 1
return Res | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR DICT VAR NUMBER EXPR FUNC_CALL VAR DICT VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR DICT VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR DICT VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
dp = [0] * (len(arr) + 2)
g = collections.Counter()
ans = -1
for i in range(len(arr)):
l, r = dp[arr[i] - 1], dp[arr[i] + 1]
dp[arr[i]] = l + r + 1
dp[arr[i] - l] = dp[arr[i] + r] = l + r + 1
g[l] -= 1
g[r] -= 1
g[dp[arr[i]]] += 1
if g[m] > 0:
ans = i + 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
bit_information = [0] * (len(arr) + 1)
target_group_size_counter = 0
ret = -2
for i in range(len(arr)):
group_sizes = []
total_length = None
if (
arr[i] > 1
and bit_information[arr[i] - 1] != 0
and arr[i] < len(arr)
and bit_information[arr[i] + 1] != 0
):
group_sizes = [bit_information[arr[i] - 1], bit_information[arr[i] + 1]]
total_length = (
1 + bit_information[arr[i] - 1] + bit_information[arr[i] + 1]
)
bit_information[arr[i] - bit_information[arr[i] - 1]] = total_length
bit_information[arr[i] + bit_information[arr[i] + 1]] = total_length
elif arr[i] > 1 and bit_information[arr[i] - 1] != 0:
group_sizes = [bit_information[arr[i] - 1]]
total_length = bit_information[arr[i] - 1] + 1
bit_information[arr[i] - bit_information[arr[i] - 1]] = total_length
bit_information[arr[i]] = total_length
elif arr[i] < len(arr) and bit_information[arr[i] + 1] != 0:
group_sizes = [bit_information[arr[i] + 1]]
total_length = bit_information[arr[i] + 1] + 1
bit_information[arr[i] + bit_information[arr[i] + 1]] = total_length
bit_information[arr[i]] = total_length
else:
bit_information[arr[i]] = 1
total_length = 1
target_group_size_counter -= group_sizes.count(m)
target_group_size_counter += 1 if total_length == m else 0
if target_group_size_counter > 0:
ret = i
return ret + 1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NONE IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
arr.append(n + 1)
start = defaultdict(dict)
finish = defaultdict(dict)
last = -1
for level, i in enumerate(arr):
if i - 1 not in finish:
finish[i - 1] = i
if i + 1 not in start:
start[i + 1] = i
s, f = finish[i - 1], start[i + 1]
start[s] = f
finish[f] = s
for os, of in [[i + 1, start[i + 1]], [finish[i - 1], i - 1]]:
if of - os + 1 == m:
last = level
return last | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR LIST LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
def dfs(start, end, cur):
if end - start + 1 < m:
return -2
if end - start + 1 == m:
return cur
while arr[cur] < start or arr[cur] > end:
cur -= 1
return max(
dfs(start, arr[cur] - 1, cur - 1), dfs(arr[cur] + 1, end, cur - 1)
)
return dfs(1, len(arr), len(arr) - 1) + 1 | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
length = len(arr)
if length == m:
return m
if length < m:
return -1
count_m = 0
array2 = [(0) for _ in range(length + 2)]
result = -1
for i in range(length):
index = arr[i]
if array2[index - 1] == m:
count_m -= 1
if array2[index + 1] == m:
count_m -= 1
array2[index] = array2[index - 1] + array2[index + 1] + 1
if array2[index - 1] > 0:
array2[index - array2[index - 1]] = array2[index]
if array2[index + 1] > 0:
array2[index + array2[index + 1]] = array2[index]
if array2[index] == m:
count_m += 1
if count_m > 0:
result = i + 1
return result | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class DSU:
def __init__(self, n):
self.n = n
self.fa = list(range(n))
self.sz = [(1) for _ in range(n)]
def find(self, x):
r = x
while self.fa[r] != r:
r = self.fa[r]
i = x
while i != r:
i, self.fa[i] = self.fa[i], r
return r
def join(self, x, y):
x = self.find(x)
y = self.find(y)
if x != y:
self.fa[x] = y
self.sz[y] += self.sz[x]
def size(self, x):
x = self.find(x)
return self.sz[x]
class Solution:
def findLatestStep(self, a: List[int], m: int) -> int:
n = len(a)
b = [(0) for _ in range(n)]
dsu = DSU(n)
ans = -1
valid = set()
for k, i in enumerate(a, 1):
j = i - 1
b[j] = 1
if j > 0 and b[j - 1]:
dsu.join(j, j - 1)
if j + 1 < n and b[j + 1]:
dsu.join(j, j + 1)
if m == dsu.size(j):
valid.add(j)
valid = set(p for p in valid if m == dsu.size(p))
if valid:
ans = k
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class DSU:
def __init__(self):
self.p = {}
self.r = {}
self.count = collections.Counter()
def add(self, x: int):
self.p[x] = x
self.r[x] = 1
self.count[1] += 1
def parent(self, x: int) -> int:
if self.p[x] != x:
self.p[x] = self.parent(self.p[x])
return self.p[x]
def unite(self, x: int, y: int) -> int:
x = self.parent(x)
y = self.parent(y)
if x == y:
return self.r[x]
if self.r[x] > self.r[y]:
x, y = y, x
self.count[self.r[x]] -= 1
self.count[self.r[y]] -= 1
self.count[self.r[x] + self.r[y]] += 1
self.p[x] = y
self.r[y] += self.r[x]
return self.r[y]
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
dsu = DSU()
bits = [0] * (n + 2)
ans = -1
for i, bit in enumerate(arr, 1):
dsu.add(bit)
bits[bit] = 1
if bits[bit - 1] == 1:
dsu.unite(bit, bit - 1)
if bits[bit + 1] == 1:
dsu.unite(bit, bit + 1)
if dsu.count[m] > 0:
ans = i
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER FUNC_DEF VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
uf = list(range(len(arr)))
sz = [0] * len(arr)
steps = set()
def find(x):
while x != uf[x]:
x = uf[x]
return x
def union(p, q):
pid, qid = find(p), find(q)
if pid == qid:
return
if sz[pid] == m and pid in steps:
steps.remove(pid)
if sz[qid] == m and qid in steps:
steps.remove(qid)
if sz[pid] < sz[qid]:
uf[pid] = qid
sz[qid] += sz[pid]
else:
uf[qid] = pid
sz[pid] += sz[qid]
last_step = -1
for i in range(len(arr)):
idx = arr[i] - 1
sz[idx] = 1
if idx - 1 >= 0 and sz[idx - 1]:
union(idx - 1, idx)
if idx + 1 < len(arr) and sz[idx + 1]:
union(idx + 1, idx)
if sz[find(idx)] == m:
steps.add(find(idx))
if steps:
last_step = i + 1
return last_step | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
class UnionFind:
def __init__(self, n, m):
self.n = n
self.m = m
self.rank = [0] * n
self.parent = [n for i in range(n)]
self.counts = [0] * n
self.counts_num = [0] * 100005
def set(self, idx):
self.rank[idx] = 1
self.parent[idx] = idx
self.counts[idx] = 1
self.counts_num[self.counts[idx]] += 1
if self.find(idx - 1) != self.n:
self.unite(idx, idx - 1)
if self.find(idx + 1) != self.n:
self.unite(idx, idx + 1)
def find(self, idx):
if idx == self.n or self.parent[idx] == idx:
return idx
self.parent[idx] = self.find(self.parent[idx])
return self.parent[idx]
def unite(self, idx, idx2):
if idx < 0 or idx2 < 0 or idx >= self.n or idx2 >= self.n:
return
root = self.find(idx)
root2 = self.find(idx2)
if root == root2:
return
self.counts_num[self.counts[root]] -= 1
self.counts_num[self.counts[root2]] -= 1
if self.rank[root] > self.rank[root2]:
self.parent[root2] = root
self.rank[root] += 1
self.counts[root] += self.counts[root2]
self.counts[root2] = 0
else:
self.parent[root] = root2
self.rank[root2] += 1
self.counts[root2] += self.counts[root]
self.counts[root] = 0
self.counts_num[self.counts[root]] += 1
self.counts_num[self.counts[root2]] += 1
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
uf = self.UnionFind(n, m)
ans = -2
for i, num in enumerate(arr):
uf.set(num - 1)
if uf.counts_num[m] > 0:
ans = max(ans, i)
return ans + 1 | CLASS_DEF CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class DSU:
def __init__(self, m):
self.reps = {}
self.size = {}
self.m = m
self.count = 0
def add(self, x):
self.reps[x] = x
self.size[x] = 1
if self.m == 1:
self.count += 1
def find(self, x):
if not x == self.reps[x]:
self.reps[x] = self.find(self.reps[x])
return self.reps[x]
def union(self, x, y):
hX = self.find(x)
hY = self.find(y)
if not hX == hY:
h = min(hX, hY)
if self.size[hX] == self.m:
self.count -= 1
if self.size[hY] == self.m:
self.count -= 1
if h == hX:
self.reps[hY] = h
self.size[hX] += self.size[hY]
if self.size[hX] == self.m:
self.count += 1
self.size.pop(hY)
else:
self.reps[hX] = h
self.size[hY] += self.size[hX]
if self.size[hY] == self.m:
self.count += 1
self.size.pop(hX)
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
dsu, s = DSU(m=m), -1
for i, x in enumerate(arr):
dsu.add(x)
if x - 1 in dsu.reps:
dsu.union(x - 1, x)
if x + 1 in dsu.reps:
dsu.union(x + 1, x)
if dsu.count > 0:
s = max(s, i + 1)
return s | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
memo = [[1, len(arr)]]
if m == len(arr):
return len(arr)
for j in range(len(arr) - 1, -1, -1):
i = arr[j]
left, right = 0, len(memo) - 1
while left <= right:
mid = (left + right) // 2
if memo[mid][0] <= i:
left = mid + 1
else:
right = mid - 1
a, b = memo[right][0], memo[right][1]
if i - a == m or a + b - i - 1 == m:
return j
flag = True
if i - a > 0:
memo[right][1] = i - a
else:
memo.pop(right)
flag = False
if a + b - i - 1 > 0:
memo[right + flag : right + flag] = [[i + 1, a + b - i - 1]]
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST LIST NUMBER FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR LIST LIST BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN NUMBER VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class UnionFindSet:
def __init__(self, n):
self.parents = [i for i in range(n)]
self.ranks = [0] * n
def find(self, x):
if x != self.parents[x]:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
px, py = self.find(x), self.find(y)
if px == py:
return False
if self.ranks[px] > self.ranks[py]:
self.parents[py] = px
self.ranks[px] += self.ranks[py]
elif self.ranks[px] < self.ranks[py]:
self.parents[px] = py
self.ranks[py] += self.ranks[px]
else:
self.parents[py] = px
self.ranks[px] += self.ranks[py]
return True
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
if m == len(arr):
return m
uf, ans = UnionFindSet(len(arr)), -1
for step, idx in enumerate(arr):
idx -= 1
uf.ranks[idx] = 1
for j in [idx - 1, idx + 1]:
if 0 <= j < len(arr):
if uf.ranks[uf.find(j)] == m:
ans = step
if uf.ranks[j]:
uf.union(idx, j)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
start_dic, end_dic = {}, {}
m_len_interval = []
latest = -1
for step, position in enumerate(arr):
if position - 1 not in end_dic and position + 1 not in start_dic:
start_dic[position] = 1
end_dic[position] = 1
if m == 1:
m_len_interval.append((position, position))
if position - 1 in end_dic and position + 1 not in start_dic:
length = end_dic[position - 1]
old_start_index = position - 1 - length + 1
old_end_index = position - 1
if length == m:
m_len_interval.remove((old_start_index, old_end_index))
new_start_index = old_start_index
new_end_index = position
start_dic[new_start_index] = length + 1
del end_dic[old_end_index]
end_dic[new_end_index] = length + 1
if length + 1 == m:
m_len_interval.append((new_start_index, new_end_index))
if position - 1 not in end_dic and position + 1 in start_dic:
length = start_dic[position + 1]
old_start_index = position + 1
old_end_index = old_start_index + length - 1
if length == m:
m_len_interval.remove((old_start_index, old_end_index))
new_start_index = position
new_end_index = old_end_index
del start_dic[old_start_index]
start_dic[new_start_index] = length + 1
end_dic[new_end_index] = length + 1
if length + 1 == m:
m_len_interval.append((new_start_index, new_end_index))
if position - 1 in end_dic and position + 1 in start_dic:
old_len_1 = end_dic[position - 1]
old_start_index_1 = position - 1 - old_len_1 + 1
old_end_index_1 = position - 1
if old_len_1 == m:
m_len_interval.remove((old_start_index_1, old_end_index_1))
old_len_2 = start_dic[position + 1]
old_start_index_2 = position + 1
old_end_index_2 = position + 1 + old_len_2 - 1
if old_len_2 == m:
m_len_interval.remove((old_start_index_2, old_end_index_2))
new_start = old_start_index_1
new_end = old_end_index_2
new_len = old_len_1 + 1 + old_len_2
if new_len == m:
m_len_interval.append((new_start, new_end))
start_dic[new_start] = new_len
end_dic[new_end] = new_len
del start_dic[old_start_index_2]
del end_dic[old_end_index_1]
if m_len_interval:
latest = step
return latest + 1 if latest != -1 else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR DICT DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
par = {}
sz = {}
target = set()
def find(i):
while i != par[i]:
par[i] = par[par[i]]
i = par[i]
return i
def union(i, j):
x = find(i)
y = find(j)
if x == y:
return
if sz[x] <= sz[y]:
sz[y] += sz[x]
par[x] = y
if sz[y] == m:
target.add(y)
if sz[x] == m and x in target:
target.remove(x)
else:
sz[x] += sz[y]
par[y] = x
if sz[x] == m:
target.add(x)
if sz[y] == m and y in target:
target.remove(y)
count = 1
ans = -1
target = set()
for i in arr:
if i not in par:
par[i] = i
sz[i] = 1
if m == 1:
target.add(i)
if i - 1 in par and i + 1 in par:
union(i - 1, i + 1)
union(i - 1, i)
elif i - 1 in par:
union(i - 1, i)
elif i + 1 in par:
union(i, i + 1)
t = set(target)
for x in t:
if sz[x] != m:
target.remove(x)
if len(target):
ans = count
count += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
if n == 1:
return 1
dic, groups = {i: (0) for i in range(1, n + 1)}, {
i: (0) for i in range(1, n + 1)
}
right = {i: (0) for i in range(1, n + 1)}
laststep = -1
for idx, i in enumerate(arr):
if (i == 1 or dic[i - 1] == 0) and (i == n or dic[i + 1] == 0):
groups[1] += 1
dic[i] = i
right[i] = i
elif (i == n or dic[i + 1] == 0) and (i > 0 and dic[i - 1] > 0):
leftmost = dic[i - 1]
dic[i] = leftmost
right[leftmost] = i
right[i] = i
groups[i - leftmost] -= 1
groups[i - leftmost + 1] += 1
elif (i == 1 or dic[i - 1] == 0) and (i < n and dic[i + 1] > 0):
rightmost = right[i + 1]
dic[rightmost] = i
dic[i] = i
right[i] = rightmost
groups[rightmost - i] -= 1
groups[rightmost - i + 1] += 1
else:
leftmost = dic[i - 1]
rightmost = right[i + 1]
right[leftmost] = rightmost
dic[rightmost] = leftmost
groups[rightmost - i] -= 1
groups[i - leftmost] -= 1
groups[rightmost - leftmost + 1] += 1
if groups[m] > 0:
laststep = idx + 1
return laststep | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class UnionFind:
def __init__(self, n):
self.parents = list(range(n))
self.ranks = [0] * n
def find(self, src):
if self.parents[src] == src:
return src
self.parents[src] = self.find(self.parents[src])
return self.parents[src]
def union(self, src, dest):
rootSrc, rootDest = self.find(src), self.find(dest)
if rootDest == rootSrc:
return False
if self.ranks[rootSrc] > self.ranks[rootDest]:
self.parents[rootDest] = rootSrc
self.ranks[rootSrc] += self.ranks[rootDest]
else:
self.parents[rootSrc] = rootDest
self.ranks[rootDest] += self.ranks[rootSrc]
return True
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n, result = len(arr), -1
uf = UnionFind(n)
for step, idx in enumerate(arr):
idx -= 1
uf.ranks[idx] = 1
for j in (idx - 1, idx + 1):
if 0 <= j < n:
if uf.ranks[uf.find(j)] == m:
result = step
if uf.ranks[j]:
uf.union(idx, j)
for i in range(n):
if uf.ranks[uf.find(i)] == m:
return n
return result | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
groupCount = dict()
n = len(arr)
parents = [0] * n
lastM = -1
mCnt = 0
for i, p in enumerate(arr):
leftParent = self.getParent(p - 1, parents)
rightParent = self.getParent(p + 1, parents)
parents[p - 1] = p
if leftParent == 0 and rightParent == 0:
groupCount[p] = 1
newCnt = 1
elif leftParent != 0 and rightParent != 0:
newCnt = groupCount[leftParent] + groupCount[rightParent] + 1
self.mergeGroups(leftParent, p, parents)
self.mergeGroups(rightParent, p, parents)
elif leftParent != 0:
newCnt = groupCount[leftParent] + 1
self.mergeGroups(leftParent, p, parents)
else:
newCnt = groupCount[rightParent] + 1
self.mergeGroups(rightParent, p, parents)
if leftParent != 0 and groupCount[leftParent] == m:
mCnt -= 1
if rightParent != 0 and groupCount[rightParent] == m:
mCnt -= 1
groupCount[p] = newCnt
if newCnt == m:
mCnt += 1
if mCnt > 0:
lastM = i + 1
return lastM
def getParent(self, p: int, parents: List[int]) -> int:
if p <= 0 or p > len(parents):
return 0
if p == parents[p - 1]:
return p
return self.getParent(parents[p - 1], parents)
def mergeGroups(self, pp, p, parents):
parents[pp - 1] = p | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, A, m):
starts = {}
ends = {}
res = -1
count = 0
for i, a in enumerate(A):
if a - 1 in ends and a + 1 in starts:
end = starts.pop(a + 1, None)
if end - (a + 1) + 1 == m:
count -= 1
st = ends.pop(a - 1, None)
if a - 1 - st + 1 == m:
count -= 1
starts[st] = end
ends[end] = st
if end - st + 1 == m:
count += 1
elif a - 1 in ends:
st = ends.pop(a - 1, None)
if a - 1 - st + 1 == m:
count -= 1
if a - 1 in starts:
starts.pop(a - 1, None)
ends[a] = st
starts[st] = a
if a - st + 1 == m:
count += 1
elif a + 1 in starts:
end = starts.pop(a + 1, None)
if end - (a + 1) + 1 == m:
count -= 1
if a + 1 in ends:
ends.pop(a + 1, None)
starts[a] = end
ends[end] = a
if end - a + 1 == m:
count += 1
else:
ends[a] = a
starts[a] = a
if m == 1:
count += 1
if count:
res = i + 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NONE IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NONE IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NONE IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NONE IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
index = [(-1) for i in range(len(arr))]
groups = [(0) for i in range(len(arr))]
count, lastindex = 0, -1
for i, num in enumerate(arr):
a = num - 1
groups[a], index[a], length = 1, a, 1
if a - 1 >= 0 and groups[a - 1] == 1:
left = index[a - 1]
if a - left == m:
count -= 1
index[left], index[a] = a, left
length += a - left
if a + 1 < len(arr) and groups[a + 1] == 1:
left, right = index[a], index[a + 1]
if right - a == m:
count -= 1
index[right], index[left] = left, right
length += right - a
if length == m:
count += 1
if count > 0:
lastindex = i + 1
return lastindex | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class UF:
def __init__(self, n):
self.p = [i for i in range(n + 1)]
self.counts = Counter()
self.rank = [(0) for i in range(n + 1)]
def getParent(self, i):
if self.p[i] == i:
return i
self.p[i] = self.getParent(self.p[i])
return self.p[i]
def set(self, i):
self.counts[1] += 1
self.rank[i] = 1
def isSet(self, i):
return 1 <= i < len(self.p) and self.rank[i] != 0
def getCount(self, i):
return self.counts[i]
def connect(self, i, j):
pi = self.getParent(i)
pj = self.getParent(j)
if pi != pj:
self.p[pi] = pj
ri, rj = self.rank[pi], self.rank[pj]
self.counts[ri] -= 1
self.counts[rj] -= 1
self.counts[ri + rj] += 1
self.rank[pj] = ri + rj
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
uf = UF(n)
result = -1
for i, e in enumerate(arr, start=1):
uf.set(e)
if uf.isSet(e - 1):
uf.connect(e - 1, e)
if uf.isSet(e + 1):
uf.connect(e, e + 1)
if uf.getCount(m) != 0:
result = i
return result | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF RETURN NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
a = [(1, len(arr))]
if m == len(arr):
return len(arr)
def binSearch(ar, num) -> int:
if len(ar) == 1:
if ar[0][0] <= num <= ar[0][1]:
return 0
lo, hi = 0, len(ar) - 1
while lo <= hi:
mid = (lo + hi) // 2
if ar[mid][1] < num:
lo = mid + 1
elif num < ar[mid][0]:
hi = mid
elif ar[mid][0] <= num <= ar[mid][1]:
return mid
else:
return -1
return -1
for i, n in enumerate(arr[::-1]):
idx = binSearch(a, n)
el = a[idx]
if el[0] == n:
if el[1] == n:
del a[idx]
else:
a[idx] = n + 1, el[1]
elif el[1] == n:
if el[0] == n:
del a[idx]
else:
a[idx] = el[0], n - 1
else:
a[idx] = el[0], n - 1
a.insert(idx + 1, (n + 1, el[1]))
if n - el[0] == m or el[1] - n == m:
return len(arr) - i - 1
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR RETURN NUMBER RETURN NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
start_dict = {}
end_dict = {}
m_dict = {}
last_step = -2
for i, num in enumerate(arr):
if num + 1 not in start_dict and num - 1 not in end_dict:
start = num
end = num
if num + 1 in start_dict and num - 1 not in end_dict:
end = start_dict.pop(num + 1)
start = num
m_dict[end - num].remove(num + 1)
if num + 1 not in start_dict and num - 1 in end_dict:
start = end_dict.pop(num - 1)
end = num
m_dict[num - start].remove(start)
if num + 1 in start_dict and num - 1 in end_dict:
end = start_dict.pop(num + 1)
start = end_dict.pop(num - 1)
m_dict[end - num].remove(num + 1)
m_dict[num - start].remove(start)
start_dict[start] = end
end_dict[end] = start
m_dict.setdefault(end - start + 1, set()).add(start)
if m in m_dict and m_dict[m]:
last_step = i
return last_step + 1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class UnionFind:
def __init__(self):
self.sets = {}
self.size = {}
self.sizes = collections.defaultdict(int)
def make_set(self, s):
self.sets[s] = s
self.size[s] = 1
self.sizes[1] += 1
def find(self, s):
if self.sets[s] != s:
self.sets[s] = self.find(self.sets[s])
return self.sets[s]
def union(self, s1, s2):
a, b = self.find(s1), self.find(s2)
if a == b:
return
self.sizes[self.size[a]] -= 1
self.sizes[self.size[b]] -= 1
if self.sizes[self.size[a]] == 0:
self.sizes.pop(self.size[a], None)
if self.sizes[self.size[b]] == 0:
self.sizes.pop(self.size[b], None)
self.sets[a] = b
self.size[b] += self.size[a]
self.sizes[self.size[b]] += 1
def get_size(self, m):
return m in self.sizes
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
seen = set()
uf = UnionFind()
res = -1
for i, v in enumerate(arr, 1):
uf.make_set(v)
if v + 1 in seen:
uf.union(v, v + 1)
if v - 1 in seen:
uf.union(v, v - 1)
seen.add(v)
if uf.get_size(m):
res = i
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NONE IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, A: List[int], m: int) -> int:
n = len(A)
arr = [0] * n
parent = [i for i in range(n)]
rank = [1] * n
groupSize = [0] * n
groupMap = set()
ans = -1
def ugm(x):
nonlocal m
if groupSize[x] == m:
groupMap.add(x)
elif x in groupMap:
groupMap.remove(x)
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
def join(x, y):
px = find(x)
py = find(y)
if px != py:
if px < py:
parent[py] = px
groupSize[px] += groupSize[py]
groupSize[py] = 0
else:
parent[px] = py
groupSize[py] += groupSize[px]
groupSize[px] = 0
ugm(px)
ugm(py)
for ind, num in enumerate(A):
num -= 1
arr[num] = 1
groupSize[num] = 1
ugm(num)
if num - 1 >= 0 and arr[num - 1]:
join(num - 1, num)
if num + 1 < n and arr[num + 1]:
join(num, num + 1)
if len(groupMap) > 0:
ans = ind + 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr, m):
n = len(arr)
if n == m:
return m
size = [0] * (n + 2)
res = -1
for i, x in enumerate(arr):
if size[x - 1] == m or size[x + 1] == m:
res = i
size[x - size[x - 1]] = size[x + size[x + 1]] = (
size[x - 1] + size[x + 1] + 1
)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
barr = [None for i in range(n + 1)]
comp_l = defaultdict(lambda: 0)
last = -1
for i, num in enumerate(arr):
low, high = num, num
if num > 1 and barr[num - 1] is not None:
llow, lhigh = barr[num - 1]
comp_l[lhigh - llow + 1] -= 1
low = llow
if num < len(barr) - 1 and barr[num + 1] is not None:
rlow, rhigh = barr[num + 1]
comp_l[rhigh - rlow + 1] -= 1
high = rhigh
comp_l[high - low + 1] += 1
barr[low] = low, high
barr[high] = low, high
if comp_l[m] > 0:
last = max(last, i + 1)
return last | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NONE ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NONE ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
groups = {}
cnt = 0
result = -1
for i, a in enumerate(arr):
b = a - 1
if b in groups:
l = groups.pop(b)
groups[a] = groups[b - l + 1] = l + 1
else:
groups[a] = 1
if groups[a] == m:
cnt += 1
elif groups[a] - 1 == m:
cnt -= 1
c = a + 1
if c in groups:
l = groups.pop(a)
r = groups.pop(c)
groups[c + r - 1] = groups[a - l + 1] = l + r
cnt += l + r == m
cnt -= (l == m) + (r == m)
if cnt != 0:
result = i + 1
return result | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
count = {}
for i in range(1, len(arr) + 1):
count[i] = 0
segment = {}
res = -1
for step, a in enumerate(arr):
if a - 1 in segment and a + 1 in segment:
count[abs(a - 1 - segment[a - 1]) + 1] -= 1
count[abs(a + 1 - segment[a + 1]) + 1] -= 1
count[abs(segment[a + 1] - segment[a - 1]) + 1] += 1
left = segment[a - 1]
right = segment[a + 1]
del segment[a - 1]
del segment[a + 1]
segment[left] = right
segment[right] = left
elif a - 1 in segment:
count[abs(a - 1 - segment[a - 1]) + 1] -= 1
count[abs(a - 1 - segment[a - 1]) + 2] += 1
left = segment[a - 1]
right = a
del segment[a - 1]
segment[left] = right
segment[right] = left
elif a + 1 in segment:
count[abs(a + 1 - segment[a + 1]) + 1] -= 1
count[abs(a + 1 - segment[a + 1]) + 2] += 1
left = a
right = segment[a + 1]
del segment[a + 1]
segment[left] = right
segment[right] = left
else:
count[1] += 1
segment[a] = a
if count[m] > 0:
res = step + 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def find(self, node1: int, group: List[int]) -> int:
head = node1
while head != group[head]:
head = group[head]
return head
def union(self, node1: int, node2: int, group: List[int], size: dict) -> int:
head1, head2 = self.find(node1, group), self.find(node2, group)
if head1 == head2:
return head1
if size[head1] < size[head2]:
group[head1] = head2
size[head2] += size[head1]
size.pop(head1)
return head2
group[head2] = head1
size[head1] += size[head2]
size.pop(head2)
return head1
def findLatestStep(self, arr: List[int], m: int) -> int:
if m == len(arr):
return m
group = [(-1) for _ in range(len(arr))]
sizes = {}
ans = -1
arr_size = len(arr)
for ix, val in enumerate(arr):
if group[val - 1] != -1:
continue
group[val - 1] = val - 1
sizes[val - 1] = 1
for neighbor in (val - 2, val):
if 0 <= neighbor < arr_size:
head = self.find(neighbor, group)
if head in sizes and sizes[head] == m:
ans = ix
if group[neighbor] != -1:
self.union(val - 1, neighbor, group, sizes)
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR BIN_OP VAR NUMBER VAR IF NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
end = {}
n = len(arr)
count = collections.defaultdict(int)
string = [0] * (n + 1)
ans = -1
for i in range(len(arr)):
start = arr[i]
string[start] = 1
left = start
right = start
flag1, flag2 = False, False
if arr[i] - 1 > 0 and string[arr[i] - 1] == 1:
l, r = end[arr[i] - 1]
left = l
count[r - l + 1] -= 1
flag1 = True
if arr[i] + 1 <= n and string[arr[i] + 1] == 1:
l2, r2 = end[arr[i] + 1]
right = r2
count[r2 - l2 + 1] -= 1
flag2 = True
end[arr[i]] = left, right
if flag1:
end[l] = l, right
if flag2:
end[r2] = left, r2
count[right - left + 1] += 1
if count[m] > 0:
ans = i + 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
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 given an integer m and you need to find the latest step at which there exists a group of ones of length m. A group of ones is a contiguous substring of 1s 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.
Example 3:
Input: arr = [1], m = 1
Output: 1
Example 4:
Input: arr = [2,1], m = 2
Output: 2
Constraints:
n == arr.length
1 <= n <= 10^5
1 <= arr[i] <= n
All integers in arr are distinct.
1 <= m <= arr.length | class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
cnt, ans = 0, -1
start_end, end_start = {}, {}
for i, n in enumerate(arr, start=1):
start, end = n, n
if n - 1 in end_start:
start = end_start[n - 1]
del end_start[n - 1]
if n - start == m:
cnt -= 1
if n + 1 in start_end:
end = start_end[n + 1]
del start_end[n + 1]
if end - n == m:
cnt -= 1
start_end[start] = end
end_start[end] = start
if end - start + 1 == m:
cnt += 1
if cnt >= 1:
ans = i
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR DICT DICT FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.