description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | t = int(input())
for _ in range(t):
n, m = list(map(int, input().split()))
s = input()
if n < 3:
for i in range(m):
a, b = list(map(int, input().split()))
print("NO")
else:
k = [s[0], s[1], s[2]]
if k[0] == k[1] or k[1] == k[2] or k[0] == k[2]:
c = 1
else:
c = 0
ans = [0, c]
for i in range(3, n):
del k[0]
k.append(s[i])
if k[0] == k[1] or k[1] == k[2] or k[0] == k[2]:
c += 1
ans.append(c)
for i in range(m):
a, b = list(map(int, input().split()))
if b - a < 2:
print("NO")
continue
b -= 2
a -= 1
if ans[b] - ans[a] == 0:
print("NO")
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | def isvalid(s):
if s[0] == s[1] or s[1] == s[2] or s[0] == s[2]:
return 1
return 0
t = int(input())
for you in range(t):
l = input().split()
n = int(l[0])
q = int(l[1])
s = input()
count = [(0) for i in range(n - 2)]
fre = 0
for i in range(n - 2):
if isvalid(s[i : i + 3]):
fre += 1
count[i] = fre
for i in range(q):
l = input().split()
L = int(l[0])
R = int(l[1])
if R - L + 1 < 3:
print("NO")
elif L == 1:
if count[R - 3] > 0:
print("YES")
else:
print("NO")
elif count[R - 3] - count[L - 2] > 0:
print("YES")
else:
print("NO") | FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | for _ in range(int(input())):
n, m = map(int, input().split())
s = input()
dp = [0] * (n - 2)
for j in range(n - 2):
st = set(list(s[j : j + 3]))
if len(st) <= 2:
dp[j] = 1
for i in range(1, len(dp)):
dp[i] += dp[i - 1]
dp = [0] + dp
for i in range(m):
u, v = map(int, input().split())
v -= 2
if u > v:
print("NO")
elif dp[v] - dp[u - 1] > 0:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | for _ in range(int(input())):
N, Q = map(int, input().split())
S = input()
Z = [(0) for x in range(N)]
LC = 0
for I in range(2, N):
if S[I - 2] == S[I - 1] or S[I - 2] == S[I] or S[I - 1] == S[I]:
LC += 1
Z[I] = LC
for I in range(Q):
L, R = map(int, input().split())
F = 0
if R - L < 2:
print("NO")
elif Z[R - 1] == Z[L]:
print("NO")
else:
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | tc = int(input())
for _ in range(tc):
n, q = map(int, input().split())
s = input()
girl = [0, 0]
lastCount = 0
for i in range(2, n):
if s[i] == s[i - 1] or s[i] == s[i - 2] or s[i - 1] == s[i - 2]:
lastCount += 1
girl.append(lastCount)
for _ in range(q):
l, r = map(int, input().split())
if r - l < 2:
print("NO")
else:
diff = girl[r - 1] - girl[l]
if diff > 0:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | for t in range(int(input())):
n, q = map(int, input().split())
s = input()
index = [-1] * n
temp = -1
for i in range(n - 2):
if s[i] == s[i + 1] or s[i] == s[i + 2] or s[i + 2] == s[i + 1]:
temp = i
index[i + 2] = temp
for Q in range(q):
l, r = map(int, input().split())
if index[r - 1] >= l - 1:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | def abc(k, a, b):
i = 0
j = len(k) - 1
while i <= j:
mid = i + (j - i) // 2
if k[mid] > b:
j = mid - 1
elif k[mid] < a:
i = mid + 1
else:
return True
return False
t = int(input())
for _ in range(t):
n, q = map(int, input().split())
s = input()
if n <= 2:
for i in range(q):
a, b = map(int, input().split())
print("NO")
continue
k = []
for i in range(n - 2):
a = s[i]
b = s[i + 1]
c = s[i + 2]
if a == b or b == c or a == c:
k.append(i)
for i in range(q):
a, b = map(int, input().split())
a -= 1
b -= 1
if b - a < 2:
print("NO")
else:
flag = abc(k, a, b - 2)
if flag:
print("YES")
else:
print("NO") | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | def getCumulative(string):
cumulative = [0]
for i in range(len(string) - 2):
nextVal = 1 if checkThreeCharsForward(i, string) else 0
cumulative.append(cumulative[-1] + nextVal)
return cumulative
def checkThreeCharsForward(i, s):
return s[i] == s[i + 1] or s[i] == s[i + 2] or s[i + 1] == s[i + 2]
def isRich(left, right, cumulative):
return right - left >= 2 and cumulative[right - 2] - cumulative[left - 1] > 0
for _ in range(int(input())):
_, numQueries = [int(i) for i in input().split()]
cumulative = getCumulative(input())
for _ in range(numQueries):
left, right = [int(i) for i in input().split()]
print("YES" if isRich(left, right, cumulative) else "NO") | FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF RETURN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | def precumpute(s, n):
a = [(0) for i in range(n)]
b = [(0) for i in range(n)]
if n < 3:
return b
for i in range(n - 3, -1, -1):
if s[i] == s[i + 1] or s[i + 1] == s[i + 2] or s[i] == s[i + 2]:
a[i] = 1
prev = -1
for i in range(n - 1, -1, -1):
if a[i] == 1:
prev = i
b[i] = prev
return b
def solve(b, n, l, r):
if n < 3:
print("NO")
return
x = b[l]
if x >= l and x <= r - 2:
print("YES")
else:
print("NO")
def read():
t = int(input())
for j in range(t):
n, q = list(map(int, input().strip().split()))
s = input().strip()
b = precumpute(s, n)
for i in range(q):
l, r = list(map(int, input().strip().split()))
solve(b, n, l - 1, r - 1)
read() | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | t = int(input())
while t:
n, q = map(int, input().split())
s = input()
lol = 0
ll = [0] * n
for i in range(n - 2):
li = []
li.append(s[i])
li.append(s[i + 1])
li.append(s[i + 2])
li.sort()
if li[0] == li[1] or li[1] == li[2]:
lol = 1
ll[i + 2] += 1
for i in range(1, n):
ll[i] = ll[i - 1] + ll[i]
for _ in range(q):
l, r = map(int, input().split())
l -= 1
r -= 1
if (
l + 2 < n
and ll[l + 1] - ll[l] == 1
and ll[l + 1] == ll[r]
and r - l + 1 >= 3
):
print("NO")
elif ll[r] - ll[l] and r - l + 1 >= 3:
print("YES")
else:
print("NO")
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | def compute_rich_substr(s):
rich = [(0) for _ in range(len(s) + 1)]
for i in range(len(s) - 2):
if s[i] == s[i + 1] or s[i] == s[i + 2] or s[i + 1] == s[i + 2]:
rich[i + 1] = rich[i] + 1
else:
rich[i + 1] = rich[i]
rich[len(s) - 1] = rich[len(s)] = rich[len(s) - 2]
return rich
t = int(input())
for _ in range(t):
n, q = list(map(int, input().strip().split()))
s = input()
rich = compute_rich_substr(s)
for _ in range(q):
l, r = list(map(int, input().strip().split()))
if r - l + 1 < 3:
print("NO")
elif rich[r - 2] - rich[l - 1] > 0:
print("YES")
else:
print("NO") | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | for _ in range(int(input())):
n, q = map(int, input().split())
s = input()
repeats = [(0) for i in range(n)]
for i in range(n - 2):
if len(set(s[i : i + 3])) <= 2:
repeats[i] = 1
carr = [(0) for i in range(n + 1)]
le = 0
for i in range(n):
carr[i + 1] = repeats[i] + le
le = carr[i + 1]
for i in range(q):
l, r = map(int, input().split())
if r - l >= 2:
if carr[r - 2] - carr[l - 1] == 0:
print("NO")
else:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | tt = int(input())
while tt:
ans = [0] * 100001
n, q = map(int, input().split())
st = input()
for i in range(2, n):
if st[i] == st[i - 1] or st[i - 1] == st[i - 2] or st[i] == st[i - 2]:
ans[i] = ans[i - 1] + 1
else:
ans[i] = ans[i - 1]
for i in range(q):
fi = 0
l, r = map(int, input().split())
if r - l < 2:
print("NO")
elif ans[r - 1] - ans[l]:
print("YES")
else:
print("NO")
tt -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | t = int(input())
for _ in range(t):
n, q = map(int, input().split())
s = input()
l = [0] * (n - 1)
for i in range(n - 2):
a, b, c = s[i], s[i + 1], s[i + 2]
if len(set([a, b, c])) < 3:
l[i] = l[i - 1] + 1
else:
l[i] = l[i - 1]
for i in range(q):
left, right = map(int, input().split())
left -= 1
right -= 1
if right - left + 1 < 3:
print("NO")
continue
if l[right - 2] - l[left - 1] > 0:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | def isvalid(string):
if string.count(string[0]) == 2 or string[1] == string[2]:
return True
return False
for _ in range(int(input())):
N, Q = list(map(int, input().split()))
s = input()
if N <= 2:
for i in range(Q):
x = input()
print("NO")
continue
if N == 3:
x = isvalid(s)
for i in range(Q):
a, b = list(map(int, input().split()))
if x and a == 1 and b == 3:
print("YES")
else:
print("NO")
continue
valid3s = ["No", "No"]
dp = []
for i in range(N - 2):
if isvalid(s[i : i + 3]):
valid3s.append("Yes")
else:
valid3s.append("No")
i = 2
while i < N:
j = 1
while i < N and valid3s[i] != "Yes":
i += 1
j += 1
for tmp in range(j):
dp.append(i)
i += 1
for i in range(Q):
L, R = list(map(int, input().split()))
L -= 1
R -= 1
if L >= len(dp):
print("NO")
elif dp[L] > R:
print("NO")
else:
print("YES") | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | __author__ = "Ronald Kaiser"
__email__ = "raios dot catodicos at gmail dot com"
for _ in range(int(input())):
N, Q = list(map(int, input().split()))
S = input()
dp = [(-1) for i in range(len(S))]
for i in range(len(S) - 3, -1, -1):
if len(set(S[i : i + 3])) <= 2:
dp[i] = i + 2
elif dp[i + 1] != -1:
dp[i] = dp[i + 1]
for _ in range(Q):
L, R = list(map(int, input().split()))
L -= 1
R -= 1
if dp[L] != -1 and dp[L] <= R:
print("YES")
else:
print("NO") | ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | for _ in range(int(input())):
n, q = map(int, input().split())
a = input()
b = [0]
for i in range(n - 2):
if a[i] == a[i + 1] or a[i + 1] == a[i + 2] or a[i] == a[i + 2]:
b.append(b[-1] + 1)
else:
b.append(b[-1] + 0)
for i in range(q):
l, r = map(int, input().split())
if r - l < 2:
print("NO")
elif b[r - 2] - b[l - 1] > 0:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | t = int(input())
while t > 0:
x, q = input().split()
x = int(x)
q = int(q)
s = input()
list = []
list.append(0)
list.append(0)
sum = 0
ptr = 0
for i in s:
if ptr + 2 > len(s) - 1:
break
else:
a = s[ptr]
b = s[ptr + 1]
c = s[ptr + 2]
if (a == b) | (b == c) | (a == c):
sum += 1
list.append(sum)
ptr += 1
while q > 0:
l, r = input().split()
l = int(l)
r = int(r)
if r - l < 2:
print("NO")
else:
res = list[r - 1] - list[l]
if res > 0:
print("YES")
else:
print("NO")
q -= 1
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | for _ in range(int(input())):
n, q = map(int, input().split())
s = input()
prefix = [0] * n
for i in range(2, n):
prefix[i] = prefix[i - 1]
if s[i] == s[i - 1] or s[i - 1] == s[i - 2] or s[i] == s[i - 2]:
prefix[i] += 1
for i in range(q):
l, r = map(int, input().split())
if r - l + 1 < 3:
print("NO")
elif prefix[r - 1] - prefix[l] > 0:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | for tea in range(int(input())):
n, q = map(int, input().split())
s = list(input())
if n <= 2:
for qq in range(q):
input()
print("NO")
continue
gud = []
for i in range(n - 2):
if len(set([s[i], s[i + 1], s[i + 2]])) < 3:
gud.append(i)
stuff = dict()
ind = 0
for i in range(n):
stuff[i] = ind
if ind < len(gud) and i >= gud[ind]:
ind += 1
for qq in range(q):
l, r = [(int(x) - 1) for x in input().split()]
r -= 2
if l > r:
print("NO")
continue
if stuff[l] == stuff[r + 1]:
print("NO")
else:
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | for _ in range(int(input())):
n, q = map(int, input().split())
s = list(input())
t = 0
li = []
for i in range(n - 2):
li.append(t)
if s[i] == s[i + 1] or s[i] == s[i + 2] or s[i + 1] == s[i + 2]:
t += 1
li.append(t)
li.append(0)
li.append(0)
for _ in range(q):
l, r = map(int, input().split())
l -= 1
r -= 1
if r - l >= 2:
if li[r - 1] - li[l] > 0:
print("YES")
else:
print("NO")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | n = int(input())
for i in range(0, n):
p = input().rstrip().split(" ")
s = input().rstrip()
x = list(s)
q = []
w = []
S = []
D = []
if len(x) < 3:
for j in range(0, int(p[1])):
G = input().rstrip().split(" ")
print("NO")
else:
l = [0] * 26
for j in range(0, 3):
A = ord(x[j]) - 97
l[A] += 1
for j in range(0, 26):
if l[j] > 1:
q.append(1)
break
for j in range(1, len(x) - 3 + 1):
A = ord(x[j - 1]) - 97
l[A] -= 1
B = ord(x[j + 3 - 1]) - 97
l[B] += 1
for k in range(0, 26):
if l[k] > 1:
q.append(j + 1)
break
for j in range(0, int(p[1])):
G = input().rstrip().split(" ")
if int(G[1]) - int(G[0]) + 1 < 3:
print("NO")
else:
I = 0
J = len(q) - 1
H = 0
while I <= J:
m = (I + J) // 2
if int(q[m]) >= int(G[0]) and int(q[m]) + 2 <= int(G[1]):
H = 1
break
elif int(q[m]) < int(G[0]):
I = m + 1
else:
J = m - 1
if H == 0:
print("NO")
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | t = int(input())
for i in range(t):
nq = input().split(" ")
n, q = int(nq[0]), int(nq[1])
s = input()
a = [(0) for i in range(len(s))]
b = [(0) for i in range(len(s))]
for j in range(len(s) - 2):
if s[j] == s[j + 1] or s[j] == s[j + 2] or s[j + 1] == s[j + 2]:
a[j] = 1
prev = -1
for j in range(len(s) - 1, -1, -1):
if a[j] != 0:
prev = j
b[j] = prev
for j in range(q):
ab = input().split(" ")
x, y = int(ab[0]) - 1, int(ab[1]) - 1
if b[x] == -1:
print("NO")
continue
if b[x] + 2 <= y:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | def list_inp():
return [int(val.strip()) for val in input().split()]
def main():
T = list_inp()[0]
while T:
T -= 1
N, Q = list_inp()
inp_str = input().strip()
good_subs_3 = [(0) for _ in range(N)]
for i in range(len(inp_str) - 3 + 1):
if i > 0:
good_subs_3[i] = good_subs_3[i - 1]
if (
inp_str[i] == inp_str[i + 1]
or inp_str[i] == inp_str[i + 2]
or inp_str[i + 1] == inp_str[i + 2]
):
good_subs_3[i] += 1
while Q:
Q -= 1
l, r = list_inp()
l -= 1
r -= 1
if l > r - 2:
print("NO")
else:
good_count = good_subs_3[r - 2]
if l > 0:
good_count -= good_subs_3[l - 1]
if good_count > 0:
print("YES")
else:
print("NO")
main() | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | for _ in range(int(input())):
n, q = map(int, input().split())
st = input()
freq = [0]
count = 0
for i in range(n - 2):
if len(set(st[i : i + 3])) < 3:
count += 1
freq.append(count)
for _ in range(q):
l, r = map(int, input().split())
if r - l < 2:
print("NO")
continue
if r - l == 2:
if freq[l] - freq[l - 1] > 0:
print("YES")
else:
print("NO")
continue
if freq[r - 2] - freq[l] > 0 or freq[l] - freq[l - 1] > 0:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | R = lambda: map(int, input().split())
for _ in range(int(input())):
n, q = R()
s = input()
L = []
for i in range(n - 2):
if s[i] == s[i + 1] or s[i] == s[i + 2] or s[i + 1] == s[i + 2]:
L.append(1)
else:
L.append(0)
for i in range(1, len(L)):
L[i] += L[i - 1]
L = [0] + L
for j in range(q):
l, r = R()
r -= 2
if l > r:
print("NO")
elif L[r] - L[l - 1] > 0:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | from itertools import accumulate
t = int(input())
while t:
n, q = [int(x) for x in input().split()]
s = input()
if n < 3:
for _ in range(q):
l, r = [int(x) for x in input().split()]
print("NO")
else:
temp = [0] * (n - 2)
for i in range(n - 2):
if s[i] == s[i + 1] or s[i + 2] == s[i + 1] or s[i] == s[i + 2]:
temp[i] = 1
temp = list(accumulate(temp))
for _ in range(q):
l, r = [int(x) for x in input().split()]
if r - l < 2:
print("NO")
continue
r -= 3
if l == 1:
if temp[r] > 0:
print("YES")
else:
print("NO")
elif temp[r] - temp[l - 2] > 0:
print("YES")
else:
print("NO")
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | t = int(input())
for _ in range(t):
n, q = [int(p) for p in input().split()]
s = input()
l = [0] * n
if n > 2:
for i in range(n - 2):
v1 = s[i]
v2 = s[i + 1]
v3 = s[i + 2]
if v1 == v2 or v2 == v3 or v1 == v3:
l[i] = 1
else:
l[i] = 0
li = [0] * n
val = 0
for i in range(n):
val += l[i]
li[i] = val
for i in range(q):
ll, hl = [int(p) for p in input().split()]
if hl - ll + 1 >= 3:
if ll > 1:
ans = li[hl - 3] - li[ll - 2]
else:
ans = li[hl - 3]
else:
ans = 0
if ans < 1:
print("NO")
else:
print("YES")
else:
for i in range(q):
ll, hl = [int(p) for p in input().split()]
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | t = int(input())
for _ in range(t):
n, q = [int(i) for i in input().split()]
string = input()
richBinaryList = [(False) for _ in range(len(string))]
leastRightList = [(0) for _ in range(len(string))]
for i in range(len(string) - 2):
first = string[i]
second = string[i + 1]
third = string[i + 2]
if first == second or first == third or second == third:
richBinaryList[i] = True
lastRich = None
for i in range(len(string) - 1, -1, -1):
if richBinaryList[i]:
lastRich = i
if lastRich is None:
leastRight = None
else:
leastRight = lastRich + 2
leastRightList[i] = leastRight
for _ in range(q):
l, r = [(int(i) - 1) for i in input().split()]
length = r - l + 1
if length < 3:
print("NO")
elif leastRightList[l] is None:
print("NO")
elif leastRightList[l] <= r:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR NONE EXPR FUNC_CALL VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
A string with length $L$ is called rich if $L \ge 3$ and there is a character which occurs in this string strictly more than $L/2$ times.
You are given a string $S$ and you should answer $Q$ queries on this string. In each query, you are given a substring $S_L, S_{L+1}, \ldots, S_R$. Consider all substrings of this substring. You have to determine whether at least one of them is rich.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$.
- The second line contains a single string $S$ with length $N$.
- Each of the next $Q$ lines contains two space-separated integers $L$ and $R$ describing a query.
-----Output-----
For each query, print a single line containing the string "YES" if the given substring contains a rich substring or "NO" if it does not contain any rich substring.
-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le L \le R \le N$
- $S$ contains only lowercase English letters
-----Example Input-----
1
10 2
helloworld
1 3
1 10
-----Example Output-----
NO
YES | T = int(input())
for test in range(T):
N, Q = [int(v) for v in input().split()]
S = input()
start_points = [(len(set(S[i : i + 3])) < 3) for i in range(N - 2)]
if N < 3:
count = []
else:
counts = [int(start_points[0])] + [0] * (N - 3)
for i in range(1, N - 2):
counts[i] = counts[i - 1] + start_points[i]
for query in range(Q):
L, R = [int(v) for v in input().split()]
if L + 2 > R:
print("NO")
continue
if L == 1:
left = 0
else:
left = counts[L - 2]
right = counts[R - 3]
if left < right:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def __init__(self):
self.arr = []
self.tree = None
def inorder(self, root, mini, maxi):
if root.left != None:
self.inorder(root.left, mini, maxi)
if root.data >= mini and root.data <= maxi:
self.arr.append(root.data)
if root.right != None:
self.inorder(root.right, mini, maxi)
def makebst(self, arr, low, high, node=None, side="ro"):
if low > high:
return
mid = low + (high - low) // 2
nde = Node(arr[mid])
if side == "ro":
self.tree = nde
if side == "l":
node.left = nde
if side == "r":
node.right = nde
self.makebst(arr, low, mid - 1, nde, "l")
self.makebst(arr, mid + 1, high, nde, "r")
def removekeys(self, root, l, r):
self.inorder(root, l, r)
low = 0
high = len(self.arr) - 1
self.makebst(self.arr, low, high)
return self.tree | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NONE FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF NONE STRING IF VAR VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | def f(node, l, r):
if node is None:
return None
node.left = f(node.left, l, r)
node.right = f(node.right, l, r)
if node.data < l:
out = node.right
node = None
return out
if node.data > r:
out = node.left
node = None
return out
return node
class Solution:
def removekeys(self, root, l, r):
return f(root, l, r) | FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def helper(self, node, l, r):
if node == None:
return node
h1 = self.helper(node.left, l, r)
h2 = self.helper(node.right, l, r)
node.left = None
node.right = None
if node.data < l or node.data > r:
if h1 == None and h2 == None:
return None
if h1 == None:
return h2
if h2 == None:
return h1
cur = h2
while cur.left != None:
cur = cur.left
cur.left = h1
return h2
if h1 == None and h2 == None:
return node
if h2 == None:
node.left = h1
return node
if h1 == None:
node.right = h2
return node
node.left = h1
node.right = h2
return node
def removekeys(self, root, l, r):
return self.helper(root, l, r) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE IF VAR VAR VAR VAR IF VAR NONE VAR NONE RETURN NONE IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, Min, Max):
if root == None:
return None
root.left = self.removekeys(root.left, Min, Max)
root.right = self.removekeys(root.right, Min, Max)
if root.data < Min:
rChild = root.right
return rChild
if root.data > Max:
lChild = root.left
return lChild
return root | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR RETURN VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def create(self, l1, r1, arr2):
if l1 > r1:
return None
m = (l1 + r1) // 2
root = Node(arr2[m])
root.left = self.create(l1, m - 1, arr2)
root.right = self.create(m + 1, r1, arr2)
return root
def inorder(self, root, arr):
if root == None:
return None
self.inorder(root.left, arr)
arr.append(root.data)
self.inorder(root.right, arr)
def removekeys(self, root, l, r):
arr1 = []
arr2 = []
self.inorder(root, arr1)
for i in arr1:
if i <= r and i >= l:
arr2.append(i)
l1 = 0
r1 = len(arr2) - 1
return self.create(l1, r1, arr2) | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def inot(self, root, l, r):
if not root:
return []
st = []
res = []
curr = root
while True:
if curr:
st.append(curr)
curr = curr.left
elif st:
curr = st.pop()
if curr.data in range(l, r + 1):
res.append(curr.data)
curr = curr.right
else:
break
root1 = None
for elem in res:
root1 = self.constructBST(root1, elem)
return root1
def hbBST(self, arr, s, e):
if s > e:
return
m = (s + e) // 2
root = Node(arr[m])
root.left = self.hbBST(arr, s, m - 1)
root.right = self.hbBST(arr, m + 1, e)
return root
def constructBST(self, root, item):
if not root:
return Node(item)
if item < root.data:
root.left = self.constructBST(root.left, item)
elif item > root.data:
root.right = self.constructBST(root.right, item)
return root
def removekeys(self, root, l, r):
if not root:
return
root.left = self.removekeys(root.left, l, r)
root.right = self.removekeys(root.right, l, r)
if root.data < l:
return root.right
elif root.data > r:
return root.left
return root | CLASS_DEF FUNC_DEF IF VAR RETURN LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR WHILE NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN VAR RETURN VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, r):
def dfs(root, l, r):
if not root:
return
root.left = dfs(root.left, l, r)
root.right = dfs(root.right, l, r)
if root.data < l:
return root.right
elif root.data > r:
return root.left
return root
return dfs(root, l, r) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | def fun(root, mini, maxi):
if root == None:
return None
elif root.data < mini:
while root != None and root.data < mini:
root = root.right
return fun(root, mini, maxi)
elif root.data > maxi:
while root != None and root.data > maxi:
root = root.left
return fun(root, mini, maxi)
else:
root.left = fun(root.left, mini, maxi)
root.right = fun(root.right, mini, maxi)
return root
class Solution:
def removekeys(self, root, l, r):
return fun(root, l, r) | FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR WHILE VAR NONE VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR WHILE VAR NONE VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, r):
if root is None:
return
root.left = self.removekeys(root.left, l, r)
root.right = self.removekeys(root.right, l, r)
if root.data not in range(l, r + 1):
if root.data < l:
return root.right
elif root.data > l:
return root.left
return root | CLASS_DEF FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN VAR RETURN VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, r):
if root != None:
if root.data < l:
root = self.removekeys(root.right, l, r)
elif root.data > r:
root = self.removekeys(root.left, l, r)
else:
left = self.removekeys(root.left, l, r)
right = self.removekeys(root.right, l, r)
root.left = left
root.right = right
return root | CLASS_DEF FUNC_DEF IF VAR NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, r):
if not root:
return
root.left = self.removekeys(root.left, l, r)
root.right = self.removekeys(root.right, l, r)
if root.data < l or root.data > r:
root = delete(root)
return root
def delete(root):
if not root.left and not root.right:
root = None
elif not root.left:
root = root.right
elif not root.right:
root = root.left
else:
temp = find(root.left)
root.data = temp.data
root = delete(temp)
return root
def find(root):
while root.right:
root = root.right
return root | CLASS_DEF FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR NONE IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR RETURN VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, r):
def f(root, l, r):
if not root:
return root
if root.data >= l and root.data <= r:
root.left = f(root.left, l, r)
root.right = f(root.right, l, r)
elif root.data < l:
return f(root.right, l, r)
else:
return f(root.left, l, r)
return root
return f(root, l, r) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def postorder(self, root, parent, brange, order):
if root is None:
return
self.postorder(root.left, root, brange, order)
self.postorder(root.right, root, brange, order)
rem = root.data < brange[0] or root.data > brange[1]
order.append([root, parent, rem])
def removeNodes(self, root, path):
for n in path:
if n[2]:
parent = n[1]
node = n[0]
if parent is None:
if node.left and node.right:
print("both left and right nodes - impossible to remove")
return None
if node.left:
root = node.left
node.left = None
elif node.right:
root = node.right
node.right = None
else:
root = None
else:
pattach = "?"
if parent.right and parent.right.data == node.data:
pattach = "R"
elif parent.left and parent.left.data == node.data:
pattach = "L"
if pattach == "R":
if node.right:
parent.right = node.right
node.right = None
elif node.left:
parent.right = node.left
node.left = None
else:
parent.right = None
elif pattach == "L":
if node.right:
parent.left = node.right
node.right = None
elif node.left:
parent.left = node.left
node.left = None
else:
parent.left = None
if node.left:
if pattach == "R":
parent.right.left = node.left
node.left = None
elif pattach == "L":
parent.left.left = node.left
node.left = None
return root
def removekeys(self, root, l, r):
brange = [l, r]
path = []
self.postorder(root, None, brange, path)
return self.removeNodes(root, path) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR FUNC_DEF FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NONE IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN NONE IF VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR STRING IF VAR VAR VAR ASSIGN VAR STRING IF VAR VAR VAR ASSIGN VAR STRING IF VAR STRING IF VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE IF VAR STRING IF VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE IF VAR IF VAR STRING ASSIGN VAR VAR ASSIGN VAR NONE IF VAR STRING ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NONE VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, r):
return self.helper(root, l, r)
def helper(self, root, l, r):
if root is None:
return None
if root.data < l:
return self.helper(root.right, l, r)
elif root.data > r:
return self.helper(root.left, l, r)
else:
left_node = self.helper(root.left, l, r)
right_node = self.helper(root.right, l, r)
root.left = left_node
root.right = right_node
return root | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, r):
def remove(root, l, r):
if root == None:
return None
root.left = remove(root.left, l, r)
root.right = remove(root.right, l, r)
if root.data < l:
child = root.right
return child
elif root.data > r:
child = root.left
return child
return root
root = remove(root, l, r)
return root | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, r):
def trim(node):
if not node:
return None
elif node.data > r:
return trim(node.left)
elif node.data < l:
return trim(node.right)
else:
node.left = trim(node.left)
node.right = trim(node.right)
return node
return trim(root) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NONE IF VAR VAR RETURN FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, r):
head = None
head = self.inorder(root, head, l, r)
return head
def inorder(self, root, head, l, r):
if not root:
return
if l <= root.data <= r:
head = root
head.left = self.inorder(root.left, head.left, l, r)
head.right = self.inorder(root.right, head.right, l, r)
return head
elif r < root.data:
head = self.inorder(root.left, head, l, r)
return head
elif l > root.data:
head = self.inorder(root.right, head, l, r)
return head | CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, r):
while root and (l > root.data or r < root.data):
if root.data > r:
root = root.left
elif root.data < l:
root = root.right
self.helper(root, l, r)
return root
def helper(self, root, l, r):
if root is None:
return
while root.right and root.right.data > r:
root.right = root.right.left
while root.left and root.left.data < l:
root.left = root.left.right
self.helper(root.left, l, r)
self.helper(root.right, l, r)
return | CLASS_DEF FUNC_DEF WHILE VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN WHILE VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, r):
def _replace(par, nd, sub):
if par.left == nd:
par.left = sub
else:
par.right = sub
def _solve(nd, par):
if not nd:
return
if nd.data < l:
_replace(par, nd, nd.right)
_solve(nd.right, par)
elif nd.data > r:
_replace(par, nd, nd.left)
_solve(nd.left, par)
else:
_solve(nd.left, nd)
_solve(nd.right, nd)
guard = Node(-(10**9))
guard.right = root
nd = root
_solve(nd, guard)
return guard.right | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def f(self, root, l, r):
if root == None:
return root
if root.data >= l and root.data <= r:
x = self.f(root.left, l, r)
y = self.f(root.right, l, r)
root.left = x
root.right = y
return root
if root.data > r:
x = self.f(root.left, l, r)
root.left = x
root.right = None
return x
if root.data < l:
x = self.f(root.right, l, r)
root.left = None
root.right = x
return x
def removekeys(self, root, l, r):
x = self.f(root, l, r)
return x | CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, h):
if root == None:
return None
if root.data > h:
return self.removekeys(root.left, l, h)
if root.data < l:
return self.removekeys(root.right, l, h)
root.left = self.removekeys(root.left, l, h)
root.right = self.removekeys(root.right, l, h)
return root | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a Binary Search Tree (BST) and a range [min, max], remove all keys which are outside the given range. The modified tree should also be BST.
Example 1:
Input:
Range = [-10, 13]
Output:
-8 6 7 13
Explanation:
Nodes with values -13, 14 and 15 are
outside the given range and hence are
removed from the BST.
This is the resultant BST and it's
inorder traversal is -8 6 7 13.
Example 2:
Input:
Range = [2, 6]
14
/ \
4 16
/ \ /
2 8 15
/ \ / \
-8 3 7 10
Output:
2 3 4
Explanation:
After removing nodes outside the range,
the resultant BST looks like:
4
/
2
\
3
The inorder traversal of this tree is: 2 3 4
Your task:
You don't have to read input or print anything. Your task is to complete the function removekeys() which takes the root node of the BST and the range as input and returns the root of the modified BST after removing the nodes outside the given range.
Expected Time Complexity: O(number of nodes)
Expected Auxiliary Space: O(h)
Constraints:
1 β€ Number of Nodes β€ 10^{5}
1 β€ K β€ 10^{5} | class Solution:
def removekeys(self, root, l, r):
if root is None:
return
root.left = self.removekeys(root.left, l, r)
root.right = self.removekeys(root.right, l, r)
if root.data < l:
if root.right is not None:
root = root.right
else:
root = None
if root and root.data > r:
if root.left is not None:
root = root.left
else:
root = None
return root | CLASS_DEF FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE IF VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR |
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 β€ Li, Ri β€ INT_MAX, 0 < Hi β€ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .
The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.
For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].
Notes:
The number of buildings in any input list is guaranteed to be in the range [0, 10000].
The input list is already sorted in ascending order by the left x position Li.
The output list must be sorted by the x position.
There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...] | class Solution:
def getSkyline(self, blds):
if not blds:
return []
if len(blds) == 1:
return [[blds[0][0], blds[0][2]], [blds[0][1], 0]]
mid = len(blds) >> 1
left = self.getSkyline(blds[:mid])
right = self.getSkyline(blds[mid:])
return self.merge(left, right)
def merge(self, left, right):
res = []
h1 = h2 = 0
while left and right:
if left[0][0] < right[0][0]:
pos, h1 = left.pop(0)
elif right[0][0] < left[0][0]:
pos, h2 = right.pop(0)
else:
pos, h1 = left.pop(0)
h2 = right.pop(0)[1]
h = max(h1, h2)
if not res or res[-1][1] != h:
res.append([pos, h])
if left:
res += left
if right:
res += right
return res | CLASS_DEF FUNC_DEF IF VAR RETURN LIST IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST VAR NUMBER NUMBER VAR NUMBER NUMBER LIST VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR VAR IF VAR VAR VAR RETURN VAR |
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 β€ Li, Ri β€ INT_MAX, 0 < Hi β€ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .
The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.
For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].
Notes:
The number of buildings in any input list is guaranteed to be in the range [0, 10000].
The input list is already sorted in ascending order by the left x position Li.
The output list must be sorted by the x position.
There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...] | class Solution(object):
def getSkyline(self, buildings):
cp = set([b[0] for b in buildings] + [b[1] for b in buildings])
i, active, res = 0, [], []
for c in sorted(cp):
while i < len(buildings) and buildings[i][0] <= c:
heapq.heappush(active, (-buildings[i][2], buildings[i][1]))
i += 1
while active and active[0][1] <= c:
heapq.heappop(active)
h = len(active) and -active[0][0]
if not res or h != res[-1][1]:
res.append([c, h])
return res | CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER LIST LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR |
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 β€ Li, Ri β€ INT_MAX, 0 < Hi β€ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .
The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.
For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].
Notes:
The number of buildings in any input list is guaranteed to be in the range [0, 10000].
The input list is already sorted in ascending order by the left x position Li.
The output list must be sorted by the x position.
There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...] | class Solution:
def getSkyline(self, buildings):
edges = []
for left, right, height in buildings:
edges.append((left, -height, right))
edges.append((right, 0, None))
edges.sort()
pq = [(0, float("inf"))]
skyline = [(0, 0)]
print(edges)
for left, negheight, right in edges:
while pq[0][1] <= left:
heapq.heappop(pq)
if negheight:
heapq.heappush(pq, (negheight, right))
if skyline[-1][1] != -pq[0][0]:
skyline.append([left, -pq[0][0]])
return skyline[1:] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NONE EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER RETURN VAR NUMBER |
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 β€ Li, Ri β€ INT_MAX, 0 < Hi β€ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .
The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.
For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].
Notes:
The number of buildings in any input list is guaranteed to be in the range [0, 10000].
The input list is already sorted in ascending order by the left x position Li.
The output list must be sorted by the x position.
There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...] | class Solution:
def getSkyline(self, buildings):
edges = []
events = collections.defaultdict(list)
for left, right, height in buildings:
edges.append((left, -height, right))
edges.append((right, 0, None))
pq = [(0, float("inf"))]
edges.sort()
skyline = [(0, 0)]
for edge, height, right in edges:
while edge >= pq[0][1]:
heapq.heappop(pq)
if height:
heapq.heappush(pq, (height, right))
if skyline[-1][1] != -pq[0][0]:
skyline.append([edge, -pq[0][0]])
return skyline[1:] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NONE ASSIGN VAR LIST NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR VAR VAR WHILE VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER RETURN VAR NUMBER |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def helper(self, node):
if node is None:
return
self.helper(node.right)
temp = node.data
node.data = self.data
self.data += temp
self.helper(node.left)
def transformTree(self, root):
self.data = 0
self.helper(root) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | def fun(root, carry):
if root == None:
return
if root.left == None and root.right == None:
temp = root.data
root.data = carry[0]
carry[0] += temp
else:
r = fun(root.right, carry)
temp = root.data
root.data = carry[0]
carry[0] += temp
fun(root.left, carry)
class Solution:
def transformTree(self, root):
carry = [0]
fun(root, carry)
return root | FUNC_DEF IF VAR NONE RETURN IF VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
self.nums = 0
def func(root):
if root is None:
return
func(root.right)
node = root.data
root.data = self.nums
self.nums += node
func(root.left)
func(root)
return self.nums | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
def dfs(root):
if not root:
return
dfs(root.right)
t = self.sum
self.sum += +root.data
root.data = t
dfs(root.left)
self.sum = 0
dfs(root) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def inorder(self, root, l):
if root == None:
return []
else:
self.inorder(root.left, l)
l.append(root.data)
self.inorder(root.right, l)
return l
def solve(self, root, d):
if root == None:
return None
else:
root.data = d[root.data]
l = self.solve(root.left, d)
r = self.solve(root.right, d)
return root
def transformTree(self, root):
l = []
self.inorder(root, l)
s = sum(l)
d = {}
for i in l:
d[i] = d.get(i, 0) + (s - i)
s = s - i
return self.solve(root, d) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def __init__(self):
self.total = 0
def inorder(self, root):
if root.left != None:
self.inorder(root.left)
self.total += root.data
if root.right != None:
self.inorder(root.right)
def setvalues(self, root):
if root.left != None:
self.setvalues(root.left)
self.total -= root.data
root.data = self.total
if root.right != None:
self.setvalues(root.right)
def transformTree(self, root):
self.inorder(root)
self.setvalues(root) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
global sm
sm = 0
self.updateValue(root)
def updateValue(self, current):
global sm
if not current:
return
self.updateValue(current.right)
temp = current.data
current.data = sm
sm += temp
self.updateValue(current.left) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
num = self.sum(root)
return self._transform(root, [num])
def _transform(self, node, num):
if node:
self._transform(node.left, num)
node.data, num[0] = num[0] - node.data, num[0] - node.data
self._transform(node.right, num)
def sum(self, node):
if node is None:
return 0
return node.data + self.sum(node.left) + self.sum(node.right) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR LIST VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def inorder_ok(self, root, arr):
if root:
self.inorder_ok(root.left, arr)
arr.append(root.data)
self.inorder_ok(root.right, arr)
def inorder(self, root, arr):
if root:
self.inorder(root.right, arr)
root.data = arr.pop()
self.inorder(root.left, arr)
def transformTree(self, root):
arr = list()
self.inorder_ok(root, arr)
temp = arr[-1]
arr[-1] = 0
for i in range(len(arr) - 2, -1, -1):
temp2 = arr[i]
arr[i] = arr[i + 1] + temp
temp = temp2
self.inorder(root, arr)
return root | CLASS_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
sorted = []
def solve(node):
if node is None:
return
else:
l = solve(node.left)
sorted.append(node.data)
r = solve(node.right)
solve(root)
suff = [0] * len(sorted)
s = 0
for i in range(len(sorted) - 1, -1, -1):
s += sorted[i]
suff[i] = s
suff.append(0)
curr = [0]
def populate(node):
if node is None:
return 0
populate(node.left)
node.data = suff[curr[0] + 1]
curr[0] += 1
populate(node.right)
populate(root) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
s = 0
def solve(self, root):
if root == None:
return
else:
self.solve(root.right)
self.s = self.s + root.data
root.data = self.s - root.data
self.solve(root.left)
return root
def transformTree(self, root):
self.solve(root)
return root | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
def traversal(root):
nonlocal sum1
if root == None:
return None
if root.right:
traversal(root.right)
temp = root.data
root.data = sum1
sum1 += temp
if root.left:
traversal(root.left)
sum1 = 0
traversal(root)
return root | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
stck = []
current = root
temp = 0
while True:
if current is not None:
stck.append(current)
current = current.right
elif stck:
current = stck.pop()
tmp = current.data
current.data = temp
temp += tmp
current = current.left
else:
break
return root | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
def get_sum(root):
if root == None:
return 0
return root.data + get_sum(root.left) + get_sum(root.right)
def helper(root, sum):
if not root:
return sum
sum = helper(root.left, sum) - root.data
root.data = sum
return helper(root.right, sum)
helper(root, get_sum(root)) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
pre_sum = []
def inorder(root):
if not root:
return
inorder(root.left)
pre_sum.append(root.data)
inorder(root.right)
return
inorder(root)
v = 0
for i in range(len(pre_sum) - 1, -1, -1):
temp = pre_sum[i]
pre_sum[i] = v
v += temp
i = 0
def func(root):
nonlocal i
if not root:
return
func(root.left)
root.data = pre_sum[i]
i += 1
func(root.right)
return root
return func(root) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def ReverseInorder(self, root, sum):
if root == None:
return
self.ReverseInorder(root.right, sum)
val = sum[0] + root.data
root.data = sum[0]
sum[0] = val
self.ReverseInorder(root.left, sum)
def transformTree(self, root):
sum = [0]
self.ReverseInorder(root, sum)
return root | CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
def my(root):
if not root:
return
my(root.left)
res.append(root.data)
my(root.right)
def my_(root):
if not root:
return
my_(root.left)
root.data = res[0]
res.pop(0)
my_(root.right)
res = []
my(root)
s = sum(res)
su = 0
for i in range(len(res)):
su += res[i]
res[i] = s - su
my_(root)
return root | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def ino(self, r, l):
if r is None:
return
self.ino(r.left, l)
l.append(r)
self.ino(r.right, l)
def transformTree(self, root):
l = []
self.ino(root, l)
s = 0
n = len(l)
for i in range(n - 1, -1, -1):
g = l[i].data
l[i].data = s
s += g | CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
add, temp, stack = 0, root, [root]
d = {root: True}
while stack:
while stack[-1].right and stack[-1].right not in d:
d[stack[-1].right] = True
stack.append(stack[-1].right)
a = stack.pop()
a.data, add = add, add + a.data
if a.left:
stack.append(a.left)
return root | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR LIST VAR ASSIGN VAR DICT VAR NUMBER WHILE VAR WHILE VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
count_, tempList = [0], self.inOrder(root)
newTemp = [0] * len(tempList)
for i in range(len(tempList) - 2, -1, -1):
newTemp[i] = tempList[i + 1] + newTemp[i + 1]
self.inOrderChng(root, newTemp, count_)
def inOrderChng(self, root, arr, count):
if root == None:
return
self.inOrderChng(root.left, arr, count)
root.data = arr[count[0]]
count[0] += 1
self.inOrderChng(root.right, arr, count)
def inOrder(self, root):
if root == None:
return []
return self.inOrder(root.left) + [root.data] + self.inOrder(root.right) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
def helper(root, a):
if root is None:
return a
b = helper(root.right, a)
c = root.data
root.data = b
d = helper(root.left, b + c)
return d
helper(root, 0) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR NUMBER |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
def inorder(root, arr):
if not root:
return
else:
inorder(root.left, arr)
arr.append(root.data)
inorder(root.right, arr)
return arr
arr = inorder(root, [])
j = 0
for i in range(len(arr) - 1, -1, -1):
temp = arr[i]
arr[i] = j
j = j + temp
def transform(root, arr, k):
if not root:
return
else:
transform(root.left, arr, k)
root.data = arr[k[0]]
k[0] = k[0] + 1
transform(root.right, arr, k)
transform(root, arr, [0])
return root | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST NUMBER RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def solve(self, root, q):
if root is None:
return
self.solve(root.left, q)
q.append(root.data)
self.solve(root.right, q)
def solve1(self, root, q):
if root is None:
return
self.solve1(root.left, q)
root.data = q[self.cnt]
self.cnt += 1
self.solve1(root.right, q)
def transformTree(self, root):
q = []
self.solve(root, q)
sm = 0
temp = q[-1]
q[-1] = 0
for i in range(len(q) - 1):
sm += temp
temp = q[-i - 2]
q[-i - 2] = sm
self.cnt = 0
self.solve1(root, q) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def rec(self, root, t):
if not root:
return 0
self.rec(root.right, t)
t[0] += root.data
root.data = t[0] - root.data
self.rec(root.left, t)
def transformTree(self, root):
t = [0]
self.rec(root, t) | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
a = 0
def f(self, root):
if root == None:
return None
self.f(root.right)
if Solution.a == 0:
x = root.data
root.data = 0
else:
x = root.data
root.data = Solution.a
Solution.a += x
self.f(root.left)
def transformTree(self, root):
Solution.a = 0
x = root
self.f(root)
return x | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | import sys
class Solution:
def transformTree(self, root):
if not root:
return root
current = root
tmp = 0
successor = -sys.maxsize - 1
while current:
if current.right == None:
if successor == -sys.maxsize - 1:
successor = current.data
current.data = 0
else:
tmp = current.data
current.data = successor
successor += tmp
current = current.left
else:
right = current.right
while right.left and right.left != current:
right = right.left
if not right.left:
right.left = current
current = current.right
else:
right.left = None
tmp = current.data
current.data = successor
successor += tmp
current = current.left | IMPORT CLASS_DEF FUNC_DEF IF VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR IF VAR NONE IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
self.tot = 0
self.tmp = 0
def reverse_inorder(root):
if root:
reverse_inorder(root.right)
self.tot += root.data
root.data = self.tmp
self.tmp = self.tot
reverse_inorder(root.left)
reverse_inorder(root) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
arr = []
self.fillArr(root, arr)
runningSum = 0
for i in reversed(range(len(arr))):
tmp = arr[i]
arr[i] = runningSum
runningSum += tmp
self.setBST(root, arr, [0])
def fillArr(self, root, arr):
if root is None:
return
self.fillArr(root.left, arr)
arr.append(root.data)
self.fillArr(root.right, arr)
def setBST(self, root, arr, index):
if root is None:
return
self.setBST(root.left, arr, index)
root.data = arr[index[0]]
index[0] += 1
self.setBST(root.right, arr, index) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
sm = 0
def transformTree(self, root):
if not root:
return
if root.right:
self.transformTree(root.right)
curr = root.data
root.data = self.sm
self.sm += curr
if root.left:
self.transformTree(root.left) | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
self.t = 0
def dfs(root):
if root is None:
return 0
dfs(root.right)
self.t = self.t + root.data
root.data = self.t - root.data
dfs(root.left)
return dfs(root) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | rsm = 0
def rev_inord(root, rsm):
if root is None:
return
if root.right:
rsm = rev_inord(root.right, rsm)
el = root.data
root.data = rsm
rsm = rsm + el
if root.left:
rsm = rev_inord(root.left, rsm)
return rsm
class Solution:
def transformTree(self, root):
rsm = 0
rsm = rev_inord(root, rsm)
return root | ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
ans = [0]
head = None
head = self.new(head, root)
self.transformUtil(root, ans)
return root
def transformUtil(self, root, ans):
if not root:
return
self.transformUtil(root.right, ans)
temp = root.data
root.data = ans[0]
ans[0] += temp
self.transformUtil(root.left, ans)
def new(self, head, root):
if root:
head = Node(1)
head.left = self.new(head.left, root.left)
head.right = self.new(head.right, root.right)
return head | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
def inorder(root):
if root == None:
return
inorder(root.left)
ino.append(root.data)
inorder(root.right)
ino = []
inorder(root)
prefixsum = []
sum = 0
for i in range(len(ino) - 1, -1, -1):
prefixsum.append(sum)
sum = sum + ino[i]
prefixsum = prefixsum[::-1]
g = {}
for i in range(0, len(ino)):
g[ino[i]] = prefixsum[i]
def answer(root):
if root == None:
return
answer(root.left)
root.data = g[root.data]
answer(root.right)
answer(root)
return root | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
def dfs(node, from_top):
if not node:
return 0
right = dfs(node.right, from_top)
val = node.data
node.data = right + from_top
left = dfs(node.left, node.data + val)
return left + right + val
dfs(root, 0) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
ar = []
arr = {}
temp = root
def inorder(root):
if root:
inorder(root.left)
ar.append(root.data)
inorder(root.right)
inorder(temp)
ar.sort()
ar = ar[::-1]
sm = 0
for it in ar:
arr[it] = sm
sm += it
def update(root):
if root:
update(root.left)
root.data = arr[root.data]
update(root.right)
update(root)
return root | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def postorder(self, root, res, val):
if root == None:
return None
self.postorder(root.right, res, val)
temp = root.data
root.data = val[0]
val[0] += temp
self.postorder(root.left, res, val)
def transformTree(self, root):
val = [0]
res = []
self.postorder(root, res, val)
return root | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root, extra=0):
if root is None:
return 0
root_right_sum = self.transformTree(root.right, extra=extra)
old_data = root.data
root.data = root_right_sum + extra
root_left_sum = self.transformTree(root.left, extra=old_data + root.data)
return root_left_sum + root_right_sum + old_data | CLASS_DEF FUNC_DEF NUMBER IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transform_tree_util(self, root, s):
if root is None:
return
self.transform_tree_util(root.right, s)
temp = root.data
root.data = s[0]
s[0] += temp
self.transform_tree_util(root.left, s)
def transformTree(self, root):
self.transform_tree_util(root, [0]) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR LIST NUMBER |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | def in_Order(root):
if not root:
return []
else:
return in_Order(root.left) + [root] + in_Order(root.right)
class Solution:
def transformTree(self, root):
arr = in_Order(root)
tot, prev = 0, 0
for i in range(len(arr) - 1, -1, -1):
tot, prev = tot + prev, arr[i].data
arr[i].data = tot | FUNC_DEF IF VAR RETURN LIST RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
def applyReverseInOrder(root, sum):
if root == None:
return sum
sum = applyReverseInOrder(root.right, sum)
rootVal = root.data
root.data = sum
sum = sum + rootVal
sum = applyReverseInOrder(root.left, sum)
return sum
sum = 0
applyReverseInOrder(root, sum)
return root | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def transformTree(self, root):
def inorder(root):
if root == None:
return
inorder(root.left)
res.append(root.data)
inorder(root.right)
res = []
inorder(root)
s = sum(res)
for i in range(1, len(res)):
res[i] += res[i - 1]
def inorderchange(root):
if root == None:
return
inorderchange(root.left)
root.data = s - res[index[0]]
index[0] += 1
inorderchange(root.right)
index = [0]
inorderchange(root)
return root | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater than that node.
Example 1:
Input:
2
/ \
1 6
/ \
3 7
Output: 18 16 13 7 0
Explanation:
Every node is replaced with the
sum of nodes greater than itself.
The resultant tree is:
16
/ \
18 7
/ \
13 0
Example 2:
Input:
2
/
1
Output: 2 0
Explanation:
The resultant tree is :
0
/
2
Your task :
You don't have to read input or print anything. Your task is to complete the function transformTree() which takes the root of the tree as input and transforms the BST to a greater sum tree.
Note : The driver code prints the inorder traversal of the transformed BST.
Expected Time Complexity: O(N), N = number of nodes
Expected Auxiliary Space: O(N), N = number of nodes
Constraints :
1 β€ N β€ 10^{4} | class Solution:
def dfs(self, root, nums):
if not root:
return
self.dfs(root.left, nums)
nums.append(root.data)
self.dfs(root.right, nums)
def transformTree(self, root):
nums = []
self.dfs(root, nums)
total = sum(nums)
def helper(root):
nonlocal total
if not root:
return
helper(root.left)
total = total - root.data
root.data = total
helper(root.right)
helper(root)
return root | CLASS_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ββ€β$i$ββ€β$N$) and column $j$ ($1$ββ€β$j$ββ€ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$.
He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$,β$y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$,β$y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle.
Finally, print the matrix after performing all the queries.
Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number.
-----Input:-----
- The first line of the input contains two integers $N$ and $M$ β the number of rows and the number of columns in the matrix.
- Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$.
- Next line contains an integer $Q$ β the number of queries.
- Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β coordinates of the up left and bottom right cells of the query rectangle.
-----Output:-----
Print the matrix, in the form of $N$ strings, after performing all the queries.
-----Constraints-----
- $1 \leq N,M \leq 1000$
- $0 \leq A_{ij} \leq 1$
- $1 \leq Q \leq 10^6$
- $1 \leq x_{1} \leq x_{2} \leq N$
- $1 \leq y_{1} \leq y_{2} \leq M$
-----Sample Input:-----
2 2
00
00
3
1 1 1 1
2 2 2 2
1 1 2 2
-----Sample Output:-----
01
10
-----Explanation:-----
Example case 1:
After processing the 1st query 1 1 1 1, matrix becomes:
[1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix}
After processing the 2nd query 2 2 2 2, the matrix becomes:
[1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix}
After processing the 3rd query 1 1 2 2, matrix becomes:
[0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix}
We need to output the matrix after processing all queries. | import sys
I = sys.stdin.readline
R = lambda: map(int, I().split())
n, m = R()
a = [I()[:-1] for _ in range(n)]
b = [([0] * (m + 2)) for _ in range(n + 1)]
(q,) = R()
for _ in range(q):
x1, y1, x2, y2 = R()
x1 -= 1
y2 += 1
for x in (x1, x2):
for y in (y1, y2):
b[x][y] ^= 1
dp = [0] * (m + 1)
for r, d in zip(a, b):
for i in range(m):
d[i + 1] ^= dp[i] ^ dp[i + 1] ^ d[i]
dp = d
print("".join(str(int(x) ^ y) for x, y in zip(r, d[1:]))) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR FOR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER |
Cherry has a binary matrix $A$ consisting of $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, columns are numbered from $1$ to $M$. Element at row $i$ ($1$ββ€β$i$ββ€β$N$) and column $j$ ($1$ββ€β$j$ββ€ $M$) is denoted as $A_{ij}$. All elements of $A$ are either $0$ or $1$.
He performs $Q$ queries on matrix. Each query is provided by four integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ which define the rectangle, where ($x_{1}$,β$y_{1}$) stands for the coordinates of the top left cell of the rectangle, while ($x_{2}$,β$y_{2}$) stands for the coordinates of the bottom right cell. You need to flip all the bits i.e. ($0$ to $1$, $1$ to $0$) that are located fully inside the query rectangle.
Finally, print the matrix after performing all the queries.
Note: $x_{1}$ represents the row number while $y_{1}$ represents the column number.
-----Input:-----
- The first line of the input contains two integers $N$ and $M$ β the number of rows and the number of columns in the matrix.
- Each of the next $N$ lines contains a string of length $M$, where the $j^{th}$ character of $i^{th}$ line denotes the value of $A_{i,j}$.
- Next line contains an integer $Q$ β the number of queries.
- Then follow $Q$ lines with queries descriptions. Each of them contains four space-seperated integers $x_{1}$, $y_{1}$, $x_{2}$, $y_{2}$ β coordinates of the up left and bottom right cells of the query rectangle.
-----Output:-----
Print the matrix, in the form of $N$ strings, after performing all the queries.
-----Constraints-----
- $1 \leq N,M \leq 1000$
- $0 \leq A_{ij} \leq 1$
- $1 \leq Q \leq 10^6$
- $1 \leq x_{1} \leq x_{2} \leq N$
- $1 \leq y_{1} \leq y_{2} \leq M$
-----Sample Input:-----
2 2
00
00
3
1 1 1 1
2 2 2 2
1 1 2 2
-----Sample Output:-----
01
10
-----Explanation:-----
Example case 1:
After processing the 1st query 1 1 1 1, matrix becomes:
[1000][1000]\begin{bmatrix} 10 \\ 00 \end{bmatrix}
After processing the 2nd query 2 2 2 2, the matrix becomes:
[1001][1001]\begin{bmatrix} 10 \\ 01 \end{bmatrix}
After processing the 3rd query 1 1 2 2, matrix becomes:
[0110][0110]\begin{bmatrix} 01 \\ 10 \end{bmatrix}
We need to output the matrix after processing all queries. | n, m = map(int, input().split())
arr = []
for i in range(n):
s = input() + "0"
temp = []
for x in s:
temp += [int(x)]
arr += [temp]
arr += [[0] * (m + 1)]
add = [[(0) for i in range(m + 1)] for j in range(n + 1)]
pref1 = [[(0) for i in range(m + 1)] for j in range(n + 1)]
pref2 = [[(0) for i in range(m + 1)] for j in range(n + 1)]
q = int(input())
for _ in range(q):
x1, y1, x2, y2 = map(int, input().split())
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
pref2[x1][y1] += 1
pref2[x2 + 1][y1] -= 1
pref2[x1][y2 + 1] += -1
pref2[x2 + 1][y2 + 1] -= -1
for i in range(m):
for j in range(n):
if j == 0:
pref1[j][i] = pref2[j][i]
else:
pref1[j][i] = pref1[j - 1][i] + pref2[j][i]
for i in range(n):
for j in range(m):
if j == 0:
add[i][j] += pref1[i][j]
else:
add[i][j] = add[i][j - 1] + pref1[i][j]
for i in range(n):
for j in range(m):
arr[i][j] -= add[i][j]
arr[i][j] %= 2
for i in range(n):
for j in range(m):
print(arr[i][j], end="")
print("") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR VAR LIST FUNC_CALL VAR VAR VAR LIST VAR VAR LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.