description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
pair = list()
pair1 = list()
pair2 = list()
for i in range(n):
x, y = map(int, input().split())
pair.append((x, y))
pair.sort()
f = True
for i in range(n):
x = pair[i][0]
y = pair[i][1]
len1 = len(pair1)
len2 = len(pair2)
if len1 == 0 or x > pair1[len1 - 1][1]:
pair1.append((x, y))
elif len2 == 0 or x > pair2[len2 - 1][1]:
pair2.append((x, y))
else:
f = False
break
if f:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
t = [0] * n
t1 = -1
t2 = -1
ff = True
for i in range(n):
a, b = list(map(int, input().split()))
t[i] = a, b
t.sort()
for i in range(n):
bl = False
if t[i][0] > t1:
t1 = t[i][1]
bl = True
if not bl and t[i][0] > t2:
t2 = t[i][1]
bl = True
if not bl:
ff = False
break
if ff:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | from sys import stdin
n = int(stdin.readline().strip())
s = [tuple(map(int, stdin.readline().strip().split())) for i in range(n)]
s.sort()
t1 = -1, -1
t2 = -1, -1
for i in s:
if i[0] > t1[1]:
t1 = i
elif i[0] > t2[1]:
t2 = i
else:
print("NO")
exit(0)
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | import sys
input = sys.stdin.readline
n = int(input())
lr = [tuple(map(int, input().split())) for _ in range(n)]
lr.sort(key=lambda k: k[0])
t1, t2 = -1, -1
for l, r in lr:
if t1 < l:
t1 = r
elif t2 < l:
t2 = r
else:
print("NO")
exit()
print("YES") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
d = []
for i in range(n):
l, r = map(int, input().split())
d.append((l, 1))
d.append((r + 1, -1))
d.sort()
res = 0
a = True
for i in d:
res = res + i[1]
if res > 2:
print("NO")
a = False
break
if a:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | d = []
n = int(input())
for i in range(n):
a, b = map(int, input().split())
d.append([a, -1])
d.append([b, 1])
d.sort()
t = 0
for i in d:
t -= i[1]
if t > 2:
print("NO")
exit(0)
print("YES") | ASSIGN VAR LIST 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 EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
line = list()
for i in range(n):
line.append(list(map(int, input().split())))
line.sort()
tv_1 = [-1, -1]
tv_2 = [-1, -1]
b = True
for elem in line:
if tv_1[1] < elem[0]:
tv_1 = elem
elif tv_2[1] < elem[0]:
tv_2 = elem
else:
b = False
break
print("YES" if b else "NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | def main():
n = int(input())
L = [None] * n
for i in range(n):
L[i] = [int(x) for x in input().split()]
print(solver(n, L))
def solver(n, L):
momentList = [None] * (2 * n)
index = 0
for l, r in L:
momentList[index] = l, 0
momentList[index + 1] = r, 1
index += 2
momentList.sort(key=lambda x: (x[0], x[1]))
showsAtOnce = 0
for time, typ in momentList:
if typ == 0:
showsAtOnce += 1
if showsAtOnce > 2:
return "NO"
elif typ == 1:
showsAtOnce -= 1
return "YES"
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE 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 FUNC_DEF ASSIGN VAR BIN_OP LIST NONE BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER VAR NUMBER RETURN STRING EXPR FUNC_CALL VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | count = int(input())
shows = []
impossible = False
showing = 0
for i in range(count):
show = input().split(" ")
shows.append([int(show[0]), 1])
shows.append([int(show[1]) + 1, -1])
shows.sort(key=lambda iterableShow: iterableShow[0] * 3 + iterableShow[1])
for i in range(count * 2):
showing += shows[i][1]
if showing > 2:
print("NO")
impossible = True
break
if not impossible:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
def conflicts(a, b):
return a[1] >= b[0]
shows = []
for i in range(n):
x, y = map(int, input().split())
shows.append((x, y))
shows.sort(key=lambda x: x[0])
ok = True
tv1 = [(-1, -1)]
tv2 = [(-1, -1)]
for s in shows:
if not conflicts(tv1[-1], s):
tv1.append(s)
elif not conflicts(tv2[-1], s):
tv2.append(s)
else:
ok = False
break
if ok:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
s = []
for _ in range(n):
x, y = map(int, input().split())
s.append((x, y))
s.sort()
a = [s[0]]
b = [(0, -10)]
for x in range(1, len(s)):
if s[x][0] > a[-1][1]:
a.append(s[x])
elif s[x][0] > b[-1][1]:
b.append(s[x])
if len(a) + len(b) - 1 == len(s):
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | def lol(a):
c = 0
for x in a:
if x[1] == 1:
c += 1
else:
c -= 1
if c >= 3:
return 0
return 1
n = int(input())
a = []
for _ in range(n):
x, y = map(int, input().split())
a.append([x, 1])
a.append([y, 2])
a = sorted(a)
print("YES" if lol(a) else "NO") | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
tv1 = -5, -5
tv2 = -5, -5
flag = True
a = []
for i in range(n):
a.append([int(x) for x in input().split()])
a.sort()
for l, r in a:
if tv1[1] < l:
tv1 = l, r
elif tv2[1] < l:
tv2 = l, r
else:
flag = False
if flag:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
m = []
for i in range(0, n):
a, b = map(int, input().split())
m.append((a, b))
m.sort()
a1 = -1
a2 = -1
flag = "YES"
for i in range(n):
if m[i][0] > a1:
a1 = m[i][1]
elif m[i][0] > a2:
a2 = m[i][1]
else:
flag = "NO"
print(flag) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
times = []
for i in range(n):
l, r = map(int, input().split())
times.append((l, 1))
times.append((r, -1))
times.sort(key=lambda x: (x[0], -x[1]))
s = 0
ans = "YES"
for x in times:
s += x[1]
if s == 3:
ans = "NO"
break
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | tv1 = -1
tv2 = -1
shows = []
for _ in range(int(input())):
l, r = map(int, input().split())
shows.append((l, r))
shows.sort(key=lambda x: x[0])
for l, r in shows:
if tv1 < l:
tv1 = r
elif tv2 < l:
tv2 = r
else:
print("NO")
break
else:
print("YES") | ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
l = []
for i in range(0, n):
a, b = input().split()
a, b = int(a), int(b)
l.append([a, 0])
l.append([b, 1])
l.sort(key=lambda x: (x[0], x[1]))
TV = 0
r = 0
ans = "YES"
for x in l:
if x[1] == 0:
TV += 1
else:
TV -= 1
if TV > 2:
ans = "NO"
break
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
show = [list(map(int, input().split())) for _ in range(n)]
show.sort(key=lambda x: x[0])
tv1 = tv2 = -1
ans = True
for i in range(n):
if show[i][0] > tv1:
tv1 = show[i][1]
elif show[i][0] > tv2:
tv2 = show[i][1]
else:
ans = False
break
print("YES" if ans else "NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
shows = []
for i in range(n):
show = list(map(int, input().split()))
shows.append(show)
shows = sorted(shows)
t = [-1, -1]
ok = True
for show in shows:
if show[0] > t[0]:
t[0] = show[1]
elif show[0] > t[1]:
t[1] = show[1]
else:
ok = False
break
if ok:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | num_shows = int(input())
showtimes = []
for i in range(num_shows):
showtime = [int(i) for i in input().split(" ")]
showtimes.append((showtime[0], showtime[1]))
showtimes = sorted(showtimes)
class TV:
def __init__(self):
self.endtime = -1
tv1 = TV()
tv2 = TV()
start_time = min(st[0] for st in showtimes)
end_time = max(st[1] for st in showtimes)
fail_signal = False
for st in showtimes:
start_time = st[0]
end_time = st[1]
if start_time <= tv1.endtime and start_time <= tv2.endtime:
fail_signal = True
break
elif start_time > tv1.endtime:
tv1.endtime = end_time
else:
tv2.endtime = end_time
if fail_signal:
print("NO")
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
shows = [tuple(map(int, input().split())) for _ in range(n)]
shows = sorted(shows, key=lambda t: t[1])
shows = sorted(shows, key=lambda t: t[0])
first, second = -1, -1
for l, r in shows:
if l > first:
first = r
elif l > second:
second = r
else:
print("NO")
break
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
data = []
k = 0
for i in range(n):
data.append(list(map(int, input().split())))
data.sort()
if n < 2:
print("YES")
else:
a = data[0]
b = data[1]
for i in range(2, n):
if a[1] < data[i][0]:
a = data[i]
elif b[1] < data[i][0]:
b = data[i]
else:
print("NO")
k = 1
break
if k == 0:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | import sys
n = int(input())
x = []
z = []
for i in range(n):
a, b = map(int, input().split())
x.append(a)
z.append(b + 1)
x.sort()
z.sort()
s = ""
sc = 0
k = 0
j = 0
while len(x) != k and len(z) != j:
if x[k] > z[j]:
s += ")"
j += 1
elif x[k] < z[j]:
s += "("
k += 1
else:
k += 1
j += 1
if len(z) - j > 0:
s += ")" * (len(z) - j)
for i in range(len(s)):
if sc > 2:
print("NO")
sys.exit()
if s[i] == "(":
sc += 1
else:
sc -= 1
print("YES") | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
segment = []
for _ in range(n):
segment.append(list(map(int, input().split())))
segment.sort(key=lambda x: x[0])
start1 = 0
end1 = -float("inf")
start2 = 0
end2 = -float("inf")
for x in segment:
start = x[0]
end = x[1]
if start > end1:
start1 = start
end1 = end
elif start > end2:
start2 = start
end2 = end
else:
print("NO")
exit()
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
arr = list()
for i in range(n):
l, r = map(int, input().split())
arr.append((l, r))
e1, e2 = -1, -1
arr.sort()
ans = "YES"
for i in arr:
if i[0] > e1:
e1 = i[1]
elif i[0] > e2:
e2 = i[1]
else:
ans = "NO"
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input().strip())
schedule = []
for i in range(n):
l, r = list(map(int, input().strip().split()))
schedule.append((l, -1))
schedule.append((r, 1))
schedule.sort()
layer = 0
is_possible = True
for _, dx in schedule:
layer += dx
if layer < -2:
is_possible = False
break
if is_possible:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST 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 NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
c = []
for i in range(n):
a = list(map(int, input().strip().split(" ")))
c.append(a)
c.sort()
if n <= 2:
print("YES")
else:
u = 0
tv1 = []
tv2 = []
tv1.append(c[0])
tv2.append(c[1])
for i in range(2, n):
if c[i][0] > tv1[0][1]:
tv1[0] = c[i]
elif c[i][0] > tv2[0][1]:
tv2[0] = c[i]
else:
u = 1
break
if u == 0:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
shows = []
for i in range(n):
shows.append([int(x) for x in input().split()])
shows = sorted(shows, key=lambda tup: tup[0])
def fit(show, tv):
if show[1] < tv[0] or show[0] > tv[1]:
return True
return False
def main():
if n < 2:
print("YES")
return
tv1 = shows[0]
tv2 = shows[1]
for show in shows[2:]:
if fit(show, tv1):
tv1 = show
elif fit(show, tv2):
tv2 = show
else:
print("NO")
return
print("YES")
return
main() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | import sys
input = sys.stdin.readline
def solve():
n = int(input())
shows = []
for show in range(n):
l, r = [int(x) for x in input().split(" ")]
shows.append((l, -1))
shows.append((r, 1))
shows.sort()
conc = 0
for s in shows:
conc += s[1]
if conc < -2:
return "NO"
return "YES"
print(solve()) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
shows = []
for i in range(n):
l, r = map(int, input().split())
shows.append((l, r))
shows = sorted(shows)
fst_tv = []
snd_tv = []
printed = False
for s in shows:
if fst_tv == []:
fst_tv.append(s)
elif snd_tv == []:
snd_tv.append(s)
elif s[0] > fst_tv[-1][1] and s[0] > snd_tv[-1][1]:
fst_tv.append(s)
elif s[0] <= fst_tv[-1][1] and s[0] > snd_tv[-1][1]:
snd_tv.append(s)
elif s[0] <= snd_tv[-1][1] and s[0] > fst_tv[-1][1]:
fst_tv.append(s)
else:
print("NO")
printed = True
break
if not printed:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
t = []
flag = True
for i in range(n):
t.append(list(map(int, input().split(" "))))
t.sort()
l1, l2 = -1, -1
for i in range(n):
if t[i][0] > l1 and t[i][0] > l2:
if l1 > l2:
l1 = t[i][1]
else:
l2 = t[i][1]
elif t[i][0] > l1:
l1 = t[i][1]
elif t[i][0] > l2:
l2 = t[i][1]
else:
print("NO")
flag = False
break
if flag:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | k = int(input())
nacalo = []
konec = []
s = 0
for gh in range(k):
x, y = map(int, input().split())
nacalo.append(x)
konec.append(y)
nacalo.sort()
konec.sort()
i_nach = 0
i_kon = 0
while i_nach < k and i_kon < k:
tek = min(nacalo[i_nach], konec[i_kon])
while i_nach < k and nacalo[i_nach] == tek:
s += 1
if s >= 3:
print("NO")
exit(0)
i_nach += 1
while i_kon < k and konec[i_kon] == tek:
s -= 1
i_kon += 1
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | s = [tuple(map(int, input().split())) for i in range(int(input()))]
s.sort(key=lambda q: q[0])
a = b = -1
q = "YES"
for l, r in s:
if a >= l:
q = "NO"
break
a = r
if b < a:
a, b = b, a
print(q) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR IF VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | def main():
l, c = [], 0
for _ in range(int(input())):
a, b = map(int, input().split())
l.append(a * 2)
l.append(b * 2 + 1)
l.sort()
for a in l:
if a & 1:
c -= 1
else:
c += 1
if c > 2:
print("NO")
break
else:
print("YES")
main() | FUNC_DEF ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
a = []
for i in range(n):
x, y = map(int, input().split())
a.append((x, y))
f = True
a.sort()
t1, t2 = -1, -1
for i in range(n):
x, y = a[i]
if x > t1:
t1 = y
elif x > t2:
t2 = y
else:
f = False
if f:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
a, b = [], []
for i in range(n):
t = [int(i) for i in input().split()]
a.append(t[0])
b.append(t[1])
a.sort()
b.sort()
i = j = 0
ans = 0
while ans <= 2 and i < n and j < n:
if a[i] <= b[j]:
ans += 1
i += 1
else:
ans -= 1
j += 1
if ans > 2:
print("NO")
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
a = []
for i in range(n):
b = list(map(int, input().split()))
a.append([b[0], -1])
a.append([b[1], 1])
a.sort()
cur = 0
for i in a:
cur += i[1]
if cur <= -3:
print("NO")
break
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | import sys
n = int(input())
shows = []
for i in range(n):
lis = list(map(int, input().split()))
shows.append(lis)
if n > 2:
tv1 = []
tv2 = []
shows.sort()
tv1.append(shows[0])
tv2.append(shows[1])
i = 2
while i < n:
if shows[i][0] > tv1[len(tv1) - 1][1]:
tv1.append(shows[i])
elif shows[i][0] > tv2[len(tv2) - 1][1]:
tv2.append(shows[i])
else:
print("NO")
sys.exit()
i += 1
print("YES")
else:
print("YES") | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
d = {}
s = []
for i in range(n):
a = list(map(int, input().split(" ")))
if a[0] in d:
d[a[0]][0] += 1
else:
d[a[0]] = [1, 0]
s.append(a[0])
if a[1] in d:
d[a[1]][1] += 1
else:
d[a[1]] = [0, 1]
s.append(a[1])
s.sort()
t = 0
r = 0
for i in s:
t += d[i][0]
if t > 2:
r = 1
break
t -= d[i][1]
if r == 1:
print("NO")
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | import itertools
def srtf(elem: list):
return elem[0]
def solve():
n = int(input())
a = []
for i in range(0, n):
rx = list(map(lambda x: int(x), input().split()))
a.append([rx[0], rx[1]])
a.sort(key=srtf)
tv = [-1, -1]
for i in range(0, n):
if tv[0] < a[i][0]:
tv[0] = -1
if tv[1] < a[i][0]:
tv[1] = -1
if tv[0] == -1:
tv[0] = a[i][1]
elif tv[1] == -1:
tv[1] = a[i][1]
else:
print("NO")
return
print("YES")
solve() | IMPORT FUNC_DEF VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
shows = [list(map(int, input().split())) for _ in range(n)]
shows.sort(key=lambda x: x[0])
tv1 = [-1, -1]
tv2 = [-1, -1]
output = "YES"
for show in shows:
if show[0] > tv1[1]:
tv1 = show
elif show[0] > tv2[1]:
tv2 = show
else:
output = "NO"
break
print(output) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
a = []
for i in range(n):
aa = list(map(int, input().split()))
a.append(aa)
a = sorted(a)
if n > 2:
tv1 = a[0][1]
tv2 = a[1][1]
t = True
for i in range(2, n):
if a[i][0] > tv1:
tv1 = a[i][1]
elif a[i][0] > tv2:
tv2 = a[i][1]
else:
t = False
break
if t:
print("YES")
else:
print("NO")
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
horarios = [tuple(map(int, input().split())) for i in range(n)]
horarios.sort()
aux = []
flag = True
for i in range(n):
if len(aux) < 2:
aux.append(horarios[i])
continue
if horarios[i][0] > aux[0][1]:
aux[0] = horarios[i]
elif horarios[i][0] > aux[1][1]:
aux[1] = horarios[i]
else:
flag = False
break
if flag:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | def Main():
timesheet = {}
n = int(input())
while n > 0:
p = list(map(int, input().split()))
if timesheet.get(p[0], -1) == -1:
timesheet[p[0]] = [p[1]]
else:
timesheet[p[0]].append(p[1])
n -= 1
times = sorted(list(timesheet.keys()))
t1, t2, res = 0, 0, "YES"
for t in times:
timeline = timesheet.get(t)
for tl in timeline:
if t1 < t or t1 == 0:
t1 = tl
elif t2 < t or t2 == 0:
t2 = tl
else:
res = "NO"
break
if res == "NO":
break
print(res)
Main() | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER LIST VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
shows = []
for i in range(n):
start, end = [int(c) for c in input().split(" ")]
shows.append((start, end))
shows.sort(key=lambda tup: tup[0])
last_show_tv1 = None
last_show_tv2 = None
can_watch = True
for show in shows:
if last_show_tv1 == None:
last_show_tv1 = show
elif last_show_tv2 == None:
last_show_tv2 = show
elif not show[0] <= last_show_tv1[1]:
last_show_tv1 = show
elif not show[0] <= last_show_tv2[1]:
last_show_tv2 = show
else:
can_watch = False
break
if can_watch:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | import sys
count = 0
lesson_list = []
for line in sys.stdin:
if count == 0:
num = int(line.strip())
count += 1
continue
start = int(line.strip().split(" ")[0])
end = int(line.strip().split(" ")[1])
lesson_list.append((start, end))
lesson_list.sort()
a_status = True
b_status = True
a_time = 0
b_time = 0
flag = 0
for tmp in lesson_list:
start = tmp[0]
end = tmp[1]
if start < a_time and start < b_time:
flag = -1
break
if start >= a_time:
a_time = end + 1
continue
b_time = end + 1
if flag < 0:
print("NO")
else:
print("YES") | IMPORT ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR 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 |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | import sys
N = int(input())
C = []
X = []
inside = {}
ctr = 0
for n in range(N):
l, r = map(int, input().split())
C += [(l, r)]
C.sort(key=lambda k: (k[0], k[1]))
i = 0
for l, r in C:
X += [(l, i), (r, i)]
i += 1
X.sort(key=lambda k: (k[0], -k[1]))
for x, i in X:
if not inside.get(i):
inside[i] = True
ctr += 1
if ctr > 2:
print("NO")
sys.exit()
else:
inside[i] = None
ctr -= 1
print("YES") | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR LIST VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NONE VAR NUMBER EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | def main():
arrl = []
arrr = []
n = int(input())
for t in range(n):
a, b = map(int, input().split())
arrl.append(a)
arrr.append(b)
arrl.sort()
arrr.sort()
l = 0
r = 0
cnt = 0
while l < n and r < n:
if arrl[l] <= arrr[r]:
cnt += 1
l += 1
else:
cnt -= 1
r += 1
if cnt > 2:
print("NO")
return
print("YES")
return
main() | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
shows = []
for i in range(n):
temp = [int(j) for j in input().split(" ")]
temp[1] += 1
shows.append(temp[:])
shows.sort()
if n > 1:
tv1 = shows[0][1]
tv2 = shows[1][1]
flag1, flag2 = 0, 0
c = 0
shows.append([1000000001, 1000000001])
for i in range(2, n, 1):
if tv1 <= shows[i][0] and flag1 == 0:
tv1 = shows[i][1]
if shows[i + 1][0] >= tv1:
flag1 = 0
else:
flag1 = 1
if shows[i + 1][0] >= tv2:
flag2 = 0
else:
flag2 = 1
elif tv2 <= shows[i][0] and flag2 == 0:
tv2 = shows[i][1]
if shows[i + 1][0] >= tv1:
flag1 = 0
else:
flag1 = 1
if shows[i + 1][0] >= tv2:
flag2 = 0
else:
flag2 = 1
else:
c = -1
if c == 0:
print("YES")
else:
print("NO")
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
events = []
for i in range(n):
a, b = map(int, input().split())
events.append((a, +1))
events.append((b, -1))
events = sorted(events, key=lambda x: (x[0], -x[1]))
max_shows = 0
cur_shows = 0
for _, e in events:
cur_shows += e
max_shows = max(max_shows, cur_shows)
if max_shows > 2:
print("NO")
else:
print("YES") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | n = int(input())
tv1 = -1
tv2 = -1
a = []
drop = False
for _ in range(n):
a.append(tuple(int(x) for x in input().split()))
a.sort()
for s, f in a:
if tv1 < s:
tv1 = f
elif tv2 < s:
tv2 = f
else:
drop = True
print("YNEOS"[drop::2]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | def main(d, t):
for i in d:
t += i[1]
if t < -2:
return "NO"
return "YES"
d = []
n = int(input())
for i in range(n):
a, b = input().split()
a = int(a)
b = int(b)
d.append([a, -1])
d.append([b, 1])
d.sort()
t = 0
ans = main(d, t)
print(ans) | FUNC_DEF FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Polycarp is a great fan of television.
He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}.
Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.
Polycarp wants to check out all n shows. Are two TVs enough to do so?
-----Input-----
The first line contains one integer n (1 β€ n β€ 2Β·10^5) β the number of shows.
Each of the next n lines contains two integers l_{i} and r_{i} (0 β€ l_{i} < r_{i} β€ 10^9) β starting and ending time of i-th show.
-----Output-----
If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes).
-----Examples-----
Input
3
1 2
2 3
4 5
Output
YES
Input
4
1 2
2 3
2 3
1 2
Output
NO | read = lambda: map(int, input().split())
n = int(input())
ev = []
for i in range(n):
l, r = read()
r += 1
ev.append((l, 1))
ev.append((r, -1))
ev.sort()
bal = 0
flag = 1
for x, t in ev:
bal += t
if bal > 2:
flag = 0
break
print("YES" if flag else "NO") | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Given a binary tree. The task is to check whether the given tree follows the max heap property or not.
Note: Properties of a tree to be a max heap - Completeness and Value of node greater than or equal to its child.
Example 1:
Input:
5
/ \
2 3
Output: 1
Explanation: The given tree follows max-heap property since 5,
is root and it is greater than both its children.
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output: 0
Your Task:
You don't need to read input or print anything. Your task is to complete the function isHeap() which takes the root of Binary Tree as parameter returns True if the given binary tree is a heap else returns False.
Expected Time Complexity: O(N)
Expected Space Complexity: O(N)
Constraints:
1 β€ Number of nodes β€ 100
1 β€ Data of a node β€ 1000 | class Solution:
def isHeap(self, root):
queue = [root]
flag = 0
while queue:
node = queue.pop(0)
if node.left:
queue.append(node.left)
if flag == 1 or node.left.data > node.data:
return False
else:
flag = 1
if node.right:
queue.append(node.right)
if flag == 1 or node.right.data > node.data:
return False
else:
flag = 1
return True | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER RETURN NUMBER |
Given a binary tree. The task is to check whether the given tree follows the max heap property or not.
Note: Properties of a tree to be a max heap - Completeness and Value of node greater than or equal to its child.
Example 1:
Input:
5
/ \
2 3
Output: 1
Explanation: The given tree follows max-heap property since 5,
is root and it is greater than both its children.
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output: 0
Your Task:
You don't need to read input or print anything. Your task is to complete the function isHeap() which takes the root of Binary Tree as parameter returns True if the given binary tree is a heap else returns False.
Expected Time Complexity: O(N)
Expected Space Complexity: O(N)
Constraints:
1 β€ Number of nodes β€ 100
1 β€ Data of a node β€ 1000 | def countnodes(root):
if not root:
return 0
ans = 1 + countnodes(root.left) + countnodes(root.right)
return ans
def isCBT(root, index, count):
if not root:
return True
if index >= count:
return False
left = isCBT(root.left, 2 * index + 1, count)
right = isCBT(root.right, 2 * index + 2, count)
return left and right
def maxorder(root):
if root.left is None and root.right is None:
return True
if root.right is None:
return root.data > root.left.data
left = maxorder(root.left)
right = maxorder(root.right)
return left and right and root.data > root.left.data and root.data > root.right.data
class Solution:
def isHeap(self, root):
index = 0
totalcount = countnodes(root)
if isCBT(root, index, totalcount) and maxorder(root):
return True
else:
return False | FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR RETURN VAR VAR FUNC_DEF IF VAR NONE VAR NONE RETURN NUMBER IF VAR NONE RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def rec_sol_left(self, root, dist, max_dist, ans):
if dist > max_dist[0]:
ans.append(root.data)
max_dist[0] = dist
if root.left != None:
self.rec_sol_left(root.left, dist + 1, max_dist, ans)
if root.right != None:
self.rec_sol_left(root.right, dist - 1, max_dist, ans)
return
def rec_sol_right(self, root, dist, max_dist, ans):
if dist > max_dist[0]:
ans.append(root.data)
max_dist[0] = dist
if root.right != None:
self.rec_sol_right(root.right, dist + 1, max_dist, ans)
if root.left != None:
self.rec_sol_right(root.left, dist - 1, max_dist, ans)
return
def topView(self, root):
queue = []
mp = {}
mp[0] = root.data
queue.append([root, 0])
while len(queue) != 0:
temp = queue.pop(0)
temp_node, temp_dist = temp[0], temp[1]
if temp_dist not in list(mp.keys()):
mp[temp_dist] = temp_node.data
if temp_node.left != None:
queue.append([temp_node.left, temp_dist - 1])
if temp_node.right != None:
queue.append([temp_node.right, temp_dist + 1])
final = []
keys = list(mp.keys())
keys.sort()
for k in keys:
final.append(mp[k])
return final
ans = []
max_dist = [-1]
self.rec_sol_left(root, 0, max_dist, ans)
ans.reverse()
ans.pop()
max_dist = [-1]
self.rec_sol_right(root, 0, max_dist, ans)
return ans | CLASS_DEF FUNC_DEF IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR IF VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_DEF IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR IF VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
queue = [(root, 0)]
hd, l, r = 0, 0, 0
left = []
right = []
while queue:
node, hd = queue.pop(0)
temp = []
if hd < l:
left = [node.data] + left
l -= 1
elif hd > r:
right.append(node.data)
r += 1
if node.left:
queue.append((node.left, hd - 1))
if node.right:
queue.append((node.right, hd + 1))
return left + [root.data] + right | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST IF VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR LIST VAR VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
class point:
x = None
y = None
def __init__(self, x, y):
self.x = x
self.y = y
def topView(self, root):
q = list()
dct = dict()
q.append(self.point(root, 0))
while q:
k = q.pop(0)
x1 = k.x
y1 = k.y
if y1 in dct:
dct[y1].append(x1.data)
else:
dct[y1] = list()
dct[y1].append(x1.data)
if x1.left != None:
q.append(self.point(x1.left, y1 - 1))
if x1.right != None:
q.append(self.point(x1.right, y1 + 1))
l1 = list(dct.keys())
l1.sort()
r = list()
for i in l1:
r.append(dct[i][0])
return r | CLASS_DEF CLASS_DEF ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
hmap = {}
Q = []
lis = []
Q.append([0, root])
while len(Q) != 0:
val = Q[0]
dist = val[0]
comp = val[1]
if dist in hmap:
pass
else:
hmap[dist] = comp.data
Q.pop(0)
if comp.left:
Q.append([dist - 1, comp.left])
if comp.right:
Q.append([dist + 1, comp.right])
sor = sorted(hmap)
for i in sor:
lis.append(hmap[i])
return lis | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
queue = [(0, 0, root)]
levelElements = {}
while queue:
col, row, node = queue.pop(0)
if col not in levelElements:
levelElements[col] = node.data
if node.left:
queue.append((col - 1, row + 1, node.left))
if node.right:
queue.append((col + 1, row + 1, node.right))
levelElements = sorted(levelElements.items(), key=lambda x: (x[0], x[1]))
return [i[1] for i in levelElements]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER VAR ASSIGN VAR DICT WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
if not root:
return
m = {}
hd = 0
q = []
q.append((root, hd))
mn, mx = 0, 0
while q:
temp = q.pop(0)
hd = temp[1]
node = temp[0]
if hd in m:
m[hd].append(node.data)
else:
m[hd] = [node.data]
if node.left:
q.append((node.left, hd - 1))
if node.right:
q.append((node.right, hd + 1))
if mn > hd:
mn = hd
elif mx < hd:
mx = hd
ans = []
for i in range(mn, mx + 1):
if i in m:
tmp = m[i]
ans.append(tmp[0])
return ans | CLASS_DEF FUNC_DEF IF VAR RETURN ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
if not root:
return []
d = {}
def traverse(node, hd, level):
if not node:
return
if hd not in d or level < d[hd][1]:
d[hd] = [node.data, level]
traverse(node.left, hd - 1, level + 1)
traverse(node.right, hd + 1, level + 1)
traverse(root, 0, 0)
return [x[0] for _, x in sorted(d.items())] | CLASS_DEF FUNC_DEF IF VAR RETURN LIST ASSIGN VAR DICT FUNC_DEF IF VAR RETURN IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
if root is None:
return []
d = {}
q = []
levels = []
q.append(root)
levels.append(0)
while q:
curr = q.pop(0)
level = levels.pop(0)
if level not in d:
d[level] = curr.data
if curr.left != None:
q.append(curr.left)
levels.append(level - 1)
if curr.right != None:
q.append(curr.right)
levels.append(level + 1)
return [d[i] for i in sorted(d.keys())] | CLASS_DEF FUNC_DEF IF VAR NONE RETURN LIST ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
dicto = {}
queue = []
queue.append([root, 0])
def topViewUtil(node, level):
nonlocal dicto
while queue:
element = queue.pop(0)
level = element[1]
if level not in dicto:
dicto[level] = element[0].data
if element[0].left:
queue.append([element[0].left, level - 1])
if element[0].right:
queue.append([element[0].right, level + 1])
topViewUtil(root, 0)
levels, elements = [], []
for k, v in dicto.items():
levels.append(k)
elements.append(v)
return [y for _, y in sorted(zip(levels, elements))] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR NUMBER FUNC_DEF WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
ans = []
if root is None:
return ans
mpp, q = dict(), list()
q.append((root, 0))
while q:
ele = q[0]
q.pop(0)
node = ele[0]
line = ele[1]
if line not in mpp:
mpp[line] = node.data
if node.left:
q.append((node.left, line - 1))
if node.right:
q.append((node.right, line + 1))
for ele in sorted(mpp.keys()):
ans.append(mpp[ele])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR NONE RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
if root == None:
return []
hashmap = {}
d = {}
queue = []
queue.append([root, 0])
while queue:
node = queue.pop(0)
cnode = node[0]
value = cnode.data
line = node[1]
if line not in hashmap:
hashmap[line] = value
if cnode.left:
queue.append([cnode.left, line - 1])
if cnode.right:
queue.append([cnode.right, line + 1])
res = []
d = sorted(hashmap.items())
for i in d:
res.append(i[1])
return res | CLASS_DEF FUNC_DEF IF VAR NONE RETURN LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
res = []
q = []
d = {}
if root == None:
return []
q.append((root, 0))
while q:
l = len(q)
for i in range(l):
x, y = q.pop(0)
if y not in d:
d[y] = x.data
if x != None and x.left != None:
q.append((x.left, y - 1))
if x != None and x.right != None:
q.append((x.right, y + 1))
for v in sorted(d.keys()):
res.append(d[v])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT IF VAR NONE RETURN LIST EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR NONE VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NONE VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
x, y = 0, 0
l, r = 0, 0
self.mapping = {}
q = [(x, y, root)]
while q:
x, y, front = q.pop(0)
if not self.mapping.get(x):
self.mapping[x] = y, front.data
l, r = min(l, x), max(r, x)
if front.left:
q.append((x - 1, y + 1, front.left))
if front.right:
q.append((x + 1, y + 1, front.right))
res = []
while l <= r:
res.append(self.mapping[l][1])
l += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR LIST VAR VAR VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
queue = [(root, 0)]
temp = {(0): root.data}
while queue:
node, level = queue.pop(0)
if level not in temp:
temp[level] = node.data
if node.left != None:
queue.append((node.left, level - 1))
if node.right != None:
queue.append((node.right, level + 1))
keys = list(temp.keys())
keys.sort()
ans = []
for key in keys:
ans.append(temp[key])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR DICT NUMBER VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
res = []
d = dict()
custom_queue = []
custom_queue.append([0, root])
while custom_queue:
temp = custom_queue.pop()
idx, parent = temp
if idx not in d:
d[idx] = parent.data
if parent.left:
custom_queue.insert(0, [idx - 1, parent.left])
if parent.right:
custom_queue.insert(0, [idx + 1, parent.right])
for ele in sorted(d):
res.append(d[ele])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST NUMBER VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER LIST BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR NUMBER LIST BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
res = []
d = {}
queue = []
queue.append([root, 0])
while queue != []:
root, level = queue.pop()
if level not in d:
d[level] = [root.data]
else:
d[level].append(root.data)
if root.left:
queue.insert(0, [root.left, level - 1])
if root.right:
queue.insert(0, [root.right, level + 1])
for ele in sorted(d):
res.append(d[ele][0])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR NUMBER WHILE VAR LIST ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER LIST VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
d = {}
q = []
res = []
q.append((root, 0))
while q:
n, l = q.pop(0)
if n and l not in d:
d[l] = n.data
if n.left:
q.append((n.left, l - 1))
if n.right:
q.append((n.right, l + 1))
d = sorted(d.items(), key=lambda x: x[0])
for k, v in d:
res.append(v)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
d = {}
q = [[root, 0]]
while len(q) != 0:
current = q[0][0]
index = q[0][1]
q.pop(0)
if index in d:
d[index].append(current.data)
else:
d[index] = [current.data]
if current.left != None:
q.append([current.left, index - 1])
if current.right != None:
q.append([current.right, index + 1])
values = []
for i in sorted(d.keys()):
values.append(d[i])
ans = []
for i in values:
ans.append(i[0])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR LIST LIST VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR IF VAR NONE EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def func(self, e):
return e[0]
def dfs(self, root, dic, col, row):
if root == None:
return
if col in dic:
if row < dic[col][0]:
dic[col] = row, root.data
else:
dic[col] = row, root.data
self.dfs(root.left, dic, col - 1, row + 1)
self.dfs(root.right, dic, col + 1, row + 1)
def topView(self, root):
dic = {}
self.dfs(root, dic, 0, 0)
result = []
for i in sorted(dic):
result.append(dic[i][1])
return result | CLASS_DEF FUNC_DEF RETURN VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
dic = {}
max1 = min1 = 0
q = [[root, 0]]
while q:
node, dist = q.pop(0)
if dist not in dic:
dic[dist] = node.data
if node.left:
q.append([node.left, dist - 1])
if node.right:
q.append([node.right, dist + 1])
min1 = min(min1, dist)
max1 = max(max1, dist)
ans = []
for i in range(min1, max1 + 1):
ans.append(dic[i])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
res = []
m = {}
def dfs(root, row, col):
if root is None:
return
dfs(root.left, row + 1, col - 1)
dfs(root.right, row + 1, col + 1)
if col in m:
if row in m[col]:
m[col][row].append(root.data)
else:
m[col][row] = [root.data]
else:
m[col] = {}
m[col][row] = [root.data]
dfs(root, 0, 0)
for k1 in sorted(m):
for k2 in sorted(m[k1]):
res.append(m[k1][k2][0])
break
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
mp = {}
def util(root, x, y, map):
if root:
util(root.left, x - 1, y + 1, map)
if x in map and map[x][1] > y or x not in map:
map[x] = root.data, y
util(root.right, x + 1, y + 1, map)
util(root, 0, 0, mp)
return [mp[keys][0] for keys in sorted(mp.keys())] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR RETURN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topviewu(self, root, d, vd, lv):
if not root:
return
if vd in d:
if d[vd][0] > lv:
d[vd] = lv, root.data
else:
d[vd] = lv, root.data
self.topviewu(root.left, d, vd - 1, lv + 1)
self.topviewu(root.right, d, vd + 1, lv + 1)
def topView(self, root):
d = dict()
ans = []
self.topviewu(root, d, 0, 0)
for e in sorted(d):
ans.append(d[e][1])
return ans | CLASS_DEF FUNC_DEF IF VAR RETURN IF VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
map, queue, res, hd = {}, [], [], 0
if root is None:
return
queue.append((root, 0))
while queue:
curr, hd = queue.pop(0)
if hd not in map.keys():
map[hd] = [curr.data]
if curr.left:
queue.append((curr.left, hd - 1))
if curr.right:
queue.append((curr.right, hd + 1))
for i in sorted(map):
res += map[i]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR DICT LIST LIST NUMBER IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR LIST VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
queue = [[root, 0, 0]]
res = {}
while queue:
fwd = []
while queue:
val = queue.pop(0)
if val[0].left:
fwd.append([val[0].left, val[1] + 1, val[2] - 1])
if val[0].right:
fwd.append([val[0].right, val[1] + 1, val[2] + 1])
if val[2] not in res:
res[val[2]] = val[0]
queue = fwd
ans = []
for i in sorted(res.keys()):
ans.append(res[i].data)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST VAR NUMBER NUMBER ASSIGN VAR DICT WHILE VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
ans = []
def dfs(node, lvl, dis):
if not node:
return
ans.append((node.data, lvl, dis))
dfs(node.left, lvl + 1, dis - 1)
dfs(node.right, lvl + 1, dis + 1)
dfs(root, 0, 0)
ans.sort(key=lambda x: x[1])
ans.sort(key=lambda x: x[2])
fans = []
lastI = -float("inf")
for n in ans:
cVa, clv, cdi = n
if cdi > lastI:
fans.append(cVa)
lastI = cdi
return fans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
if root is None:
return []
queue = deque()
queue.append([root, 0])
hashmap = {}
mini, maxi = 0, 0
while queue:
temp = queue.popleft()
node = temp[0]
height = temp[1]
if not height in hashmap:
hashmap[height] = node.data
if node.left:
queue.append([node.left, height - 1])
mini = min(mini, height - 1)
if node.right:
queue.append([node.right, height + 1])
maxi = max(maxi, height + 1)
ans = []
for i in range(mini, maxi + 1):
ans.append(hashmap[i])
return ans | CLASS_DEF FUNC_DEF IF VAR NONE RETURN LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
vert_map = {}
def recursive(root, dist=0, lvl=0):
if root is None:
return
if dist in vert_map:
if vert_map[dist][0] > lvl:
vert_map[dist] = lvl, root.data
else:
vert_map[dist] = lvl, root.data
recursive(root.left, dist - 1, lvl + 1)
recursive(root.right, dist + 1, lvl + 1)
recursive(root)
return [vert_map[k][1] for k in sorted(vert_map)] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF NUMBER NUMBER IF VAR NONE RETURN IF VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR NUMBER VAR FUNC_CALL VAR VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
ans = []
queue = deque([(root, 0)])
views = {}
min_pos = max_pos = 0
while queue:
node, pos = queue.popleft()
if pos not in views:
views[pos] = node.data
min_pos = min(min_pos, pos)
max_pos = max(max_pos, pos)
if node.left:
queue.append((node.left, pos - 1))
if node.right:
queue.append((node.right, pos + 1))
for i in range(min_pos, max_pos + 1):
ans.append(views[i])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
q = [(0, root)]
dict = {}
list1 = []
while q:
node = q.pop(0)
list1.append(node)
if node[0] not in dict:
dict[node[0]] = node[1].data
if node[1].left is not None:
q.append((node[0] - 1, node[1].left))
if node[1].right is not None:
q.append((node[0] + 1, node[1].right))
k = sorted(dict.items(), key=lambda x: x[0])
list2 = []
for i in k:
list2.append(i[1])
return list2 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NONE EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NONE EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
levels = []
q = []
d = {}
levels.append(0)
q.append(root)
while len(q) != 0:
curr = q.pop(0)
lvl = levels.pop(0)
if lvl not in d:
d[lvl] = curr.data
if curr.left != None:
q.append(curr.left)
levels.append(lvl - 1)
if curr.right != None:
q.append(curr.right)
levels.append(lvl + 1)
ans = []
for i in sorted(d.keys()):
ans.append(d[i])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
my_map = {}
def topView(root, dist, level):
if root == None:
return
if dist not in my_map or level < my_map[dist][1]:
my_map[dist] = root.data, level
topView(root.left, dist - 1, level + 1)
topView(root.right, dist + 1, level + 1)
ans = []
topView(root, 0, 0)
for key in sorted(my_map.keys()):
ans.append(my_map.get(key)[0])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NONE RETURN IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
ans = []
q = []
q.append([root, 0, 0])
d = {}
while q:
node, pos, lev = q.pop(0)
if pos not in d.keys():
d[pos] = node.data
if node.left:
q.append([node.left, pos - 1, lev + 1])
if node.right:
q.append([node.right, pos + 1, lev + 1])
for i in sorted(d):
ans.append(d[i])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER ASSIGN VAR DICT WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | import sys
class Solution:
def topView(self, root):
q = []
map = {}
mini = sys.maxsize
q.append((root, 0))
while len(q) != 0:
n = len(q)
for i in range(n):
node, l = q.pop(0)
if l not in map:
map[l] = node.data
mini = min(mini, l)
if node.left:
q.append((node.left, l - 1))
if node.right:
q.append((node.right, l + 1))
ans = []
while mini in map:
ans.append(map[mini])
mini = mini + 1
return ans | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
ans = []
hd = 0
MIN = 999
d = dict()
queue = deque()
queue.append([root, hd])
while queue:
curr, hd = queue.popleft()
if hd not in d:
MIN = min(hd, MIN)
d[hd] = curr.data
if curr.left:
queue.append([curr.left, hd - 1])
if curr.right:
queue.append([curr.right, hd + 1])
while MIN in d:
ans.append(d[MIN])
MIN += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
if not root:
return []
q = [[root, 0]]
topview = {}
while q:
node, level = q.pop(0)
if level not in topview:
topview[level] = node.data
if node.left:
q.append([node.left, level - 1])
if node.right:
q.append([node.right, level + 1])
return [topview[level] for level in sorted(topview.keys())] | CLASS_DEF FUNC_DEF IF VAR RETURN LIST ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR DICT WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
q = []
h = 0
q.append([root, h])
mp = dict()
mini = 0
maxi = 0
while len(q) > 0:
node, hd = q[0]
q.pop(0)
if hd not in mp:
mp[hd] = node.data
mini = min(mini, hd)
maxi = max(maxi, hd)
if node.left:
q.append([node.left, hd - 1])
if node.right:
q.append([node.right, hd + 1])
a = []
for i in range(mini, maxi + 1):
a.append(mp[i])
return a | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
res = []
if root is None:
return
else:
visit = {}
q = []
hd = 0
q.append([root, hd])
while len(q) != 0:
rn, hd = q.pop(0)
if hd not in visit:
visit[hd] = [rn.data]
else:
visit[hd].append(rn.data)
if rn.left is not None:
q.append([rn.left, hd - 1])
if rn.right is not None:
q.append([rn.right, hd + 1])
for i in sorted(visit.keys()):
res.append(visit[i][0])
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR NONE RETURN ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
def traversal(root, x, y):
if not root:
return
if y not in d:
d[y] = root.data, x
elif d[y][1] > x:
d[y] = root.data, x
traversal(root.left, x + 1, y - 1)
traversal(root.right, x + 1, y + 1)
result = []
d = dict()
traversal(root, 0, 0)
m = min(d)
for i in range(m, m + len(d)):
result.append(d[i][0])
return result | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
d = dict()
if root == None:
return []
q = [(root, 0)]
while q:
nod, h = q.pop(0)
if h not in d:
d[h] = nod.data
if nod.left:
q.append([nod.left, h - 1])
if nod.right:
q.append([nod.right, h + 1])
d = sorted(d.items())
ans = []
for k, v in d:
ans.append(v)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR NONE RETURN LIST ASSIGN VAR LIST VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
def top(root, level):
q = deque()
q.append((0, root))
d = {}
while q:
for i in range(len(q)):
level, node = q.popleft()
if level not in d:
d[level] = node.data
if node.left:
q.append((level - 1, node.left))
if node.right:
q.append((level + 1, node.right))
return d
le = float("inf")
r = float("-inf")
d = top(root, 0)
for i in d:
le = min(le, i)
r = max(r, i)
res = []
for i in range(le, r + 1):
res.append(d[i])
return res | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR DICT WHILE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
ans = []
dic = {}
if root is None:
return ans
queue = []
queue.append([root, 0])
while queue:
elem = queue.pop(0)
node = elem[0]
line = elem[-1]
if dic.get(line, -1) == -1:
dic[line] = node.data
if node.left is not None:
queue.append([node.left, line - 1])
if node.right is not None:
queue.append([node.right, line + 1])
key = dic.keys()
for i in sorted(key):
ans.append(dic[i])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT IF VAR NONE RETURN VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given below is a binary tree. The task is to print the top view of binary tree. Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. For the given below tree
1
/ \
2 3
/ \ / \
4 5 6 7
Top view will be: 4 2 1 3 7
Note: Return nodes from leftmost node to rightmost node. Also if 2 nodes are outside the shadow of the tree and are at same position then consider the extreme ones only(i.e. leftmost and rightmost).
For ex - 1 2 3 N 4 5 N 6 N 7 N 8 N 9 N N N N N will give 8 2 1 3 as answer. Here 8 and 9 are on the same position but 9 will get shadowed.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \ / \
40 60 90 100
Output: 40 20 10 30 100
Your Task:
Since this is a function problem. You don't have to take input. Just complete the function topView() that takes root node as parameter and returns a list of nodes visible from the top view from left to right.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(N).
Constraints:
1 β€ N β€ 10^{5}
1 β€ Node Data β€ 10^{5} | class Solution:
def topView(self, root):
if not root:
return []
queue = []
queue.append([root, 0])
seen = {}
ret = []
while queue:
n = len(queue)
for i in range(n):
curr = queue.pop(0)
if curr[1] not in seen.keys():
seen[curr[1]] = curr[0].data
if curr[0].left:
queue.append([curr[0].left, curr[1] - 1])
if curr[0].right:
queue.append([curr[0].right, curr[1] + 1])
val = seen.keys()
val = sorted(val)
for i in val:
ret.append(seen[i])
return ret | CLASS_DEF FUNC_DEF IF VAR RETURN LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 β€ |S| β€ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | s = input()
count = 0
map = {}
map[0, 0] = 1
A, B, C = 0, 0, 0
ans = 0
for i in s:
if i == "A":
A += 1
elif i == "B":
B += 1
elif i == "C":
C += 1
temp = C - A, C - B
if temp in map:
ans += map[temp]
map[temp] += 1
else:
map[temp] = 1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Mike likes strings. He is also interested in algorithms. A few days ago he discovered for himself a very nice problem:
You are given an AB-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s and 'B'-s.
Do you know how to solve it? Good. Mike will make the problem a little bit more difficult for you.
You are given an ABC-string S. You need to count the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
A string is called AB-string if it doesn't contain any symbols except 'A' or 'B'. A string is called ABC-string if it doesn't contain any symbols except 'A', 'B' or 'C'.
------ Input ------
The first line of the input contains an ABC-string S.
------ Output ------
Your output should contain the only integer, denoting the number of substrings of S, which have an equal number of 'A'-s, 'B'-s and 'C'-s.
The answer can go above a 32-bit integer. Please, use 64-bit integers for storing and processing data.
------ Constraints ------
1 β€ |S| β€ 1 000 000; where |S| denotes the length of the given ABC-string.
------ Example ------
Input:
ABACABA
Output:
2
------ Explanation ------
In the example you should count S[2..4] = "BAC" and S[4..6] = "CAB". | string = input()
a, b, c = 0, 0, 0
substr = dict()
substr[0, 0, 0] = 1
for char in string:
a, b, c = a + (char == "A"), b + (char == "B"), c + (char == "C")
new_sub = a - min(a, b, c), b - min(a, b, c), c - min(a, b, c)
if new_sub in substr:
substr[new_sub] += 1
else:
substr[new_sub] = 1
solution = sum(map(lambda x: x * (x - 1) // 2, substr.values()))
print(solution) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR STRING BIN_OP VAR VAR STRING BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.