description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
x, y = [int(i) for i in input().split(" ")] exp = [[i + 1, int(j), 0] for i, j in enumerate(input().split(" "))] q = {} exps = {} scores = {} exp = sorted(exp, key=lambda y: y[1], reverse=True) for j, i in enumerate(exp): exps[i[0]] = i[1] scores[i[1]] = j for i in range(1, y + 1): a, b = [int(i) for i in input().split(" ")] if a not in q: q[a] = [] if b not in q: q[b] = [] if exps[a] > exps[b]: q[a].append(b) elif exps[b] > exps[a]: q[b].append(a) for i in range(0, len(exp)): j = scores[exp[i][1]] - i try: exp[i][2] = len(exp) - i - j - len(q[exp[i][0]]) - 1 except: exp[i][2] = len(exp) - i - j - 1 exp = sorted(exp, key=lambda y: y[0]) for i in exp: print(i[2], end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR LIST IF VAR VAR ASSIGN VAR VAR LIST IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = map(int, input().split()) r = list(map(int, input().split())) a = [] d = dict() for i in range(n): a.append([r[i], i + 1]) a.sort() summ = 0 cur = 1 for i in range(n): if i >= 1 and a[i - 1][0] != a[i][0]: summ += cur cur = 1 elif i >= 1 and a[i - 1][0] == a[i][0]: cur += 1 a[i].append(summ) d[a[i][1]] = i for i in range(k): x, y = map(int, input().split()) x = d[x] y = d[y] if a[x][0] > a[y][0]: a[x][2] -= 1 elif a[y][0] > a[x][0]: a[y][2] -= 1 def f(x): return x[1] a.sort(key=f) for i in range(n): print(a[i][2], end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = map(int, input().split()) lst = [int(x) for x in input().split()] cnt = [0] * n for x in range(k): a, b = map(int, input().split()) a -= 1 b -= 1 if lst[a] < lst[b]: cnt[b] += 1 elif lst[a] > lst[b]: cnt[a] += 1 v = [] for x in range(n): v.append((lst[x], x)) v.sort() ans = [0] * n ans[v[0][1]] = 0 cnt0 = 0 for x in range(1, n): if v[x][0] == v[x - 1][0]: cnt0 += 1 else: cnt0 = 0 ans[v[x][1]] = x - cnt0 - cnt[v[x][1]] print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
aa = input() n, k = [int(s) for s in aa.split(" ")] aa = input() r = [int(s) for s in aa.split(" ")] a = [] for i in range(n): a.append({"n": i, "r": r[i]}) a.sort(key=lambda x: x["r"]) res = [0] * n prev_r = a[0]["r"] prev_prog_num = 0 for i in range(n): ai = a[i] if ai["r"] > prev_r: prev_prog_num = i prev_r = ai["r"] res[ai["n"]] = prev_prog_num for i in range(k): aa = input() x, y = [int(s) for s in aa.split(" ")] x = x - 1 y = y - 1 if r[x] > r[y]: res[x] = res[x] - 1 if r[y] > r[x]: res[y] = res[y] - 1 print(" ".join([str(x) for x in res]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR DICT STRING STRING VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR ASSIGN VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
def solve(n, m, a, adj): a = sorted(a) res = [] for i in range(n): res.append(0) equal = 0 for i in range(n): pos = a[i][1] if i == 0: res[pos] = 0 continue if a[i][0] == a[i - 1][0]: equal += 1 else: equal = 0 res[pos] = i - equal - len(adj[pos]) for i in res: print(i, end=" ") n, m = map(int, input().split()) tmp = list(map(int, input().split())) a = [] adj = [] for i in range(n): a.append((tmp[i], i)) adj.append([]) for i in range(m): x, y = map(int, input().split()) x -= 1 y -= 1 if a[x][0] < a[y][0]: adj[y].append(x) elif a[x][0] > a[y][0]: adj[x].append(y) solve(n, m, a, adj)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = map(int, input().split()) a = input().split() l = [[int(a[i]), i, 0] for i in range(n)] s = [] for i in range(k): x, y = map(int, input().split()) x -= 1 y -= 1 s.append([x, y]) if l[x][0] > l[y][0]: l[x][2] += 1 elif l[x][0] < l[y][0]: l[y][2] += 1 l.sort() p = l[0][0] k = 1 for i in range(1, n): if l[i][0] == p: l[i][2] += k k += 1 else: k = 1 p = l[i][0] r = [0] * n for i in range(n): r[l[i][1]] = max(i - l[i][2], 0) print(*r)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
from sys import stdin def lower_bound(sa, target): low = 0 high = len(sa) while low < high: mid = low + high >> 1 if sa[mid] >= target: high = mid else: low = mid + 1 return low def main(): n, k = map(int, stdin.readline().split()) ar = list(map(int, stdin.readline().split())) count = [0] * n for _ in range(k): x, y = map(int, stdin.readline().split()) if ar[x - 1] > ar[y - 1]: count[x - 1] += 1 elif ar[x - 1] < ar[y - 1]: count[y - 1] += 1 sa = sorted(ar) for i in range(n): i1 = lower_bound(sa, ar[i]) i2 = count[i] print(i1 - i2, end=" ") print() main()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = map(int, input().split()) skills = list(map(int, input().split())) quarrels = [0] * n for _ in range(k): x, y = map(int, input().split()) x -= 1 y -= 1 if skills[x] > skills[y]: quarrels[x] += 1 elif skills[y] > skills[x]: quarrels[y] += 1 result = [0] * n next_p = 1 skills = sorted(enumerate(skills), key=lambda x: -x[1]) for i, skill in skills[:-1]: while next_p < n and skills[next_p][1] == skill: next_p += 1 result[i] = n - next_p - quarrels[i] print(" ".join(map(str, result)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
def binsearch(nums, target): left = 0 right = len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid elif nums[mid] > target: right = mid - 1 elif nums[mid] < target: left = mid + 1 return left n, k = map(int, input().split()) arr = [int(z) for z in input().split()] sortedarr = sorted(arr) quarrels = [] for q in range(k): a, b = map(int, input().split()) if a > b: a, b = b, a quarrels.append([a, b]) res = {} for i in range(n): pl = binsearch(sortedarr, arr[i] - 0.1) res[i] = pl for i in quarrels: a, b = i a -= 1 b -= 1 if arr[a] < arr[b]: res[b] -= 1 if arr[b] < arr[a]: res[a] -= 1 print(*res.values())
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
mer, pair = list(map(int, input().split(" "))) skills = list(map(int, input().split(" "))) skills = [0] + skills quarrelCount = [0] * (mer + 1) sortskills = sorted(skills) for i in range(pair): v1, v2 = map(int, input().split(" ")) if skills[v1] > skills[v2]: quarrelCount[v1] += 1 elif skills[v1] < skills[v2]: quarrelCount[v2] += 1 for i in range(1, len(skills)): low = 0 high = len(sortskills) while low < high - 1: mid = (low + high) // 2 if skills[i] <= sortskills[mid]: high = mid else: low = mid print(max(high - quarrelCount[i] - 1, 0))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = map(int, input().split()) ar = list(map(int, input().split())) cross = [0] * n res = [0] * n nar = list(sorted([[ar[x], x] for x in range(n)])) cur = ["1", 0] for x in nar: if x[0] == cur[0]: cur[1] += 1 cross[x[1]] += cur[1] else: cur[0] = x[0] cur[1] = 0 for x in range(k): a, b = map(lambda x: int(x) - 1, input().split()) if ar[a] != ar[b]: cross[max(a, b, key=lambda x: ar[x])] += 1 for x in range(n): res[x] -= cross[x] res[nar[x][1]] += x print(*res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
def main(): n, k = map(int, input().split(" ")) rs = list(enumerate(map(int, input().split(" ")))) pairs = [] for i in range(k): x, y = map(int, input().split(" ")) pairs.append((x, y)) rating = dict(rs) rs.sort(key=lambda x: x[1]) counts = dict((x, 0) for x in range(n)) cnt = 0 r_prev = None for l, (m, r) in enumerate(rs): if r != r_prev: cnt = l counts[m] = cnt r_prev = r for a, b in pairs: ra = rating[a - 1] rb = rating[b - 1] if ra > rb: counts[a - 1] -= 1 elif ra < rb: counts[b - 1] -= 1 print(" ".join(map(str, counts.values()))) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = list(map(int, input().split())) arr = list(map(int, input().split())) person_to_skill = {} skill_to_numMentor = {} person_to_numMentor = {} for person in range(len(arr)): person_to_skill[person] = arr[person] arr = sorted(arr) temp = -1 for i in range(len(arr)): if arr[i] != temp: skill_to_numMentor[arr[i]] = i temp = arr[i] for person in range(n): person_to_numMentor[person] = skill_to_numMentor[person_to_skill[person]] for quarrel in range(k): p1, p2 = list(map(int, input().split())) if person_to_skill[p1 - 1] > person_to_skill[p2 - 1]: person_to_numMentor[p1 - 1] -= 1 if person_to_skill[p2 - 1] > person_to_skill[p1 - 1]: person_to_numMentor[p2 - 1] -= 1 s = "" s += str(person_to_numMentor[0]) for i in range(1, n): s += " " + str(person_to_numMentor[i]) print(s)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = map(int, input().split()) r = list(map(int, input().split())) h = sorted(r) dic = dict() dic[h[0]] = 0 for i in range(1, n): if h[i - 1] != h[i]: dic[h[i]] = i out = [dic[r[i]] for i in range(n)] for i in range(k): a, b = map(lambda i: int(i) - 1, input().split()) if r[a] > r[b]: out[a] -= 1 elif r[a] < r[b]: out[b] -= 1 for i in out: print(i, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = map(int, input().split()) a = list(map(int, input().split())) rec = [] rec1 = {} for i in range(n): rec.append((i, a[i])) rec1[i + 1] = a[i] rec = sorted(rec, key=lambda s: s[1]) num = [0] * n j = 0 for i in range(n): num[rec[i][0]] = i i = 1 while i < n: if rec[i - 1][1] == rec[i][1]: j = 1 while i < n and rec[i - 1][1] == rec[i][1]: num[rec[i][0]] -= j j += 1 i += 1 i += 1 for i in range(k): x, y = map(int, input().split()) if rec1[x] < rec1[y]: num[y - 1] -= 1 elif rec1[y] < rec1[x]: num[x - 1] -= 1 print(" ".join(map(str, num)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
class programmer: def __init__(self, ind, skill): self.ind = ind self.skill = skill yoar = list(map(int, input().split())) n = yoar[0] k = yoar[1] skillar = list(map(int, input().split())) skillar.insert(0, 0) ansar = [] for i in range(n + 1): ansar.append(0) prar = [] abhitak = {} for i in range(1, n + 1): prar.append(programmer(i, skillar[i])) abhitak[skillar[i]] = 0 prar.sort(key=lambda programmer: programmer.skill) prar.insert(0, 0) dushman = {} for i in range(1, n + 1): dushman[i] = [] for i in range(k): yoar = list(map(int, input().split())) a = yoar[0] b = yoar[1] dushman[a].append(b) dushman[b].append(a) for i in range(1, n + 1): curr = prar[i] curind = curr.ind curskill = curr.skill curans = i - 1 - abhitak[curskill] for j in dushman[curind]: if skillar[j] < curskill: curans -= 1 ansar[curind] = curans abhitak[curskill] += 1 for i in range(1, n + 1): print(ansar[i], end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
def ke(i): return a[i] n, m = map(int, input().split()) a = list(map(int, input().split())) b = [] for i in range(n): b.append(i) b.sort(key=ke) ans = [0] * n k = 0 for i in range(1, n): if a[b[i]] == a[b[i - 1]]: k += 1 ans[b[i]] = i - k else: k = 0 ans[b[i]] = i for i in range(m): r1, r2 = map(int, input().split()) if a[r1 - 1] > a[r2 - 1]: ans[r1 - 1] -= 1 elif a[r1 - 1] < a[r2 - 1]: ans[r2 - 1] -= 1 for i in ans: print(i, end=" ")
FUNC_DEF RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = map(int, input().split()) r = list(map(int, input().split())) xy = [tuple(map(lambda _: int(_) - 1, input().split())) for _ in range(k)] ir = list(enumerate(r)) ir.sort(key=lambda _: (_[1], _[0])) a, i, ia = 0, 0, [[ir[0][0], 0]] for i in range(1, n): if ir[i][1] > ir[i - 1][1]: a = i ia.append([ir[i][0], a]) ia.sort() for x, y in xy: if r[x] > r[y]: ia[x][1] -= 1 if r[x] < r[y]: ia[y][1] -= 1 print(*list(map(lambda _: _[1], ia)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER LIST LIST VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = list(map(int, input().split())) r = list(map(int, input().split())) index = sorted(range(n), key=lambda x: r[x]) m = [(0) for _ in range(n)] ls = 0 seq = 1 for i in range(1, n): if r[index[i]] > r[index[i - 1]]: ls += seq seq = 1 if r[index[i]] == r[index[i - 1]]: seq += 1 m[index[i]] = ls for _ in range(k): i, j = list(map(int, input().split())) i -= 1 j -= 1 if r[i] < r[j]: m[j] -= 1 elif r[i] > r[j]: m[i] -= 1 print(" ".join([str(x) for x in m]))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
def sol(): n, w = list(map(int, input().split())) arr = list(map(int, input().split())) out = [0] * n nArr = [] def heh(el): return el[0] for i in range(n): nArr.append([arr[i], i]) nArr.sort(key=heh) l = 99999999999999 t = 0 for i in range(n): if nArr[i][0] != l: t = 0 l = nArr[i][0] else: t += 1 out[nArr[i][1]] += i - t for i in range(w): a, b = list(map(int, input().split())) if arr[a - 1] > arr[b - 1]: out[a - 1] -= 1 if arr[b - 1] > arr[a - 1]: out[b - 1] -= 1 print(*out) sol()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FUNC_DEF RETURN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = [int(i) for i in input().split()] l = [int(i) for i in input().split()] l_new = [] for i in range(n): l_new.append((l[i], i)) l_new = sorted(l_new) graph = [["o"] for i in range(n)] for i in range(k): a1, a2 = [int(i) for i in input().split()] graph[a1 - 1].append(a2 - 1) graph[a2 - 1].append(a1 - 1) ANS = {} been = set() d = {} for i in l: if i not in been: been.add(i) d[i] = False for pers in range(n): i_pers = l_new[pers][0] num_pers = l_new[pers][1] ans = pers - d[i_pers] ssor = graph[num_pers] if len(ssor) > 1: for i in range(1, len(ssor)): if l[ssor[i]] < i_pers: ans -= True d[i_pers] += True ANS[num_pers] = ans for i in range(n): print(ANS[i], end=" ") print("\n")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = map(int, input().split()) m = list(map(int, input().split())) new_m = [(m[i], i) for i in range(n)] new_m.sort() ans = [(0) for i in range(n)] last = -1 for i in range(1, len(new_m)): if new_m[i][0] == new_m[i - 1][0]: ans[new_m[i][1]] = ans[new_m[i - 1][1]] else: ans[new_m[i][1]] = i for i in range(k): a, b = map(int, input().split()) if m[a - 1] > m[b - 1]: ans[a - 1] -= 1 elif m[a - 1] != m[b - 1]: ans[b - 1] -= 1 print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
def lei(a): l = sorted(zip(a, list(range(len(a))))) t = [None] * len(a) for i, e in enumerate(l): t[e[1]] = e[0], i d = {} for e in t: if e[0] not in d or d[e[0]] > e[1]: d[e[0]] = e[1] tt = [] for e in t: tt.append(d[e[0]]) return tt a = input().split(" ") a = [int(e) for e in a] l = input().split(" ") l = [int(e) for e in l] assert a[0] == len(l) smaller_quarrel = [0] * len(l) for i in range(a[1]): ll = input().split(" ") ll = [int(e) for e in ll] i1 = ll[0] - 1 i2 = ll[1] - 1 if l[i1] > l[i2]: smaller_quarrel[i1] += 1 if l[i2] > l[i1]: smaller_quarrel[i2] += 1 tt = lei(l) for i in range(len(tt)): print(tt[i] - smaller_quarrel[i], end=" ")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR STRING
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
[n, k] = [int(x) for x in input().split()] r = [int(x) for x in input().split()] progs = [[i, x, 0, 0] for i, x in enumerate(r)] IND = 0 RATE = 1 REL = 2 RES = 3 i = 0 while i < k: [a, b] = [int(x) for x in input().split()] a -= 1 b -= 1 if progs[a][RATE] > progs[b][RATE]: progs[a][REL] += 1 if progs[a][RATE] < progs[b][RATE]: progs[b][REL] += 1 i += 1 progs = sorted(progs, key=lambda p: p[RATE]) i = 1 count = 0 while i < n: cur = progs[i] prev = progs[i - 1] if cur[RATE] > prev[RATE]: count = i cur[RES] = count - cur[REL] i += 1 progs = sorted(progs, key=lambda p: p[IND]) for p in progs: print(p[RES], end=" ")
ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
params = [int(s) for s in input().split(" ")] n = params[0] k = params[1] skills = [int(s) for s in input().split(" ")] indexes_sorted = [b[0] for b in sorted(enumerate(skills), key=lambda i: i[1])] bad_relations = {} for i in range(k): items = [int(s) for s in input().split(" ")] first = items[0] - 1 second = items[1] - 1 if skills[first] > skills[second]: bad_relations[first] = bad_relations.get(first, 0) + 1 elif skills[second] > skills[first]: bad_relations[second] = bad_relations.get(second, 0) + 1 num_the_same = 0 results = {} prev = None for idx, index in enumerate(indexes_sorted): skill = skills[index] if skill == prev: num_the_same += 1 else: num_the_same = 0 cnt = idx - num_the_same - bad_relations.get(index, 0) if cnt < 0: cnt = 0 results[index] = cnt prev = skill final = "" for i in range(n): final += str(results[i]) + " " print(final.strip())
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
import sys input = sys.stdin.readline n, k = map(int, input().split()) ratings = list(map(lambda x: int(x) - 1, input().split())) q = [0] * n for _ in range(k): a, b = map(lambda x: int(x) - 1, input().split()) d = ratings[a] - ratings[b] if d > 0: q[a] += 1 elif d < 0: q[b] += 1 res = [0] * n rating_idx = dict() for i, r in enumerate(ratings): if r in rating_idx: rating_idx[r].append(i) else: rating_idx[r] = [i] cur_count = 0 for r in sorted(rating_idx.keys()): for idx in rating_idx[r]: res[idx] = cur_count - q[idx] cur_count += len(rating_idx[r]) print(*res)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = list(map(int, input().split())) r = list(map(int, input().split())) r = list(map(lambda x: (x[1], x[0]), enumerate(r))) a = [(0) for i in r] argues = [[] for i in r] for i in range(k): x, y = list(map(int, input().split())) argues[x - 1].append(y - 1) argues[y - 1].append(x - 1) if r[x - 1][0] > r[y - 1][0]: a[x - 1] += 1 elif r[x - 1][0] < r[y - 1][0]: a[y - 1] += 1 r.sort() res = [(0) for i in r] d = {} for i in range(n): answ = i - a[r[i][1]] if r[i][0] not in d.keys(): d[r[i][0]] = 1 else: answ -= d[r[i][0]] d[r[i][0]] += 1 res[r[i][1]] = max(0, answ) for i in res: print(i, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
n, k = list(map(int, input().split())) r = list(map(int, input().split())) b = [(0) for i in range(n)] for i in range(k): x, y = list(map(int, input().split())) if r[x - 1] > r[y - 1]: b[x - 1] += 1 if r[y - 1] > r[x - 1]: b[y - 1] += 1 p = r.copy() p.sort(reverse=True) c = {} d = {} for i in range(n): c[p[i]] = 0 for i in range(n): c[p[i]] += 1 s = 0 for i in c: s = s + c[i] d[i] = n - s a = [] for i in range(n): a.append(d[r[i]] - b[i]) print(*a)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
from sys import stdin input = stdin.buffer.readline def func(l, indx): ans = -1 start = 0 end = indx while start <= end: mid = (start + end) // 2 if l[mid][0] >= l[indx][0]: end = mid - 1 else: ans = mid start = mid + 1 return ans n, k = map(int, input().split()) arr = [int(x) for x in input().split()] l = [] for i in range(n): l.append((arr[i], i)) l.sort() dp = [[] for i in range(n)] for i in range(k): x, y = map(int, input().split()) dp[x - 1].append(y - 1) dp[y - 1].append(x - 1) result = [0] * n for indx in range(n): x = func(l, indx) if x != -1: ll = x + 1 for j in dp[l[indx][1]]: if arr[l[indx][1]] > arr[j]: ll = ll - 1 result[l[indx][1]] = ll print(*result)
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$. A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel. You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. -----Input----- The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel. The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer. Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. -----Output----- Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. -----Examples----- Input 4 2 10 4 10 15 1 2 4 3 Output 0 0 1 2 Input 10 4 5 4 1 5 4 3 7 1 2 5 4 6 2 1 10 8 3 5 Output 5 4 0 5 3 3 9 0 2 5 -----Note----- In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
R = lambda: map(int, input().split()) n, k = R() r = list(R()) h = sorted(r) d = {} d[h[0]] = 0 for i in range(1, n): if h[i - 1] != h[i]: d[h[i]] = i res = [0] * n for i in range(n): res[i] = d[r[i]] for i in range(k): a, b = R() a -= 1 b -= 1 if r[a] > r[b]: res[a] -= 1 elif r[a] < r[b]: res[b] -= 1 res = list(map(str, res)) print(" ".join(res))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number. In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better). After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics: * it starts and ends in the same place — the granny's house; * the route goes along the forest paths only (these are the segments marked black in the picture); * the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway); * the route cannot cross itself; * there shouldn't be any part of dense forest within the part marked out by this route; You should find the amount of such suitable oriented routes modulo 1000000009. <image> The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example. Input The input data contain the only even integer n (2 ≤ n ≤ 106). Output Output the only number — the amount of Peter's routes modulo 1000000009. Examples Input 2 Output 10 Input 4 Output 74
__author__ = "Darren" def solve(): n = int(input()) a, b, c, mod = 2, 2, 4, 1000000009 while n > 2: a = (a << 1) % mod c = c * (a - 3) % mod b = (b + c) % mod n -= 2 print((b * b + 1) * 2 % mod) solve()
ASSIGN VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number. In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better). After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics: * it starts and ends in the same place — the granny's house; * the route goes along the forest paths only (these are the segments marked black in the picture); * the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway); * the route cannot cross itself; * there shouldn't be any part of dense forest within the part marked out by this route; You should find the amount of such suitable oriented routes modulo 1000000009. <image> The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example. Input The input data contain the only even integer n (2 ≤ n ≤ 106). Output Output the only number — the amount of Peter's routes modulo 1000000009. Examples Input 2 Output 10 Input 4 Output 74
a, b, c, m = 1, 2, 4, 10**9 + 9 n = int(input()) for i in range(1, n // 2): c = c * a % m b = (b + c) % m a = (2 * a + 3) % m print((b * b + 1) * 2 % m)
ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number. In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better). After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics: * it starts and ends in the same place — the granny's house; * the route goes along the forest paths only (these are the segments marked black in the picture); * the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway); * the route cannot cross itself; * there shouldn't be any part of dense forest within the part marked out by this route; You should find the amount of such suitable oriented routes modulo 1000000009. <image> The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example. Input The input data contain the only even integer n (2 ≤ n ≤ 106). Output Output the only number — the amount of Peter's routes modulo 1000000009. Examples Input 2 Output 10 Input 4 Output 74
n = int(input()) z = 8 y = [] for i in range(n // 2 - 1): y.append((z - 3) % 1000000009) z *= 2 z %= 1000000009 if n == 2: print(10) else: a = 4 for i in range(n // 2, 2, -1): a *= y[i - 3] a += 4 a %= 1000000009 a += 2 print(2 * (a**2 + 1) % 1000000009)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: maxlen = 0 dt = collections.defaultdict(lambda: [0, 0]) def dfs(root, dt): nonlocal maxlen if root.left: dfs(root.left, dt) dt[root][0] = dt[root.left][1] + 1 else: dt[root][0] = 0 if root.right: dfs(root.right, dt) dt[root][1] = dt[root.right][0] + 1 else: dt[root][1] = 0 maxlen = max(maxlen, dt[root][0], dt[root][1]) dfs(root, dt) return maxlen
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: longest = 0 if not root: return longest def helper(root, level, direction): nonlocal longest if level > longest: longest = level if direction: if root.left: helper(root.left, level + 1, not direction) if root.right: helper(root.right, 1, direction) else: if root.right: helper(root.right, level + 1, not direction) if root.left: helper(root.left, 1, direction) if root.right: helper(root.right, 1, True) if root.left: helper(root.left, 1, False) if not root.left and not root.right: return 0 return longest
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER IF VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: ans = 0 nodes = [root] self.dp = {} while len(nodes) != 0: tmp = [] for node in nodes: if node.left is not None: tmp.append(node.left) if node.right is not None: tmp.append(node.right) ans = max(ans, self.helper(node, 1)) ans = max(ans, self.helper(node, 0)) nodes = tmp return ans - 1 def helper(self, node, status): if node is None: return 0 if (node, status) in self.dp: return self.dp[node, status] if status == 1: ans = self.helper(node.right, 0) else: ans = self.helper(node.left, 1) self.dp[node, status] = 1 + ans return 1 + ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR DICT WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR RETURN BIN_OP NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: def solve(root): if root: solve(root.left) solve(root.right) if root.left: d[root][0] = d[root.left][1] + 1 if root.right: d[root][1] = d[root.right][0] + 1 self.ans = max(self.ans, max(d[root])) return self.ans d = defaultdict(lambda: [0, 0]) self.ans = 0 solve(root) return self.ans
CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def helper(self, node: TreeNode, lastDirection: str, cache) -> int: if not node: return 0 if (node, lastDirection) in cache: return cache[node, lastDirection] count = 1 childCount = float("-inf") if lastDirection == "right": childCount = max(childCount, self.helper(node.left, "left", cache)) else: childCount = max(childCount, self.helper(node.right, "right", cache)) count += childCount cache[node, lastDirection] = count return count def longestZigZag(self, root: TreeNode) -> int: maxCount = float("-inf") cache = {} stack = [root] while stack: node = stack.pop() maxCount = max(maxCount, self.helper(node, "left", cache)) maxCount = max(maxCount, self.helper(node, "right", cache)) if node.left: stack.append(node.left) if node.right: stack.append(node.right) return maxCount - 1
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: if root == None: return None maxlength = 0 stack = collections.deque() if root.left: stack.append((1, 1, root.left)) if root.right: stack.append((1, 0, root.right)) while stack: length, isleft, node = stack.pop() if isleft: if node.right: stack.append((length + 1, 0, node.right)) else: maxlength = max(maxlength, length) if node.left: stack.append((1, 1, node.left)) else: if node.left: stack.append((length + 1, 1, node.left)) else: maxlength = max(maxlength, length) if node.right: stack.append((1, 0, node.right)) return maxlength
CLASS_DEF FUNC_DEF VAR IF VAR NONE RETURN NONE ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR IF VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: def traverse(node, d, curr): print((d, curr)) if not node: res[0] = max(res[0], curr - 1) return if d == 0: traverse(node.left, 0, 1) traverse(node.right, 1, curr + 1) else: traverse(node.left, 0, curr + 1) traverse(node.right, 1, 1) if not root: return 0 res = [-float("inf")] traverse(root.left, 0, 1) traverse(root.right, 1, 1) return res[0]
CLASS_DEF FUNC_DEF VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR RETURN NUMBER ASSIGN VAR LIST FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: if root is None: return 0 self.helper(root) l = [root] m = 0 while len(l) != 0: node = l.pop() if node.left != None: l.append(node.left) if node.right != None: l.append(node.right) if max(node.val) > m: m = max(node.val) return m def helper(self, root): if root is None: return 0 self.helper(root.left) self.helper(root.right) if root.left == None and root.right == None: root.val = 0, 0 elif root.left != None and root.right == None: root.val = root.left.val[1] + 1, 0 elif root.right != None and root.left == None: root.val = 0, root.right.val[0] + 1 else: root.val = root.left.val[1] + 1, root.right.val[0] + 1
CLASS_DEF FUNC_DEF VAR IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NONE EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE VAR NONE ASSIGN VAR NUMBER NUMBER IF VAR NONE VAR NONE ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NONE VAR NONE ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def __init__(self): self.max_length = float("-inf") def longestZigZag(self, root): return self.dfs(root)[2] - 1 def dfs(self, root): if root is None: return [0, 0, 0] left_res = self.dfs(root.left) right_res = self.dfs(root.right) maxForSubtree = max(left_res[1], right_res[0]) + 1 return [ left_res[1] + 1, right_res[0] + 1, max(maxForSubtree, left_res[2], right_res[2]), ]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF IF VAR NONE RETURN LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER RETURN LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.max = 0 def dfs(node): if not node: return -1, -1 l_dir_left, l_dir_right = dfs(node.left) r_dir_left, r_dir_right = dfs(node.right) self.max = max( self.max, l_dir_left, l_dir_right + 1, r_dir_left + 1, r_dir_right ) return l_dir_right + 1, r_dir_left + 1 dfs(root) return self.max
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: nodes = self.bfs(root) self.dp = {node: [False, False] for node in nodes} ans = 0 for x in range(len(nodes)): ans = max(ans, self.recur(0, nodes[x], -1), self.recur(1, nodes[x], -1)) return ans def bfs(self, node): q = [node] arr = [] while q: r = q.pop() arr.append(r) if r.left: q.append(r.left) if r.right: q.append(r.right) return arr def recur(self, p, node, c): if not node: return c if self.dp[node][p] != False: return self.dp[node][p] if p == 0: self.dp[node][p] = self.recur(p ^ 1, node.left, c + 1) return self.dp[node][p] else: self.dp[node][p] = self.recur(p ^ 1, node.right, c + 1) return self.dp[node][p]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: seenSoFar = 0 def longestZigZagUtil(root): nonlocal seenSoFar if not root: return 0, 0 Ll, Lr = longestZigZagUtil(root.left) Rl, Rr = longestZigZagUtil(root.right) curL, curR = 0, 0 if root.left: curL = 1 + Lr seenSoFar = max(seenSoFar, Ll) if root.right: curR = 1 + Rl seenSoFar = max(seenSoFar, Rr) return curL, curR l, r = longestZigZagUtil(root) return max(l, r, seenSoFar)
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: l = [[root, 0]] r = [[root, 0]] m = 0 while True: if l == [] and r == []: return m l1 = [] r1 = [] for i in l: m = max(m, i[1]) if i[0].right != None: r1.append([i[0].right, i[1] + 1]) if i[0].left != None: r1.append([i[0].left, 0]) for i in r: m = max(m, i[1]) if i[0].left != None: l1.append([i[0].left, i[1] + 1]) if i[0].right != None: l1.append([i[0].right, 0]) r = r1 l = l1
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR LIST VAR LIST RETURN VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER NONE EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NONE EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER NONE EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NONE EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: memo = {} ans = [0] def cur(node: TreeNode): if node != None: memo[node] = [0, 0] if node.left != None: cur(node.left) memo[node][0] = memo[node.left][1] + 1 ans[0] = max(ans[0], memo[node][0]) if node.right != None: cur(node.right) memo[node][1] = memo[node.right][0] + 1 ans[0] = max(ans[0], memo[node][1]) cur(root) return ans[0]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR LIST NUMBER FUNC_DEF VAR IF VAR NONE ASSIGN VAR VAR LIST NUMBER NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: if root == None: return 0 maxZigZag = 0 def zigZagStart(root): nonlocal maxZigZag if root == None or root.left == None and root.right == None: return [0, 0] ll, lr = zigZagStart(root.left) rl, rr = zigZagStart(root.right) bestLeft = 0 bestRight = 0 if root.left: bestLeft = 1 + lr if root.right: bestRight = 1 + rl maxZigZag = max(maxZigZag, bestLeft, bestRight) return [bestLeft, bestRight] zigZagStart(root) return maxZigZag
CLASS_DEF FUNC_DEF VAR IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE VAR NONE VAR NONE RETURN LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN LIST VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: def helper(root): stack = [[root, True, 0], [root, False, 0]] ans = 0 while stack: root, right, length = stack.pop() if root: ans = max(length, ans) stack.append( (root.right if right else root.left, not right, length + 1) ) stack.append((root.left if right else root.right, right, 1)) return ans return helper(root)
CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR LIST LIST VAR NUMBER NUMBER LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZagRec(self, root, goLeft, steps): if root == None: return steps - 1 if goLeft: return max( self.longestZigZagRec(root.left, False, steps + 1), self.longestZigZagRec(root.right, True, 1), ) else: return max( self.longestZigZagRec(root.right, True, steps + 1), self.longestZigZagRec(root.left, False, 1), ) def longestZigZag(self, root: TreeNode) -> int: if root == None: return 0 return max( self.longestZigZagRec(root, True, 0), self.longestZigZagRec(root, False, 0) )
CLASS_DEF FUNC_DEF IF VAR NONE RETURN BIN_OP VAR NUMBER IF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF VAR IF VAR NONE RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: def dfs(root): if root is None: return 0, [0, 0] if root.left is None and root.right is None: return 0, [0, 0] left_zigzag, [_, right] = dfs(root.left) right_zigzag, [left, _] = dfs(root.right) zigzag = max(left_zigzag, right_zigzag) if root.right: right_zigzag = 1 + left zigzag = max(zigzag, right_zigzag) else: right_zigzag = 0 if root.left: left_zigzag = 1 + right zigzag = max(zigzag, left_zigzag) else: left_zigzag = 0 return zigzag, [left_zigzag, right_zigzag] return dfs(root)[0]
CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR NONE RETURN NUMBER LIST NUMBER NUMBER IF VAR NONE VAR NONE RETURN NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR LIST VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: def zigzag(node, mz): if not node: return -1, -1 l1, r1 = zigzag(node.left, mz) l2, r2 = zigzag(node.right, mz) mz[0] = max(mz[0], r1 + 1, l2 + 1) return r1 + 1, l2 + 1 maxzigzag = [0] zigzag(root, maxzigzag) zigzag(root, maxzigzag) return maxzigzag[0]
CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.ans = 0 def helper(node, direction, l): if not node: return self.ans = max(self.ans, l) helper((node.left, node.right)[direction], 1 - direction, l + 1) helper((node.left, node.right)[1 - direction], direction, 1) helper(root, 0, 0) helper(root, 1, 0) return self.ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: check = 0 count = 0 self.res = 0 def dfs(node, count, check): if node: if check == 1: dfs(node.left, 0, 1) dfs(node.right, count + 1, 2) elif check == 2: dfs(node.left, count + 1, 1) dfs(node.right, 0, 2) elif check == 0: dfs(node.left, count, 1) dfs(node.right, count, 2) self.res = max(self.res, count) dfs(root, count, check) return self.res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
_max = 0 class Solution: def preorder(self, root, lr, cur_len): nonlocal _max if root == None: return _max = max(_max, cur_len) if lr == "l": self.preorder(root.left, "l", 1) self.preorder(root.right, "r", cur_len + 1) elif lr == "r": self.preorder(root.left, "l", cur_len + 1) self.preorder(root.right, "r", 1) else: self.preorder(root.left, "l", cur_len + 1) self.preorder(root.right, "r", cur_len + 1) def longestZigZag(self, root: TreeNode) -> int: nonlocal _max _max = 0 self.preorder(root, None, 0) return _max
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR STRING NUMBER EXPR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING NUMBER EXPR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER FUNC_DEF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NONE NUMBER RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def helper(self, node, prenode, ans): if not node: return self.res = max(self.res, ans) if node == prenode.left: self.helper(node.right, node, ans + 1) self.helper(node.left, node, 1) elif node == prenode.right: self.helper(node.left, node, ans + 1) self.helper(node.right, node, 1) def longestZigZag(self, root: TreeNode) -> int: self.res = 0 self.helper(root.left, root, 1) self.helper(root.right, root, 1) return self.res
CLASS_DEF FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: stack = [(root, 0, "left")] res = 0 while stack: node, length, d = stack.pop() res = max(res, length) if node.left: if d != "left": stack.append((node.left, length + 1, "left")) else: stack.append((node.left, 1, "left")) if node.right: if d != "right": stack.append((node.right, length + 1, "right")) else: stack.append((node.right, 1, "right")) return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR IF VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER STRING IF VAR IF VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER STRING RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: def check(root): l = 0 r = 0 m = 0 if root.left != None: r1 = check(root.left) l = r1[1] + 1 m = max(m, r1[2]) if root.right != None: r2 = check(root.right) r = r2[0] + 1 m = max(m, r2[2]) return l, r, max(l, r, m) if root == None: return 0 r = check(root) return r[2]
CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: longest = [0] def dfs(node, d, c): if not node: longest[0] = max(longest[0], c) return if d == "r": dfs(node.left, "l", c + 1) else: dfs(node.left, "l", 0) if d == "l": dfs(node.right, "r", c + 1) else: dfs(node.right, "r", 0) dfs(root, "", 0) return longest[0]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN IF VAR STRING EXPR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING NUMBER EXPR FUNC_CALL VAR VAR STRING NUMBER RETURN VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.max_len = 0 def subTree(root, indir, flip): if root is None: return 0 if indir == -1: subcount = subTree(root.right, 1, True) self.max_len = max(subcount + 1, self.max_len) else: subcount = subTree(root.left, -1, True) self.max_len = max(subcount + 1, self.max_len) if flip: subTree(root, -indir, False) return subcount + 1 subTree(root, 1, True) return self.max_len - 1
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: memo = dict() def zig_zag_len(node, direction): if not node: return -1 if (node, direction) in memo: return memo[node, direction] if direction == "L": memo[node, direction] = 1 + zig_zag_len(node.right, "R") elif direction == "R": memo[node, direction] = 1 + zig_zag_len(node.left, "L") else: memo[node, direction] = max( 1 + zig_zag_len(node.right, "R"), 1 + zig_zag_len(node.left, "L"), zig_zag_len(node.right, "N"), zig_zag_len(node.left, "N"), ) return memo[node, direction] return zig_zag_len(root, "N")
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR STRING IF VAR STRING ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR STRING BIN_OP NUMBER FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR STRING VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.ret = 0 self.dfs(root, 0, 0) self.dfs(root, 1, 0) return self.ret - 1 def dfs(self, root, prevright, length): if root is None: self.ret = max(self.ret, length) return if prevright: self.dfs(root.left, 1 - prevright, length + 1) self.dfs(root.right, prevright, 1) else: self.dfs(root.right, 1 - prevright, length + 1) self.dfs(root.left, prevright, 1) return
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN IF VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: def dfs(node): if not node: return [-1, -1, -1] left = dfs(node.left) right = dfs(node.right) return [ left[1] + 1, right[0] + 1, max(left[1] + 1, right[0] + 1, left[2], right[2]), ] return dfs(root)[-1]
CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR RETURN LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
def solve(root, res, ind): if root == None: return 0 l = solve(root.left, res, 0) r = solve(root.right, res, 1) if ind == 0: temp = 1 + r elif ind == 1: temp = 1 + l ans = 1 + max(l, r) res[0] = max(res[0], ans) return temp class Solution: def longestZigZag(self, root: TreeNode) -> int: if root == None: return 0 if root.left == None and root.right == None: return 0 res1 = [0] res2 = [0] c1 = solve(root, res1, 0) c2 = solve(root, res2, 1) return max(res1[0], res2[0]) - 1
FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN VAR CLASS_DEF FUNC_DEF VAR IF VAR NONE RETURN NUMBER IF VAR NONE VAR NONE RETURN NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: if not root: return 0 if not root.left and not root.right: return 0 def dfs(root, flag, count): if not root: self.res = max(self.res, count - 1) return if flag == 1: dfs(root.left, -1, 1 + count) dfs(root.right, 1, 1) else: dfs(root.right, 1, 1 + count) dfs(root.left, -1, 1) self.res = 0 dfs(root, -1, 0) return self.res
CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER IF VAR VAR RETURN NUMBER FUNC_DEF IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def __init__(self): self.memo = {} def h(self, node: TreeNode, left: bool) -> int: if not node: return 0 if (node, left) not in self.memo: ret = 0 if left and node.left is not None: ret = 1 + self.h(node.left, False) elif not left and node.right is not None: ret = 1 + self.h(node.right, True) self.memo[node, left] = ret return self.memo[node, left] def longestZigZag(self, root: TreeNode) -> int: if not root: return 0 ret = [0] if root.left is not None: ret.extend([1 + self.h(root.left, False), self.longestZigZag(root.left)]) if root.right is not None: ret.extend([1 + self.h(root.right, True), self.longestZigZag(root.right)]) return max(ret)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF VAR VAR IF VAR RETURN NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NONE ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR NONE ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF VAR IF VAR RETURN NUMBER ASSIGN VAR LIST NUMBER IF VAR NONE EXPR FUNC_CALL VAR LIST BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR LIST BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: def zigzag(node: TreeNode) -> tuple: if not node: return 0, 0 _, lr = zigzag(node.left) rl, _ = zigzag(node.right) self.max_path = max(self.max_path, lr + 1, rl + 1) return lr + 1, rl + 1 self.max_path = 0 zigzag(root) return self.max_path - 1
CLASS_DEF FUNC_DEF VAR FUNC_DEF VAR IF VAR RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: if not root: return 0 q = deque() max_depth = 0 if root.left: q.append((root.left, True, 1)) if root.right: q.append((root.right, False, 1)) while q: n, is_left, depth = q.popleft() max_depth = max(depth, max_depth) if n.left: if is_left: q.append((n.left, True, 1)) else: q.append((n.left, True, depth + 1)) if n.right: if is_left: q.append((n.right, False, depth + 1)) else: q.append((n.right, False, 1)) return max_depth
CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: res = collections.namedtuple("res", "left right total") def dfs(root): if not root: return res(-1, -1, -1) left = dfs(root.left) right = dfs(root.right) return res( left.right + 1, right.left + 1, max(left.right + 1, right.left + 1, left.total, right.total), ) return dfs(root).total
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR STRING STRING FUNC_DEF IF VAR RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: Max = [0] vl = set() vr = set() def path(node, l, d): if node == None: return l if d == 1: vr.add(node) return path(node.left, l + 1, 0) else: vl.add(node) return path(node.right, l + 1, 1) def dfs(node): if node == None: return if node not in vl: Max[0] = max(Max[0], path(node, -1, 0)) if node not in vr: Max[0] = max(Max[0], path(node, -1, 1)) dfs(node.left) dfs(node.right) dfs(root) return Max[0]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.sol = 0 left = self.DFS(root, 0) right = self.DFS(root, 1) print((left, right, self.sol)) return self.sol - 1 def DFS(self, node, direction): if not node: return 0 left = self.DFS(node.left, 0) right = self.DFS(node.right, 1) self.sol = max(left + 1, right + 1, self.sol) print(self.sol) if direction == 0: return right + 1 else: return left + 1
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def run(self, node, side, count, memo): if not node: return count if node in memo: if memo[node][side] > -1: return memo[node][side] memo[node] = [-1, -1, -1] if side == 0: result = self.run(node.right, 1, count + 1, memo) elif side == 1: result = self.run(node.left, 0, count + 1, memo) else: using = max( self.run(node.right, 1, 1, memo), self.run(node.left, 0, 1, memo) ) notUsing = max( self.run(node.right, -1, 0, memo), self.run(node.left, -1, 0, memo) ) result = max(using, notUsing) memo[node][side] = result return result def longestZigZag(self, root: TreeNode) -> int: if not root: return 0 return self.run(root, -1, 0, {}) - 1
CLASS_DEF FUNC_DEF IF VAR RETURN VAR IF VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR IF VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER DICT NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.zigzag(root) return self.find_longest(root) def zigzag(self, node: TreeNode) -> int: if node is None: return self.zigzag(node.left) self.zigzag(node.right) if node.left is not None: node.left_depth = node.left.right_depth + 1 else: node.left_depth = 0 if node.right is not None: node.right_depth = node.right.left_depth + 1 else: node.right_depth = 0 def find_longest(self, node: TreeNode) -> int: if node is None: return 0 left_max = self.find_longest(node.left) right_max = self.find_longest(node.right) return max(left_max, right_max, node.left_depth, node.right_depth)
CLASS_DEF FUNC_DEF VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_DEF VAR IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def __init__(self): self.lmemo = {} self.rmemo = {} def longestZigZag(self, root: TreeNode) -> int: def lzz(root, mx): if root is not None: lzz(root.left, mx) lzz(root.right, mx) mx[0] = max(mx[0], llzz(root), rlzz(root)) return mx def llzz(root): if root not in self.lmemo: self.lmemo[root] = 1 + rlzz(root.left) return self.lmemo[root] def rlzz(root): if root not in self.rmemo: self.rmemo[root] = 1 + llzz(root.right) return self.rmemo[root] self.lmemo[None] = self.rmemo[None] = 0 return lzz(root, [float("-inf")])[0] - 1
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF VAR FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR NONE VAR NONE NUMBER RETURN BIN_OP FUNC_CALL VAR VAR LIST FUNC_CALL VAR STRING NUMBER NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.res = 0 self.helper(root, True) self.helper(root, False) return self.res - 1 def helper(self, root, isLeft): if not root: return 0 left = self.helper(root.left, True) right = self.helper(root.right, False) self.res = max(self.res, left + 1, right + 1) return right + 1 if isLeft else left + 1
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: m = 0 def longestZigZag(self, root: TreeNode) -> int: def longest(node: TreeNode, state: int, acc: int) -> int: self.m = max(self.m, acc) if not node: return 0 if state == 0: longest(node.right, 1, acc + 1), longest(node.left, 0, 0) else: longest(node.left, 0, acc + 1), longest(node.right, 1, 0) longest(root.left, 0, 0), longest(root.right, 1, 0) return self.m
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: q = collections.deque() res = 0 q.append((root, 0, 0)) while q: size = len(q) for _ in range(size): node, l, r = q.popleft() if node.left: q.append((node.left, r + 1, 0)) res = max(res, r + 1) if node.right: q.append((node.right, 0, l + 1)) res = max(res, l + 1) return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.max_ = 0 def preorder(root, count, dir_): self.max_ = max(self.max_, count) if root.left: if dir_ == 1 or dir_ == -1: preorder(root.left, count + 1, 0) else: preorder(root.left, 1, 0) if root.right: if dir_ == 0 or dir_ == -1: preorder(root.right, count + 1, 1) else: preorder(root.right, 1, 1) preorder(root, 0, -1) return self.max_
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: if root == None: return 0 max_zigzag = 0 zigzag = 0 stack = [(root, None, 0)] while stack: node = stack[-1][0] prev_dir = stack[-1][1] max_up_to_node = stack[-1][2] del stack[-1] if max_up_to_node > max_zigzag: max_zigzag = max_up_to_node if prev_dir == None: if node.right != None: stack.append((node.right, "R", max_up_to_node + 1)) if node.left != None: stack.append((node.left, "L", max_up_to_node + 1)) else: if prev_dir == "R": if node.right != None: stack.append((node.right, "R", 1)) if node.left != None: stack.append((node.left, "L", max_up_to_node + 1)) if prev_dir == "L": if node.right != None: stack.append((node.right, "R", max_up_to_node + 1)) if node.left != None: stack.append((node.left, "L", 1)) return max_zigzag
CLASS_DEF FUNC_DEF VAR IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NONE NUMBER WHILE VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NONE IF VAR NONE EXPR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER IF VAR STRING IF VAR NONE EXPR FUNC_CALL VAR VAR STRING NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER IF VAR STRING IF VAR NONE EXPR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR VAR STRING NUMBER RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.res = 0 if root.left: self.dfs(root.left, 1, True, False) if root.right: self.dfs(root.right, 1, False, True) return self.res def dfs(self, node, count, prevL, prevR): if not node: return self.res = max(self.res, count) if prevL: self.dfs(node.left, 1, True, False) self.dfs(node.right, count + 1, False, True) if prevR: self.dfs(node.left, count + 1, True, False) self.dfs(node.right, 1, False, True)
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN VAR VAR FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def __init__(self): self.max_cnt = 0 def longestZigZag(self, root: TreeNode) -> int: self.dfs(root, True, 0) self.dfs(root, False, 0) return self.max_cnt def dfs(self, root, isLeft, cnt): if root is None: return self.max_cnt = max(self.max_cnt, cnt) if isLeft: self.dfs(root.left, False, cnt + 1) self.dfs(root.right, True, 1) else: self.dfs(root.right, True, cnt + 1) self.dfs(root.left, False, 1)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF IF VAR NONE RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: if root is None: return 0 self.sol = 0 def recursive_long_zigzag(node): if node.left is None and node.right is None: return 0, 0 max_left = 0 max_right = 0 if node.left: _, left_s_right = recursive_long_zigzag(node.left) max_left = left_s_right + 1 self.sol = max(self.sol, max_left) if node.right: right_s_left, _ = recursive_long_zigzag(node.right) max_right = right_s_left + 1 self.sol = max(self.sol, max_right) return max_left, max_right recursive_long_zigzag(root) return self.sol
CLASS_DEF FUNC_DEF VAR IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE VAR NONE RETURN NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.longest = 0 self.dfs(root, 0, 0) return self.longest def dfs(self, node, longest_left, longest_right): self.longest = max(self.longest, longest_left, longest_right) if node.left: self.dfs(node.left, longest_right + 1, 0) if node.right: self.dfs(node.right, 0, longest_left + 1)
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: dp = [0] def f(n, ind): if not n: dp[ind] = 0, 0 else: dp.extend([0, 0]) temp = len(dp) f(n.left, temp - 2) f(n.right, temp - 1) dp[ind] = dp[temp - 1][1] + 1, dp[temp - 2][0] + 1 f(root, 0) m = -1 for i in dp: m = max(i[0], i[1], m) return m - 1
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR RETURN BIN_OP VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: ans = [-1] def aux(root, isleft, ans): if not root: return -1 left = aux(root.left, 1, ans) + 1 right = aux(root.right, 0, ans) + 1 ans[0] = max(ans[0], left, right) if isleft: return right else: return left if not root: return 0 aux(root, 0, ans) return ans[0]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR RETURN VAR RETURN VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: res = 0 def helper(root, direction): nonlocal res if not root: return 0 left = helper(root.left, "left") right = helper(root.right, "right") res = max(res, left + 1, right + 1) return right + 1 if direction == "left" else left + 1 if not root: return 0 helper(root, "left") helper(root, "right") return res - 1
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR STRING BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING RETURN BIN_OP VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: if not root: return 0 return max(self.length(root.left, "l", 1), self.length(root.right, "r", 1)) def length(self, node, d, depth): if not node: return depth - 1 print((d, depth, node.val)) if d == "l": return max( self.length(node.right, "r", depth + 1), self.length(node.left, "l", 1) ) else: return max( self.length(node.left, "l", depth + 1), self.length(node.right, "r", 1) )
CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR STRING NUMBER FUNC_CALL VAR VAR STRING NUMBER VAR FUNC_DEF IF VAR RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR STRING RETURN FUNC_CALL VAR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR STRING NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR STRING NUMBER
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: def Search(root, left, height): if root is None: return height if left: return max(Search(root.left, 1, 0), Search(root.right, 0, height + 1)) else: return max(Search(root.right, 0, 0), Search(root.left, 1, height + 1)) return max(Search(root.left, 1, 0), Search(root.right, 0, 0))
CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def node_is_on_the_right(self, node): if not node: return 0 continuation = 1 + self.node_is_on_the_left(node.left) starts_here = self.node_is_on_the_right(node.right) self.m = max(self.m, starts_here) return continuation def node_is_on_the_left(self, node): if not node: return 0 continuation = 1 + self.node_is_on_the_right(node.right) starts_here = self.node_is_on_the_left(node.left) self.m = max(self.m, starts_here) return continuation def longestZigZag(self, root: TreeNode) -> int: self.m = 0 x = self.node_is_on_the_right(root) - 1 self.m = max(self.m, x) return self.m
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.res = float(-inf) def helper(root, left, right): self.res = max(self.res, max(left, right)) if root == None: return if root.left: helper(root.left, right + 1, 0) if root.right: helper(root.right, 0, left + 1) return helper(root, 0, 0) return self.res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: _, max_depth = self.zigzag(root) return max_depth def zigzag(self, node: TreeNode, return_left=False) -> int: if node is None: return -1, 0 left_depth, left_max = self.zigzag(node.left) right_depth, right_max = self.zigzag(node.right, return_left=True) left_depth += 1 right_depth += 1 max_depth = max(left_depth, right_depth, left_max, right_max) return left_depth if return_left else right_depth, max_depth
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF VAR NUMBER IF VAR NONE RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.mx = 0 out = self.traverse(root, 0) out = self.traverse(root, 1) return self.mx - 1 def traverse(self, node, ctype): if node is None: return 0, 0 ll = lr = rl = rr = 0 if node.left: ll, lr = self.traverse(node.left, 0) if node.right: rl, rr = self.traverse(node.right, 1) best = max(lr, rl) + 1 self.mx = max(self.mx, best) return 1 + lr, 1 + rl
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NONE RETURN NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP NUMBER VAR BIN_OP NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: def helper(root): if not root: return -1, -1, -1 if not root.left and not root.right: return 0, 0, 0 else: l1, l2, l3 = helper(root.left) r1, r2, r3 = helper(root.right) return l3 + 1, max(l3 + 1, r1 + 1, l2, r2), r1 + 1 return helper(root)[1]
CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR RETURN NUMBER NUMBER NUMBER IF VAR VAR RETURN NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.ans = 0 memo = {} def dfs2(node, direction): if not node: return 0 if (node, direction) not in memo: if direction == False: memo[node, direction] = 1 + dfs2(node.right, True) else: memo[node, direction] = 1 + dfs2(node.left, False) return memo[node, direction] def dfs1(node): if not node: return self.ans = max(self.ans, dfs2(node, True) - 1, dfs2(node, False) - 1) dfs1(node.left) dfs1(node.right) dfs1(root) return self.ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). If the current direction is right then move to the right child of the current node otherwise move to the left child. Change the direction from right to left or right to left. Repeat the second and third step until you can't move in the tree. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). Return the longest ZigZag path contained in that tree.   Example 1: Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] Output: 3 Explanation: Longest ZigZag path in blue nodes (right -> left -> right). Example 2: Input: root = [1,1,1,null,1,null,null,1,1,null,1] Output: 4 Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). Example 3: Input: root = [1] Output: 0   Constraints: Each tree has at most 50000 nodes.. Each node's value is between [1, 100].
class Solution: def longestZigZag(self, root: TreeNode) -> int: self.maxpath = 0 l, r = self.visit(root) return self.maxpath def visit(self, root): l = 0 r = 0 if root.left: ll, lr = self.visit(root.left) l = lr + 1 if root.right: rl, rr = self.visit(root.right) r = rl + 1 if max(l, r) > self.maxpath: self.maxpath = max(l, r) return l, r
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
You are given $n$ lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start. The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$, the segment can be placed either on the coordinates $[x-d, x]$, and then the coordinate of its "end" is $x - d$, or on the coordinates $[x, x+d]$, in which case its "end" coordinate is $x + d$. The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 10^4$) — the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 1000$) — lengths of the segments in the same order they should be placed on the axis. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible length of the axis coverage. -----Examples----- Input 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 Output 3 3 9 9 7 8 -----Note----- In the third sample test case the segments should be arranged as follows: $[0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]$. As you can see, the last segment $[-2, 7]$ covers all the previous ones, and the total length of coverage is $9$. In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$. The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n = int(input()) A = list(map(int, input().split())) NG = max(A) - 1 OK = max(A) * 2 while OK > NG + 1: mid = (OK + NG) // 2 MAX = (1 << mid + 1) - 1 DP = MAX for i in range(n): DP = (DP << A[i] | DP >> A[i]) & MAX if DP != 0: OK = mid else: NG = mid print(OK)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given $n$ lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start. The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$, the segment can be placed either on the coordinates $[x-d, x]$, and then the coordinate of its "end" is $x - d$, or on the coordinates $[x, x+d]$, in which case its "end" coordinate is $x + d$. The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 10^4$) — the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 1000$) — lengths of the segments in the same order they should be placed on the axis. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible length of the axis coverage. -----Examples----- Input 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 Output 3 3 9 9 7 8 -----Note----- In the third sample test case the segments should be arranged as follows: $[0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]$. As you can see, the last segment $[-2, 7]$ covers all the previous ones, and the total length of coverage is $9$. In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$. The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$.
t = int(input()) for _ in range(t): best = [0] * 3000 n = int(input()) l = map(int, input().split()) for d in l: nex = [] for i in range(3000): p1 = best[i + d] + d if i < 3000 - d else 10000 p2 = max(0, best[i - d] - d) if i >= d else 10000 nex.append(min(p1, p2)) best = nex for i in range(3000): best[i] += i print(min(best))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given $n$ lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start. The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$, the segment can be placed either on the coordinates $[x-d, x]$, and then the coordinate of its "end" is $x - d$, or on the coordinates $[x, x+d]$, in which case its "end" coordinate is $x + d$. The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 10^4$) — the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 1000$) — lengths of the segments in the same order they should be placed on the axis. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible length of the axis coverage. -----Examples----- Input 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 Output 3 3 9 9 7 8 -----Note----- In the third sample test case the segments should be arranged as follows: $[0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]$. As you can see, the last segment $[-2, 7]$ covers all the previous ones, and the total length of coverage is $9$. In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$. The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$.
from sys import stdin def solve(N, nums): MAX = max(nums) * 2 + 1 cur = [MAX] * MAX cur[0] = 0 for d in nums: nxt = [MAX] * MAX for l, r in enumerate(cur): if r >= MAX: continue for ll, rr in [(l, r), (r, l)]: nl = ll + d if nl >= MAX: continue nr = max(ll + rr, nl) - nl nxt[nl] = min(nxt[nl], nr) cur = nxt return min(l + r for l, r in enumerate(cur)) def main(): from sys import stdin T = int(stdin.readline().strip()) for _ in range(T): N = int(stdin.readline().strip()) nums = list(map(int, stdin.readline().strip().split())) out = solve(N, nums) print(out) main()
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR LIST VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given $n$ lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start. The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$, the segment can be placed either on the coordinates $[x-d, x]$, and then the coordinate of its "end" is $x - d$, or on the coordinates $[x, x+d]$, in which case its "end" coordinate is $x + d$. The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 10^4$) — the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 1000$) — lengths of the segments in the same order they should be placed on the axis. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible length of the axis coverage. -----Examples----- Input 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 Output 3 3 9 9 7 8 -----Note----- In the third sample test case the segments should be arranged as follows: $[0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]$. As you can see, the last segment $[-2, 7]$ covers all the previous ones, and the total length of coverage is $9$. In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$. The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$.
from sys import stdout output = [] for _t in range(int(input())): n = int(input()) a = list(map(int, input().split())) limit = max(a) * 2 + 1 dp = [([limit] * limit) for _ in range(n + 1)] dp[0][0] = 0 for i, a in enumerate(a): for left in range(limit): if dp[i][left] < limit: if left < a: dp[i + 1][0] = min(dp[i + 1][0], dp[i][left] + a - left) else: dp[i + 1][left - a] = min(dp[i + 1][left - a], dp[i][left]) if left + a < limit: dp[i + 1][left + a] = min( dp[i + 1][left + a], max(dp[i][left], left + a) ) output.append(min(dp[n])) stdout.write("\n".join(map(str, output)))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given $n$ lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate $0$. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start. The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is $d$ and the "end" of the previous one has the coordinate $x$, the segment can be placed either on the coordinates $[x-d, x]$, and then the coordinate of its "end" is $x - d$, or on the coordinates $[x, x+d]$, in which case its "end" coordinate is $x + d$. The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The next $2t$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $n$ ($1 \le n \le 10^4$) — the number of segments. The second line of the description contains $n$ space-separated integers $a_i$ ($1 \le a_i \le 1000$) — lengths of the segments in the same order they should be placed on the axis. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$. -----Output----- Print $t$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the minimal possible length of the axis coverage. -----Examples----- Input 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 Output 3 3 9 9 7 8 -----Note----- In the third sample test case the segments should be arranged as follows: $[0, 6] \rightarrow [4, 6] \rightarrow [4, 7] \rightarrow [-2, 7]$. As you can see, the last segment $[-2, 7]$ covers all the previous ones, and the total length of coverage is $9$. In the fourth sample test case the segments should be arranged as $[0, 6] \rightarrow [-2, 6] \rightarrow [-2, 2] \rightarrow [2, 7]$. The union of these segments also occupies the area $[-2, 7]$ and has the length of $9$.
for t in range(int(input())): n = int(input()) arr = tuple(map(int, input().rstrip().split())) max_seg = max(arr) a0 = [3000] * max_seg a0[0] = 0 for v in arr: a1 = [3000] * max_seg for x0, y0 in enumerate(a0): if y0 == 3000: continue x1, y1 = max(x0 - v, 0), y0 + v if x1 < max_seg: a1[x1] = min(a1[x1], y1) x1, y1 = tuple(sorted((max(y0 - v, 0), x0 + v))) if x1 < max_seg: a1[x1] = min(a1[x1], y1) a0 = a1 print(min(x + y for x, y in enumerate(a0)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR