description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, N):
s = []
for i in S:
while s and s[-1] > i and N > 0:
s.pop()
N -= 1
s.append(i)
t = "".join(s)
t = t.lstrip("0")
if len(t) == 0:
return "0"
if N > 0:
if len(t) <= N:
return "0"
return t[:-N]
return t | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER RETURN STRING IF VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN STRING RETURN VAR VAR RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stk = []
for i in range(len(S)):
while K > 0 and stk and stk[-1] > S[i]:
stk.pop()
K -= 1
stk.append(S[i])
stk = stk[: len(stk) - K]
ans = "".join(stk).lstrip("0")
if ans == "":
return "0"
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING IF VAR STRING RETURN STRING RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, k):
n = len(S)
st = []
for i in range(n):
while len(st) != 0 and S[i] < st[-1] and k > 0:
st.pop()
k -= 1
if len(st) != 0 or S[i] != 0:
st.append(S[i])
if len(st) == 1 and st[-1] == "0":
st.pop()
while len(st) != 0 and k > 0:
k -= 1
st.pop()
if len(st) == 0:
return "0"
res = ""
while len(st) != 0:
z = st[-1]
st.pop()
res = z + res
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, s, k):
if k == len(s):
return "0"
stack = []
for dig in s:
while len(stack) > 0 and dig < stack[-1] and k > 0:
stack.pop()
k -= 1
stack.append(dig)
while k > 0:
stack.pop()
k -= 1
stack = stack[::-1]
while len(stack) > 0 and stack[-1] == "0":
stack.pop()
stack = stack[::-1]
return "".join(stack) if len(stack) > 0 else "0" | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR LIST FOR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL STRING VAR STRING |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, num, k):
if k == len(num):
return "0"
stack = []
for i in range(len(num)):
while stack and k > 0 and num[stack[-1]] > num[i]:
stack.pop()
k -= 1
stack.append(i)
while k > 0 and stack:
stack.pop()
k -= 1
answer = ""
for i in range(len(stack)):
if num[stack[i]] != "0" or answer != "":
answer += num[stack[i]]
return "0" if answer == "" else answer | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR STRING VAR VAR VAR VAR RETURN VAR STRING STRING VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, s, k):
st = []
for x in s:
while st and k and st[-1] > x:
st.pop()
k -= 1
if x != "0" or st:
st.append(x)
while st and k:
st.pop()
k -= 1
if not st:
return 0
return "".join(st) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR STRING VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR RETURN NUMBER RETURN FUNC_CALL STRING VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | 788
7
78
788
class Solution:
def removeKdigits(self, S, K):
stack = []
for el in S:
while stack and K and stack[-1] > el:
stack.pop()
K -= 1
stack.append(el)
for i in range(K):
stack.pop()
return int("".join(stack)) | EXPR NUMBER EXPR NUMBER EXPR NUMBER EXPR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL STRING VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, num, k):
numStack = []
for digit in num:
while k and numStack and numStack[-1] > digit:
numStack.pop()
k -= 1
numStack.append(digit)
finalStack = numStack[:-k] if k else numStack
return "".join(finalStack).lstrip("0") or "0" | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL FUNC_CALL STRING VAR STRING STRING |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Node:
def __init__(self, v):
self.v = v
self.prev = self.next = None
class Solution:
def removeKdigits(self, S, K):
guard = Node(-1)
p = guard
for c in S:
nd = Node(int(c))
p.next = nd
nd.prev = p
p = nd
nd = guard.next
while nd and nd.next and K > 0:
if nd.v > nd.next.v:
nex, npr = nd.next, nd.prev
npr.next = nex
nex.prev = npr
nd.next = nd.prev = None
nd = npr
K -= 1
else:
nd = nd.next
nd = guard.next
while nd and nd.v == 0:
nd = nd.next
out = []
while nd:
out.append(str(nd.v))
nd = nd.next
ans = "".join(out)
if len(ans) <= K:
return "0"
else:
return ans[: len(ans) - K] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NONE ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR VAR RETURN STRING RETURN VAR BIN_OP FUNC_CALL VAR VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = []
for elem in S:
while K > 0 and len(stack) > 0 and stack[-1] > elem:
stack.pop()
K -= 1
stack.append(elem)
while K > 0:
stack.pop()
K -= 1
ans = ""
while stack:
x = stack.pop(0)
ans += x
newans = int(ans)
return str(newans) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
answer = ""
for ch in S:
while answer and answer[-1] > ch and K > 0:
answer = answer[:-1]
K -= 1
if answer or ch != "0":
answer += ch
while answer and K > 0:
answer = answer[:-1]
K -= 1
if not answer:
return "0"
return answer | CLASS_DEF FUNC_DEF ASSIGN VAR STRING FOR VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR RETURN STRING RETURN VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, num, n):
stack = []
for i in range(len(num)):
while stack and n > 0 and stack[-1] > num[i]:
stack.pop()
n -= 1
if n <= 0:
break
stack.append(num[i])
if n <= 0:
for k in range(i, len(num)):
stack.append(num[k])
else:
while n > 0:
stack.pop()
n -= 1
res = ""
for i in stack:
res += i
return int(res) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given a non-negative integer S represented as a string, remove K digits from the number so that the new number is the smallest possible.
Note : The given num does not contain any leading zero.
Example 1:
Input:
S = "149811", K = 3
Output: 111
Explanation: Remove the three digits
4, 9, and 8 to form the new number 111
which is smallest.
Example 2:
Input:
S = "1002991", K = 3
Output: 21
Explanation: Remove the three digits 1(leading
one), 9, and 9 to form the new number 21(Note
that the output must not contain leading
zeroes) which is the smallest.
Your Task:
You don't need to read input or print anything. Your task is to complete the function removeKdigits() which takes the string S and an integer K as input and returns the new number which is the smallest possible.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(|S|).
Constraints:
1<=|S|<=10002
1<=K<=|S| | class Solution:
def removeKdigits(self, S, K):
stack = []
for n in S:
while stack and K > 0 and stack[-1] > n:
stack.pop()
K -= 1
if stack or n is not "0":
stack.append(n)
if K:
stack = stack[0:-K]
return "".join(stack) or "0" | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR NUMBER VAR RETURN FUNC_CALL STRING VAR STRING |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
adjListA = [[] for i in range(n)]
adjListB = [[] for i in range(n)]
for i in range(0, 2 * e, 2):
x, y = A[i], A[i + 1]
a, b = B[i], B[i + 1]
adjListA[x].append(y)
adjListB[a].append(b)
for i, x in enumerate(adjListA):
if adjListA[i][::-1] != adjListB[i]:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def __init__(self):
self.flag = 1
def dfs(self, a1, a2, g):
if g not in a2 or a1[g] != a2[g][::-1]:
self.flag = 0
return
else:
print(a1[g], a2[g])
for i in a1[g]:
self.dfs(a1, a2, i)
def checkMirrorTree(self, n, e, A, B):
a1 = {}
a2 = {}
for i in range(1, n + 1):
a1[i] = []
a2[i] = []
for i in range(0, len(A), 2):
if A[i] not in a1:
a1[A[i]] = []
a1[A[i]].append(A[i + 1])
for i in range(0, len(B), 2):
if B[i] not in a2:
a2[B[i]] = []
a2[B[i]].append(B[i + 1])
for i in a1:
if a1[i] != a2[i][::-1]:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
dic = {}
for i in range(0, len(A), 2):
try:
dic[A[i]].append(A[i + 1])
except KeyError:
dic[A[i]] = []
dic[A[i]].append(A[i + 1])
for i in range(0, len(B), 2):
if B[i] in dic.keys() and len(dic[B[i]]) > 0:
el = dic[B[i]].pop(-1)
if el != B[i + 1]:
return 0
else:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
dictA = {}
dictB = {}
for x in range(0, 2 * e, 2):
if A[x] in dictA:
dictA[A[x]].append(A[x + 1])
else:
dictA[A[x]] = [A[x + 1]]
if B[x] in dictB:
dictB[B[x]].append(B[x + 1])
else:
dictB[B[x]] = [B[x + 1]]
for x in dictA:
if dictA[x] != list(reversed(dictB[x])):
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
map = dict()
for i in range(n):
map[i] = []
for i in range(0, 2 * e, 2):
map[A[i]].append(A[i + 1])
for i in range(0, 2 * e, 2):
if len(map[B[i]]) == 0 or B[i + 1] != map[B[i]].pop():
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
mp = {}
for i in range(e):
u = A[2 * i]
v = A[2 * i + 1]
if u in mp:
mp[u].append(v)
else:
mp[u] = [v]
for i in range(e):
u = B[2 * i]
v = B[2 * i + 1]
if u not in mp:
return 0
if len(mp[u]) == 0:
return 0
top = mp[u].pop()
if v != top:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
h1 = {}
h2 = {}
prevpar = None
for i in range(e):
par = A[2 * i]
if par not in h1:
h1[par] = []
h1[par].append(A[2 * i + 1])
for i in range(e):
par = B[2 * i]
if par not in h2:
h2[par] = []
h2[par].append(B[2 * i + 1])
root = A[0]
def ismir(root, h1, h2):
if root not in h1:
return 1
nnn = len(h1[root])
for i in range(nnn):
if h1[root][i] == h2[root][nnn - i - 1]:
if not ismir(h1[root][i], h1, h2):
return 0
else:
return 0
return 1
return ismir(root, h1, h2) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
mpa = dict()
mpb = dict()
for i in range(1, n + 1):
mpa[i] = []
mpb[i] = []
for i in range(0, 2 * e, 2):
mpa[A[i]].append(A[i + 1])
mpb[B[i]].append(B[i + 1])
for i in range(1, n + 1):
lista = mpa[i]
listb = mpb[i]
flag = True
if len(lista) != len(listb):
flag = False
return 0
else:
ln = len(lista)
for x in range(ln):
if lista[x] != listb[ln - 1 - x]:
flag = False
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
d1 = {}
d2 = {}
for i in range(0, 2 * e, 2):
if A[i] not in d1:
d1[A[i]] = [A[i + 1]]
else:
d1[A[i]].append(A[i + 1])
for i in range(0, 2 * e, 2):
if B[i] not in d2:
d2[B[i]] = [B[i + 1]]
else:
d2[B[i]].append(B[i + 1])
l = list(d1.keys())
for i in l:
t1 = d1[i]
t2 = d2[i]
t2.reverse()
if t1 != t2:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
m = {}
for i in range(0, 2 * e, 2):
if A[i] in m:
m[A[i]].append(A[i + 1])
else:
m[A[i]] = [A[i + 1]]
for i in range(0, 2 * e, 2):
if B[i + 1] != m[B[i]].pop():
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def constructMap(self, n, lst, dct):
mapA = dct
i = 0
j = 1
while i <= n and j <= n:
try:
mapA[lst[i]] = mapA[lst[i]] + [lst[j]]
except:
mapA[lst[i]] = [lst[j]]
i += 2
j += 2
return mapA
def checkMirrorTree(self, n, e, A, B):
mapA = {}
mapB = {}
mapA = self.constructMap(2 * e, A, mapA)
mapB = self.constructMap(2 * e, B, mapB)
for itr in mapA:
tA = mapA.get(itr)
tB = mapB.get(itr)
if tA[::-1] != tB:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR LIST VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
def _buildTree(arr):
n = len(arr)
graph = {}
for i in range(0, n, 2):
if arr[i] not in graph:
graph[arr[i]] = []
graph[arr[i]].append(arr[i + 1])
return graph
tree1 = _buildTree(A)
tree2 = _buildTree(B)
def _isMirror(root1, root2):
if root1 != root2:
return False
children1 = tree1.get(root1, [])
children2 = tree2.get(root2, [])
if len(children1) != len(children2):
return False
n = len(children1)
for i in range(n):
if _isMirror(children1[i], children2[n - i - 1]) == False:
return False
return True
return 1 if _isMirror(A[0], B[0]) else 0 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR FUNC_CALL VAR VAR LIST IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
i = 0
mp = {}
if len(A) != len(B):
return 0
while i < len(A):
try:
mp[A[i]].append(A[i + 1])
except:
mp[A[i]] = [A[i + 1]]
i = i + 2
j = 0
while j < len(B):
if (
B[j] not in mp.keys()
or len(mp[B[j]]) == 0
or mp[B[j]].pop() != B[j + 1]
):
return 0
j = j + 2
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
set1, set2 = {}, {}
for i in range(0, len(A), 2):
if A[i : i + 2][0] not in set1:
set1[A[i : i + 1][0]] = []
set1[A[i : i + 2][0]].append(A[i : i + 2][1])
for i in range(0, len(B), 2):
if B[i : i + 2][0] not in set2:
set2[B[i : i + 1][0]] = []
set2[B[i : i + 2][0]].append(B[i : i + 2][1])
for i in set1.keys():
if set1[i][::-1] != set2[i]:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER LIST EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER LIST EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
map1 = {}
i = 0
while i < 2 * e:
if A[i] in map1:
map1[A[i]] += [A[i + 1]]
else:
map1[A[i]] = [A[i + 1]]
i += 2
j = 0
while j < 2 * e:
if map1[B[j]][-1] != B[j + 1]:
return 0
map1[B[j]].pop()
j += 2
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
c = {}
for i in range(0, 2 * e, 2):
if A[i] not in c:
c[A[i]] = [A[i + 1]]
else:
c[A[i]].append(A[i + 1])
for i in range(0, 2 * e, 2):
if B[i] in c and c[B[i]] and c[B[i]].pop() == B[i + 1]:
continue
else:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
a1 = {}
a2 = {}
a = []
b = []
d = A[0]
f = B[0]
for i in range(0, len(A) - 1, 2):
if A[i] in a1.keys():
a1[A[i]].append(A[i + 1])
else:
a1[A[i]] = [A[i + 1]]
if B[i] in a2.keys():
a2[B[i]].append(B[i + 1])
else:
a2[B[i]] = [B[i + 1]]
f = 0
if sorted(a1.keys()) == sorted(a2.keys()):
for k in a1.keys():
if a1[k] != a2[k][::-1]:
f = 1
return 0
else:
return 0
if f == 0:
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER IF VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
dic, bic = {}, {}
for i in range(0, len(A), 2):
try:
dic[A[i]] += [A[i + 1]]
except:
dic[A[i]] = [A[i + 1]]
for i in range(0, len(B), 2):
try:
bic[B[i]] += [B[i + 1]]
except:
bic[B[i]] = [B[i + 1]]
for i in dic:
try:
if dic[i] != bic[i][::-1]:
return 0
except:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
dp1 = [[] for i in range(n)]
dp2 = [[] for i in range(n)]
i = 0
while i < len(A):
dp1[A[i] - 1].append(A[i + 1] - 1)
dp2[B[i] - 1].append(B[i + 1] - 1)
i += 2
for i in dp2:
i.reverse()
for i in range(n):
if dp1[i] != dp2[i]:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
dic1 = {}
for i in range(0, len(A), 2):
if A[i] not in dic1:
dic1[A[i]] = [A[i + 1]]
else:
dic1[A[i]].append(A[i + 1])
dic2 = {}
for i in range(0, len(B), 2):
if B[i] not in dic2:
dic2[B[i]] = [B[i + 1]]
else:
dic2[B[i]].append(B[i + 1])
for i in dic1:
if dic1[i] != dic2[i][::-1]:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
d = {}
for i in range(0, len(A), 2):
if A[i] not in d:
d[A[i]] = []
d[A[i]].append(A[i + 1])
for i in range(0, len(B), 2):
k = d[B[i]].pop()
if k != B[i + 1]:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
if A == B and len(A) > 2:
temp = set(A)
length = len(temp)
flag = 0
s = A.count(A[0]) + A.count(A[-1])
if s == 2:
for i in range(1, length - 1):
if A.count(A[i]) != 2:
return 0
return 1
else:
return 0
return 1 | CLASS_DEF FUNC_DEF IF VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
dic = {}
for i in range(0, len(A), 2):
if A[i] not in dic:
dic[A[i]] = []
dic[A[i]].append(A[i + 1])
for i in range(0, len(B), 2):
if B[i] not in dic:
return 0
else:
if dic[B[i]][-1] != B[i + 1]:
return 0
dic[B[i]].pop()
for i in dic:
if dic[i]:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
mp = dict()
i = 0
while i < len(A):
if A[i] in mp:
mp[A[i]].append(A[i + 1])
else:
mp[A[i]] = []
mp[A[i]].append(A[i + 1])
i += 2
i = 0
while i < len(B):
if B[i] in mp and len(mp[B[i]]) > 0:
if mp[B[i]][-1] == B[i + 1]:
mp[B[i]].pop()
else:
return 0
i += 2
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, arr_1, arr_2):
n = len(arr_1)
m = len(arr_2)
if n != m:
return False
node_element_dict = {}
n = len(arr_1)
i = 0
while i < n:
node = arr_1[i]
connect_to = arr_1[i + 1]
if node in node_element_dict:
node_element_dict[node].append(connect_to)
else:
node_element_dict[node] = [connect_to]
i = i + 2
m = len(arr_2)
j = 0
answer = 1
while j < m:
node = arr_2[j]
connect_to = arr_2[j + 1]
if node not in node_element_dict:
answer = 0
break
else:
pop_element = node_element_dict[node].pop()
if pop_element != connect_to:
answer = 0
break
j = j + 2
return answer | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
a1 = {}
a2 = {}
for i in range(1, n + 1):
a1[i] = []
a2[i] = []
for i in range(0, len(A), 2):
if A[i] not in a1:
a1[A[i]] = []
a1[A[i]].append(A[i + 1])
for i in range(0, len(B), 2):
if B[i] not in a2:
a2[B[i]] = []
a2[B[i]].append(B[i + 1])
for i in a1:
if a1[i] != a2[i][::-1]:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
d = {}
for i in range(0, len(A), 2):
if A[i] in d:
d[A[i]].append(A[i + 1])
else:
d[A[i]] = [A[i + 1]]
e = {}
for i in range(0, len(B), 2):
if B[i] in e:
e[B[i]].append(B[i + 1])
else:
e[B[i]] = [B[i + 1]]
for i in d:
if i not in e:
return 0
t1 = d[i]
t2 = e[i][::-1]
if t1 != t2:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
map1 = {}
i = 0
temp = A[0]
ans = []
while i < len(A) - 1:
if A[i] == temp:
ans.append(A[i + 1])
i += 2
else:
map1[temp] = ans
ans = []
temp = A[i]
map1[temp] = ans
i = 0
while i < len(B) - 1:
if B[i + 1] != map1[B[i]].pop():
return 0
i += 2
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER |
Given two n-ary trees. Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has 2*e space separated values u,v denoting an edge from u to v for the both trees.
Example 1:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 3, 1, 2}
Output:
1
Explanation:
1 1
/ \ / \
2 3 3 2
As we can clearly see, the second tree
is mirror image of the first.
Example 2:
Input:
n = 3, e = 2
A[] = {1, 2, 1, 3}
B[] = {1, 2, 1, 3}
Output:
0
Explanation:
1 1
/ \ / \
2 3 2 3
As we can clearly see, the second tree
isn't mirror image of the first.
Your Task:
You don't need to read input or print anything. Your task is to complete the function checkMirrorTree() which takes 2 Integers n, and e; and two arrays A[] and B[] of size 2*e as input and returns 1 if the trees are mirror images of each other and 0 if not.
Expected Time Complexity: O(e)
Expected Auxiliary Space: O(e)
Constraints:
1 <= n,e <= 10^{5} | class Solution:
def checkMirrorTree(self, n, e, A, B):
has = {}
for i in range(1, n + 1):
has[i] = []
for i in range(0, 2 * e, 2):
has[A[i]].append(A[i + 1])
for i in range(0, 2 * e, 2):
temp = has[B[i]].pop()
if temp != B[i + 1]:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
last_one = None
for i in range(len(nums)):
if nums[i]:
if last_one != None and i - last_one - 1 < k:
return False
last_one = i
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NONE BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart1(self, nums: List[int], k: int) -> bool:
d = [i for i, v in enumerate(nums) if v == 1]
for i in range(1, len(d)):
if d[i] - d[i - 1] <= k:
return False
return True
def kLengthApart(self, nums: List[int], k: int) -> bool:
idx = -1
for i in range(len(nums)):
if nums[i] == 1:
if idx != -1 and i - idx - 1 < k:
return False
idx = i
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
try:
last = nums.index(1)
except:
return True
for i in range(last + 1, len(nums)):
if nums[i] == 1 and i - last <= k:
return False
elif nums[i] == 1:
last = i
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR RETURN NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if len(nums) == 1:
return True
i = 0
while i < len(nums):
if nums[i] == 1:
break
i += 1
counter = 0
i += 1
while i < len(nums):
j = i - 1
print(nums[j], nums[i])
if nums[j] == 0 and nums[i] == 1:
if counter < k:
return False
counter = 0
if (nums[j] == 1 or nums[j] == 0) and nums[i] == 0:
counter += 1
if nums[j] == 1 and nums[i] == 1 and k >= 1:
return False
i += 1
j += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
prev = 0
i = 0
for num in nums:
if num == 1:
if prev and k > i - prev:
return False
prev = i + 1
i += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if k == 0:
return True
while sum(nums) >= 2:
temp = nums.index(1)
nums[temp] = 0
if nums.index(1) - temp <= k:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
last1 = -1
tally = len(nums)
for idx, val in enumerate(nums):
if val == 1:
last1 = idx
if tally < k:
return False
tally = 0
else:
tally += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
h = defaultdict(list)
for i in range(len(nums)):
h[nums[i]].append(i)
if not h[1]:
return True
return all([(h[1][i] - h[1][i - 1] > k) for i in range(1, len(h[1]))]) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
listx = []
for i in range(0, len(nums)):
if nums[i] == 1:
if listx:
if abs(i - listx[-1]) >= k + 1:
listx.append(i)
else:
return False
else:
listx.append(i)
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
index = []
for i in range(len(nums)):
if nums[i] == 1:
index.append(i)
if not index:
return True
if len(index) == 1:
return True
print(index)
for i in range(len(index) - 1):
print(i)
if index[i + 1] - index[i] >= k + 1:
continue
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
before = -k - 5
for i in range(len(nums)):
if nums[i] == 1:
if i - before <= k:
return False
before = i
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
zCounter = k
for n in nums:
if n == 1:
if zCounter < k:
return False
zCounter = 0
else:
zCounter += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
pos = []
for i in range(len(nums)):
if nums[i] == 1:
pos.append(i)
true = 0
false = 0
for j in range(len(pos) - 1):
if pos[j + 1] - pos[j] <= k:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
prev = -k - 1
for i, n in enumerate(nums):
if n == 1:
if i - prev > k:
prev = i
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
for idx in range(len(nums)):
if nums[idx] == 1 and 1 in nums[idx + 1 : idx + k + 1]:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
result = []
n = len(nums)
for i in range(n):
if nums[i] == 1:
result.append(i + 1)
m = len(result)
for j in range(m - 1):
if result[j + 1] - result[j] <= k:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
p = -1000000
n = len(nums)
for i in range(n):
if nums[i] == 1:
if i - p - 1 >= k:
p = i
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
next_one = 0
previous_one = 0
while next_one < len(nums) and nums[next_one] != 1:
next_one += 1
if next_one == len(nums):
return True
previous_one = next_one
next_one += 1
while next_one < len(nums):
while next_one < len(nums) and nums[next_one] != 1:
next_one += 1
if next_one < len(nums) and next_one - previous_one - 1 < k:
return False
elif next_one < len(nums) and next_one - previous_one - 1 >= k:
previous_one = next_one
else:
return True
next_one += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
lis = []
for i in range(len(nums)):
if nums[i] == 1:
lis.append(i)
for i in range(1, len(lis)):
if lis[i] - lis[i - 1] - 1 < k:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
i = 0
while i < len(nums):
if nums[i]:
break
i += 1
j = i
i += 1
while i < len(nums):
if nums[i]:
if i - j - 1 < k:
return False
j = i
i += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if 1 not in nums:
return True
num_place = nums.index(1)
f_idx = nums.index(1) + 1
for i in range(f_idx, len(nums)):
if nums[i] == 1 and i - num_place < k + 1:
return False
elif nums[i] == 1 and i - num_place > k:
num_place = i
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF NUMBER VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
n = len(nums)
prev = -1
ans = []
for i in range(n):
if prev == -1:
if nums[i] == 1:
prev = i
elif nums[i] == 1:
ans.append(i - prev - 1)
prev = i
for i in ans:
if i < k:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
last1 = -1
for num in nums:
if num == 0 and last1 != -1:
last1 += 1
elif num == 1 and last1 != -1:
if last1 < k:
return False
last1 = 0
elif num == 1 and last1 == -1:
last1 = 0
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
i = 0
while i < len(nums) and nums[i] == 0:
i += 1
first = i
for j in range(i + 1, len(nums)):
if nums[j] == 0:
continue
else:
second = j
print((first, second))
dis = second - first - 1
if dis < k:
return False
first = second
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
locs = {}
for i in range(len(nums)):
if nums[i] == 1:
locs[i] = True
key = list(locs.keys())
for i in range(len(key) - 1):
if key[i + 1] - key[i] - 1 < k:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
c = nums.count(1) - 1
if c < 0:
return True
if c == 0:
return True
ini = nums.index(1)
while c:
fin = nums.index(1, ini + 1, len(nums))
print(fin)
if fin - ini < k + 1:
return False
c -= 1
ini = fin
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if k == 0:
return True
flag = 0
start = 0
for i in range(len(nums)):
if i == 0:
if nums[i] == 1:
start = 1
else:
start = 0
continue
if nums[i] == 0 and start == 1:
flag += 1
if nums[i] == 1:
if start == 1:
if flag < k:
return False
start = 1
flag = 0
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
count = 0
for i in range(len(nums)):
if nums[i] == 1:
if i >= 1:
if count < k:
return False
count = 0
else:
count += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if not k:
return True
pre_index = -1
for i, val in enumerate(nums):
if val:
if pre_index != -1 and i - pre_index - 1 < k:
return False
pre_index = i
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
count = 0
first = False
for i in nums:
if i == 1 and not first:
first = True
elif i == 1 and first:
if k <= count:
count = 0
else:
return False
else:
count += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
zeros = 0
i = 0
while i < len(nums):
if i == 0 and nums[i] == 1:
i += 1
continue
if nums[i] == 1 and zeros < k:
return False
break
elif nums[i] == 0:
zeros += 1
i += 1
elif nums[i] == 1 and zeros >= k:
zeros = 0
i += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
position = -1
for index, item in enumerate(nums):
if item & 1:
if index - position - 1 < k and position != -1:
return False
position = index
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
last = float("-inf")
for i, x in enumerate(nums):
if x == 1:
if i - 1 - last < k:
return False
last = i
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
ind = 0
for i, num in enumerate(nums):
if not nums[ind] and num:
ind = i
if i != ind and num:
if i - ind < k + 1:
return False
ind = i
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if k == 0:
return True
N = len(nums)
past, curr = -1, 0
for curr in range(N):
if nums[curr] == 1:
if past < 0:
past = curr
elif curr - past > k:
past = curr
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
i, j, d = float("-inf"), 0, float("inf")
while j < len(nums):
if nums[j]:
d = min(d, j - i - 1)
if d < k:
return False
i = j
j += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING WHILE VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
xs = [i for i, n in enumerate(nums) if n]
return all(y - x > k for x, y in zip(xs, xs[1:])) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
prev = 0
while prev < len(nums) and nums[prev] == 0:
prev += 1
if prev == len(nums) or nums[prev] == 0:
return True
if k == 0:
return True
for i in range(prev + 1, len(nums)):
if nums[i] == 1:
if i - prev <= k:
return False
prev = i
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
last_loc, curr_loc = -1, -1
for i, n in enumerate(nums):
if n == 1:
if last_loc == -1:
last_loc = i
elif curr_loc == -1:
curr_loc = i
curr_loc = i
print((i, last_loc, curr_loc))
if curr_loc - last_loc <= k and curr_loc != last_loc:
return False
last_loc = i
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
p = 0
count = k + 1
while p < len(nums):
if nums[p] == 1:
if count <= k:
return False
else:
count = 0
count += 1
p += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
counter = -1
for i in nums:
if counter < 0:
if i == 1:
counter = 0
elif i == 0:
counter += 1
else:
if counter < k:
return False
counter = 0
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
res = []
start_key = 0
while start_key != len(nums):
if nums[start_key] == 1:
res.append(start_key)
start_key += 1
zero = 0
while zero < len(res) - 1:
if res[zero + 1] - (res[zero] + 1) < k:
return False
zero += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR RETURN NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
pos = 0
while pos < len(nums):
try:
pos = nums.index(1, pos)
pos2 = nums.index(1, pos + 1)
except:
break
if pos2 - pos - 1 < k:
return False
pos = pos2
print(pos)
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
s = -1
n = len(nums)
for i in range(n):
if nums[i] == 1 and s == -1:
s = i
elif nums[i] == 1:
interval = i - s - 1
if interval < k:
return False
s = i
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if 1 not in nums:
return True
prev = nums.index(1)
if prev == len(nums) - 1:
return True
for i in range(prev + 1, len(nums)):
if nums[i] == 1:
if i - prev <= k:
return False
prev = i
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF NUMBER VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if k == 0:
return True
idx = [n for n, x in enumerate(nums) if x == 1]
for i in range(1, len(idx)):
print((idx[i], idx[i - 1]))
if idx[i] - idx[i - 1] <= k:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
i, j = 0, 0
flag = True
count = 0
while i < len(nums):
if nums[i] == 1:
if flag:
count = 0
flag = False
else:
if count < k:
return False
count = 0
else:
count += 1
i += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
ones = [-k - 1]
for i, num in enumerate(nums):
if num:
if i - ones[-1] < k + 1:
return False
else:
ones.append(i)
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
flag = 0
start = None
end = 0
for i in range(len(nums)):
if nums[i] == 1 and flag == 0:
start = i
flag = 1
elif nums[i] == 1 and flag == 1:
end = i
dis = end - start
start = i
if dis <= k:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if k > 0:
flag = False
last1 = -1
for i in range(len(nums)):
if not flag:
if nums[i] == 1:
flag = True
last1 = i
elif nums[i] == 1:
if i - last1 - 1 < k:
return False
last1 = i
return True
else:
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
temp = 0
increment = False
one_checked = False
for i in nums:
if one_checked and i != 1:
temp += 1
print(temp, i)
if i == 1:
if one_checked and temp < k:
return False
temp = 0
one_checked = True
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
def find(nums, *argc):
try:
return nums.index(*argc)
except ValueError:
return -1
prev = find(nums, 1)
while prev != -1:
cur = find(nums, 1, prev + 1)
if cur != -1 and cur - prev - 1 < k:
return False
prev = cur
return True | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
if nums.count(0) == len(nums):
return True
idx = nums.index(1)
ctr = 0
for num in nums[idx + 1 :]:
if num == 1:
if ctr < k:
return False
ctr = 0
else:
ctr += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.
Example 1:
Input: nums = [1,0,0,0,1,0,0,1], k = 2
Output: true
Explanation: Each of the 1s are at least 2 places away from each other.
Example 2:
Input: nums = [1,0,0,1,0,1], k = 2
Output: false
Explanation: The second 1 and third 1 are only one apart from each other.
Example 3:
Input: nums = [1,1,1,1,1], k = 0
Output: true
Example 4:
Input: nums = [0,1,0,1], k = 1
Output: true
Constraints:
1 <= nums.length <= 10^5
0 <= k <= nums.length
nums[i] is 0 or 1 | class Solution:
def kLengthApart(self, nums: List[int], k: int) -> bool:
start = 0
for ind in range(len(nums)):
if nums[ind] == 1:
start = ind
break
if start >= len(nums) - 1:
return True
minDist = float("inf")
print(start)
for i in range(start + 1, len(nums)):
if nums[i] == 1:
dist = i - start - 1
if dist < minDist:
minDist = dist
start = i
return minDist >= k | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR |
You are given an array a of size n, and q queries to it. There are queries of two types: 1 l_{i} r_{i} — perform a cyclic shift of the segment [l_{i}, r_{i}] to the right. That is, for every x such that l_{i} ≤ x < r_{i} new value of a_{x} + 1 becomes equal to old value of a_{x}, and new value of a_{l}_{i} becomes equal to old value of a_{r}_{i}; 2 l_{i} r_{i} — reverse the segment [l_{i}, r_{i}].
There are m important indices in the array b_1, b_2, ..., b_{m}. For each i such that 1 ≤ i ≤ m you have to output the number that will have index b_{i} in the array after all queries are performed.
-----Input-----
The first line contains three integer numbers n, q and m (1 ≤ n, q ≤ 2·10^5, 1 ≤ m ≤ 100).
The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
Then q lines follow. i-th of them contains three integer numbers t_{i}, l_{i}, r_{i}, where t_{i} is the type of i-th query, and [l_{i}, r_{i}] is the segment where this query is performed (1 ≤ t_{i} ≤ 2, 1 ≤ l_{i} ≤ r_{i} ≤ n).
The last line contains m integer numbers b_1, b_2, ..., b_{m} (1 ≤ b_{i} ≤ n) — important indices of the array.
-----Output-----
Print m numbers, i-th of which is equal to the number at index b_{i} after all queries are done.
-----Example-----
Input
6 3 5
1 2 3 4 5 6
2 1 3
2 3 6
1 1 6
2 2 1 5 3
Output
3 3 1 5 2 | import sys
n, q, m = map(int, sys.stdin.buffer.readline().decode("utf-8").split())
a = list(map(int, sys.stdin.buffer.readline().decode("utf-8").split()))
query = [
list(map(int, sys.stdin.buffer.readline().decode("utf-8").split()))
for _ in range(q)
]
b = list(map(int, sys.stdin.buffer.readline().decode("utf-8").split()))
for qt, l, r in reversed(query):
if qt == 1:
for i in range(m):
if l <= b[i] <= r:
b[i] = r if b[i] == l else b[i] - 1
else:
for i in range(m):
if l <= b[i] <= r:
b[i] = r - (b[i] - l)
print(*(a[i - 1] for i in b)) | IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR |
You are given an array a of size n, and q queries to it. There are queries of two types: 1 l_{i} r_{i} — perform a cyclic shift of the segment [l_{i}, r_{i}] to the right. That is, for every x such that l_{i} ≤ x < r_{i} new value of a_{x} + 1 becomes equal to old value of a_{x}, and new value of a_{l}_{i} becomes equal to old value of a_{r}_{i}; 2 l_{i} r_{i} — reverse the segment [l_{i}, r_{i}].
There are m important indices in the array b_1, b_2, ..., b_{m}. For each i such that 1 ≤ i ≤ m you have to output the number that will have index b_{i} in the array after all queries are performed.
-----Input-----
The first line contains three integer numbers n, q and m (1 ≤ n, q ≤ 2·10^5, 1 ≤ m ≤ 100).
The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
Then q lines follow. i-th of them contains three integer numbers t_{i}, l_{i}, r_{i}, where t_{i} is the type of i-th query, and [l_{i}, r_{i}] is the segment where this query is performed (1 ≤ t_{i} ≤ 2, 1 ≤ l_{i} ≤ r_{i} ≤ n).
The last line contains m integer numbers b_1, b_2, ..., b_{m} (1 ≤ b_{i} ≤ n) — important indices of the array.
-----Output-----
Print m numbers, i-th of which is equal to the number at index b_{i} after all queries are done.
-----Example-----
Input
6 3 5
1 2 3 4 5 6
2 1 3
2 3 6
1 1 6
2 2 1 5 3
Output
3 3 1 5 2 | n, q, m = map(int, input().split())
a = list(map(int, input().split()))
qq = list(list(map(int, input().split())) for i in range(q))
qq = list(reversed(qq))
b = list(map(int, input().split()))
for i in range(q):
l = qq[i][1]
r = qq[i][2]
if qq[i][0] == 1:
for j in range(m):
if b[j] < l or b[j] > r:
continue
if b[j] == l:
b[j] = r
else:
b[j] = b[j] - 1
else:
for j in range(m):
if b[j] < l or b[j] > r:
continue
b[j] = l + r - b[j]
for i in range(m):
if i != m - 1:
print(a[b[i] - 1], end=" ")
else:
print(a[b[i] - 1]) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER |
You are given an array a of size n, and q queries to it. There are queries of two types: 1 l_{i} r_{i} — perform a cyclic shift of the segment [l_{i}, r_{i}] to the right. That is, for every x such that l_{i} ≤ x < r_{i} new value of a_{x} + 1 becomes equal to old value of a_{x}, and new value of a_{l}_{i} becomes equal to old value of a_{r}_{i}; 2 l_{i} r_{i} — reverse the segment [l_{i}, r_{i}].
There are m important indices in the array b_1, b_2, ..., b_{m}. For each i such that 1 ≤ i ≤ m you have to output the number that will have index b_{i} in the array after all queries are performed.
-----Input-----
The first line contains three integer numbers n, q and m (1 ≤ n, q ≤ 2·10^5, 1 ≤ m ≤ 100).
The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
Then q lines follow. i-th of them contains three integer numbers t_{i}, l_{i}, r_{i}, where t_{i} is the type of i-th query, and [l_{i}, r_{i}] is the segment where this query is performed (1 ≤ t_{i} ≤ 2, 1 ≤ l_{i} ≤ r_{i} ≤ n).
The last line contains m integer numbers b_1, b_2, ..., b_{m} (1 ≤ b_{i} ≤ n) — important indices of the array.
-----Output-----
Print m numbers, i-th of which is equal to the number at index b_{i} after all queries are done.
-----Example-----
Input
6 3 5
1 2 3 4 5 6
2 1 3
2 3 6
1 1 6
2 2 1 5 3
Output
3 3 1 5 2 | import sys
def input():
return sys.stdin.readline().rstrip()
DXY = [(0, -1), (1, 0), (0, 1), (-1, 0)]
def main():
n, q, m = map(int, input().split())
a = list(map(int, input().split()))
query = [tuple(map(int, input().split())) for i in range(q)]
indices = list(map(int, input().split()))
for t, l, r in query[::-1]:
if t == 1:
for i, idx in enumerate(indices):
if l <= idx <= r:
if idx > l:
indices[i] = idx - 1
else:
indices[i] = r
else:
for i, idx in enumerate(indices):
if l <= idx <= r:
indices[i] = l + r - idx
ans = [a[idx - 1] for idx in indices]
print(*ans)
return 0
main() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR |
You are given an array a of size n, and q queries to it. There are queries of two types: 1 l_{i} r_{i} — perform a cyclic shift of the segment [l_{i}, r_{i}] to the right. That is, for every x such that l_{i} ≤ x < r_{i} new value of a_{x} + 1 becomes equal to old value of a_{x}, and new value of a_{l}_{i} becomes equal to old value of a_{r}_{i}; 2 l_{i} r_{i} — reverse the segment [l_{i}, r_{i}].
There are m important indices in the array b_1, b_2, ..., b_{m}. For each i such that 1 ≤ i ≤ m you have to output the number that will have index b_{i} in the array after all queries are performed.
-----Input-----
The first line contains three integer numbers n, q and m (1 ≤ n, q ≤ 2·10^5, 1 ≤ m ≤ 100).
The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
Then q lines follow. i-th of them contains three integer numbers t_{i}, l_{i}, r_{i}, where t_{i} is the type of i-th query, and [l_{i}, r_{i}] is the segment where this query is performed (1 ≤ t_{i} ≤ 2, 1 ≤ l_{i} ≤ r_{i} ≤ n).
The last line contains m integer numbers b_1, b_2, ..., b_{m} (1 ≤ b_{i} ≤ n) — important indices of the array.
-----Output-----
Print m numbers, i-th of which is equal to the number at index b_{i} after all queries are done.
-----Example-----
Input
6 3 5
1 2 3 4 5 6
2 1 3
2 3 6
1 1 6
2 2 1 5 3
Output
3 3 1 5 2 | import sys
f = lambda: list(map(int, sys.stdin.readline().split()))
q = f()[1]
a = f()
d = [f() for i in range(q)][::-1]
for k in f():
for t, l, r in d:
if l <= k <= r:
k = l + r - k if t == 2 else r if k == l else k - 1
print(a[k - 1]) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.