description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). q events are about to happen (in chronological order). They are of three types: Application x generates a notification (this new notification is unread). Thor reads all notifications generated so far by application x (he may re-read some notifications). Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. -----Input----- The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer type_{i} — type of the i-th event. If type_{i} = 1 or type_{i} = 2 then it is followed by an integer x_{i}. Otherwise it is followed by an integer t_{i} (1 ≤ type_{i} ≤ 3, 1 ≤ x_{i} ≤ n, 1 ≤ t_{i} ≤ q). -----Output----- Print the number of unread notifications after each event. -----Examples----- Input 3 4 1 3 1 1 1 2 2 3 Output 1 2 3 2 Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Output 1 2 3 0 1 2 -----Note----- In the first sample: Application 3 generates a notification (there is 1 unread notification). Application 1 generates a notification (there are 2 unread notifications). Application 2 generates a notification (there are 3 unread notifications). Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: Application 2 generates a notification (there is 1 unread notification). Application 4 generates a notification (there are 2 unread notifications). Application 2 generates a notification (there are 3 unread notifications). Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. Application 3 generates a notification (there is 1 unread notification). Application 3 generates a notification (there are 2 unread notifications).
import sys n, m = list(map(int, input().split())) k = 0 pos = 0 L = [] L1 = [] d = {i: [0, -1] for i in range(1, n + 1)} for i in range(m): a, b = list(map(int, sys.stdin.readline()[:-1].split())) if a == 1: d[b][0] += 1 k += 1 L.append(b) elif a == 2: k -= d[b][0] d[b][0] = 0 d[b][1] = len(L) else: for j in range(pos, b): if d[L[j]][0] > 0 and d[L[j]][1] < j + 1: k -= 1 d[L[j]][0] -= 1 pos = max(pos, b) L1.append(k) sys.stdout.write("\n".join(map(str, L1)))
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher: You are given a sequence of integers a_1, a_2, ..., a_{n}. Your task is to perform on it m consecutive operations of the following type: For given numbers x_{i} and v_{i} assign value v_{i} to element a_{x}_{i}. For given numbers l_{i} and r_{i} you've got to calculate sum $\sum_{x = 0}^{r_{i} - l_{i}}(f_{x} \cdot a_{l_{i} + x})$, where f_0 = f_1 = 1 and at i ≥ 2: f_{i} = f_{i} - 1 + f_{i} - 2. For a group of three numbers l_{i} r_{i} d_{i} you should increase value a_{x} by d_{i} for all x (l_{i} ≤ x ≤ r_{i}). Smart Beaver planned a tour around great Canadian lakes, so he asked you to help him solve the given problem. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 2·10^5) — the number of integers in the sequence and the number of operations, correspondingly. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^5). Then follow m lines, each describes an operation. Each line starts with an integer t_{i} (1 ≤ t_{i} ≤ 3) — the operation type: if t_{i} = 1, then next follow two integers x_{i} v_{i} (1 ≤ x_{i} ≤ n, 0 ≤ v_{i} ≤ 10^5); if t_{i} = 2, then next follow two integers l_{i} r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n); if t_{i} = 3, then next follow three integers l_{i} r_{i} d_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n, 0 ≤ d_{i} ≤ 10^5). The input limits for scoring 30 points are (subproblem E1): It is guaranteed that n does not exceed 100, m does not exceed 10000 and there will be no queries of the 3-rd type. The input limits for scoring 70 points are (subproblems E1+E2): It is guaranteed that there will be queries of the 1-st and 2-nd type only. The input limits for scoring 100 points are (subproblems E1+E2+E3): No extra limitations. -----Output----- For each query print the calculated sum modulo 1000000000 (10^9). -----Examples----- Input 5 5 1 3 1 2 4 2 1 4 2 1 5 2 2 4 1 3 10 2 1 5 Output 12 32 8 50 Input 5 4 1 3 1 2 4 3 1 4 1 2 2 4 1 2 10 2 1 5 Output 12 45
mod = 10**9 FibArray = [1, 1] def fibonacci(n): if n <= len(FibArray): return FibArray[n - 1] else: temp_fib = fibonacci(n - 1) + fibonacci(n - 2) FibArray.append(temp_fib) return temp_fib n, m = list(map(int, input().split())) a = list(map(int, input().split())) for _ in range(m): query = list(map(int, input().split())) if query[0] == 1: a[query[1] - 1] = query[2] elif query[0] == 3: d = query[3] for i in range(query[1] - 1, query[2]): a[i] += d else: l, r = query[1], query[2] s = 0 for x in range(r - l + 1): s += fibonacci(x + 1) * a[l + x - 1] print(s % mod)
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher: You are given a sequence of integers a_1, a_2, ..., a_{n}. Your task is to perform on it m consecutive operations of the following type: For given numbers x_{i} and v_{i} assign value v_{i} to element a_{x}_{i}. For given numbers l_{i} and r_{i} you've got to calculate sum $\sum_{x = 0}^{r_{i} - l_{i}}(f_{x} \cdot a_{l_{i} + x})$, where f_0 = f_1 = 1 and at i ≥ 2: f_{i} = f_{i} - 1 + f_{i} - 2. For a group of three numbers l_{i} r_{i} d_{i} you should increase value a_{x} by d_{i} for all x (l_{i} ≤ x ≤ r_{i}). Smart Beaver planned a tour around great Canadian lakes, so he asked you to help him solve the given problem. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 2·10^5) — the number of integers in the sequence and the number of operations, correspondingly. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^5). Then follow m lines, each describes an operation. Each line starts with an integer t_{i} (1 ≤ t_{i} ≤ 3) — the operation type: if t_{i} = 1, then next follow two integers x_{i} v_{i} (1 ≤ x_{i} ≤ n, 0 ≤ v_{i} ≤ 10^5); if t_{i} = 2, then next follow two integers l_{i} r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n); if t_{i} = 3, then next follow three integers l_{i} r_{i} d_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n, 0 ≤ d_{i} ≤ 10^5). The input limits for scoring 30 points are (subproblem E1): It is guaranteed that n does not exceed 100, m does not exceed 10000 and there will be no queries of the 3-rd type. The input limits for scoring 70 points are (subproblems E1+E2): It is guaranteed that there will be queries of the 1-st and 2-nd type only. The input limits for scoring 100 points are (subproblems E1+E2+E3): No extra limitations. -----Output----- For each query print the calculated sum modulo 1000000000 (10^9). -----Examples----- Input 5 5 1 3 1 2 4 2 1 4 2 1 5 2 2 4 1 3 10 2 1 5 Output 12 32 8 50 Input 5 4 1 3 1 2 4 3 1 4 1 2 2 4 1 2 10 2 1 5 Output 12 45
n, m = list(map(int, input().split())) a = list(map(int, input().split())) for i in range(m): t, l, r = list(map(int, input().split())) if t == 1: a[l - 1] = r else: s = 0 fiba = fibb = 1 for i in range(l - 1, r): s += fiba * a[i] fiba, fibb = fibb, fiba + fibb print(s % 1000000000)
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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher: You are given a sequence of integers a_1, a_2, ..., a_{n}. Your task is to perform on it m consecutive operations of the following type: For given numbers x_{i} and v_{i} assign value v_{i} to element a_{x}_{i}. For given numbers l_{i} and r_{i} you've got to calculate sum $\sum_{x = 0}^{r_{i} - l_{i}}(f_{x} \cdot a_{l_{i} + x})$, where f_0 = f_1 = 1 and at i ≥ 2: f_{i} = f_{i} - 1 + f_{i} - 2. For a group of three numbers l_{i} r_{i} d_{i} you should increase value a_{x} by d_{i} for all x (l_{i} ≤ x ≤ r_{i}). Smart Beaver planned a tour around great Canadian lakes, so he asked you to help him solve the given problem. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 2·10^5) — the number of integers in the sequence and the number of operations, correspondingly. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^5). Then follow m lines, each describes an operation. Each line starts with an integer t_{i} (1 ≤ t_{i} ≤ 3) — the operation type: if t_{i} = 1, then next follow two integers x_{i} v_{i} (1 ≤ x_{i} ≤ n, 0 ≤ v_{i} ≤ 10^5); if t_{i} = 2, then next follow two integers l_{i} r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n); if t_{i} = 3, then next follow three integers l_{i} r_{i} d_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n, 0 ≤ d_{i} ≤ 10^5). The input limits for scoring 30 points are (subproblem E1): It is guaranteed that n does not exceed 100, m does not exceed 10000 and there will be no queries of the 3-rd type. The input limits for scoring 70 points are (subproblems E1+E2): It is guaranteed that there will be queries of the 1-st and 2-nd type only. The input limits for scoring 100 points are (subproblems E1+E2+E3): No extra limitations. -----Output----- For each query print the calculated sum modulo 1000000000 (10^9). -----Examples----- Input 5 5 1 3 1 2 4 2 1 4 2 1 5 2 2 4 1 3 10 2 1 5 Output 12 32 8 50 Input 5 4 1 3 1 2 4 3 1 4 1 2 2 4 1 2 10 2 1 5 Output 12 45
import sys f = [-1] * 200000 def fib(n): if f[n] == -1: k = n // 2 while k > 0 and f[k] == -1: k = k / 2 for i in range(k, n): f[i + 1] = f[i] + f[i - 1] return f[n] def solve(numbers, t, l, r, d): if t == 1: numbers[l - 1] = r elif t == 2: total = 0 for j in range(l - 1, r): total = (total + numbers[j] * fib(j - l + 1)) % 10**9 print(total) else: for j in range(l - 1, r): numbers[j] = numbers[j] + d n, m = sys.stdin.readline().rstrip().split() n = int(n) m = int(m) numbers = list(map(lambda x: int(x), sys.stdin.readline().rstrip().split())) f[0] = f[1] = 1 for i in range(m): line = sys.stdin.readline().rstrip().split() t = int(line[0]) d = 0 if t == 1: l = int(line[1]) r = int(line[2]) elif t == 2: l = int(line[1]) r = int(line[2]) else: l = int(line[1]) r = int(line[2]) d = int(line[3]) solve(numbers, t, l, r, d)
IMPORT ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR
By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher: You are given a sequence of integers a_1, a_2, ..., a_{n}. Your task is to perform on it m consecutive operations of the following type: For given numbers x_{i} and v_{i} assign value v_{i} to element a_{x}_{i}. For given numbers l_{i} and r_{i} you've got to calculate sum $\sum_{x = 0}^{r_{i} - l_{i}}(f_{x} \cdot a_{l_{i} + x})$, where f_0 = f_1 = 1 and at i ≥ 2: f_{i} = f_{i} - 1 + f_{i} - 2. For a group of three numbers l_{i} r_{i} d_{i} you should increase value a_{x} by d_{i} for all x (l_{i} ≤ x ≤ r_{i}). Smart Beaver planned a tour around great Canadian lakes, so he asked you to help him solve the given problem. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 2·10^5) — the number of integers in the sequence and the number of operations, correspondingly. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^5). Then follow m lines, each describes an operation. Each line starts with an integer t_{i} (1 ≤ t_{i} ≤ 3) — the operation type: if t_{i} = 1, then next follow two integers x_{i} v_{i} (1 ≤ x_{i} ≤ n, 0 ≤ v_{i} ≤ 10^5); if t_{i} = 2, then next follow two integers l_{i} r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n); if t_{i} = 3, then next follow three integers l_{i} r_{i} d_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n, 0 ≤ d_{i} ≤ 10^5). The input limits for scoring 30 points are (subproblem E1): It is guaranteed that n does not exceed 100, m does not exceed 10000 and there will be no queries of the 3-rd type. The input limits for scoring 70 points are (subproblems E1+E2): It is guaranteed that there will be queries of the 1-st and 2-nd type only. The input limits for scoring 100 points are (subproblems E1+E2+E3): No extra limitations. -----Output----- For each query print the calculated sum modulo 1000000000 (10^9). -----Examples----- Input 5 5 1 3 1 2 4 2 1 4 2 1 5 2 2 4 1 3 10 2 1 5 Output 12 32 8 50 Input 5 4 1 3 1 2 4 3 1 4 1 2 2 4 1 2 10 2 1 5 Output 12 45
n, m = map(int, input().split()) a = list(map(int, input().split())) f = [1] * n for i in range(2, n): f[i] = f[i - 1] + f[i - 2] for i in range(m): t, l, r = map(int, input().split()) if t == 1: a[l - 1] = r else: sum_lr = 0 for x in range(r - l + 1): sum_lr += f[x] * a[l + x - 1] print(sum_lr % 1000000000)
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 NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher: You are given a sequence of integers a_1, a_2, ..., a_{n}. Your task is to perform on it m consecutive operations of the following type: For given numbers x_{i} and v_{i} assign value v_{i} to element a_{x}_{i}. For given numbers l_{i} and r_{i} you've got to calculate sum $\sum_{x = 0}^{r_{i} - l_{i}}(f_{x} \cdot a_{l_{i} + x})$, where f_0 = f_1 = 1 and at i ≥ 2: f_{i} = f_{i} - 1 + f_{i} - 2. For a group of three numbers l_{i} r_{i} d_{i} you should increase value a_{x} by d_{i} for all x (l_{i} ≤ x ≤ r_{i}). Smart Beaver planned a tour around great Canadian lakes, so he asked you to help him solve the given problem. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 2·10^5) — the number of integers in the sequence and the number of operations, correspondingly. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^5). Then follow m lines, each describes an operation. Each line starts with an integer t_{i} (1 ≤ t_{i} ≤ 3) — the operation type: if t_{i} = 1, then next follow two integers x_{i} v_{i} (1 ≤ x_{i} ≤ n, 0 ≤ v_{i} ≤ 10^5); if t_{i} = 2, then next follow two integers l_{i} r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n); if t_{i} = 3, then next follow three integers l_{i} r_{i} d_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n, 0 ≤ d_{i} ≤ 10^5). The input limits for scoring 30 points are (subproblem E1): It is guaranteed that n does not exceed 100, m does not exceed 10000 and there will be no queries of the 3-rd type. The input limits for scoring 70 points are (subproblems E1+E2): It is guaranteed that there will be queries of the 1-st and 2-nd type only. The input limits for scoring 100 points are (subproblems E1+E2+E3): No extra limitations. -----Output----- For each query print the calculated sum modulo 1000000000 (10^9). -----Examples----- Input 5 5 1 3 1 2 4 2 1 4 2 1 5 2 2 4 1 3 10 2 1 5 Output 12 32 8 50 Input 5 4 1 3 1 2 4 3 1 4 1 2 2 4 1 2 10 2 1 5 Output 12 45
n, m = map(int, input().split()) a = list(map(int, input().split())) f = [1, 1] for i in range(2, n + 1): f.append(f[i - 1] + f[i - 2]) f[i] %= 10**9 for q in range(m): z, l, r = map(int, input().split()) if z == 1: a[l - 1] = r else: s = 0 for j in range(l - 1, r): s += a[j] * f[j - l + 1] s %= 10**9 print(s)
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 NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR
By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher: You are given a sequence of integers a_1, a_2, ..., a_{n}. Your task is to perform on it m consecutive operations of the following type: For given numbers x_{i} and v_{i} assign value v_{i} to element a_{x}_{i}. For given numbers l_{i} and r_{i} you've got to calculate sum $\sum_{x = 0}^{r_{i} - l_{i}}(f_{x} \cdot a_{l_{i} + x})$, where f_0 = f_1 = 1 and at i ≥ 2: f_{i} = f_{i} - 1 + f_{i} - 2. For a group of three numbers l_{i} r_{i} d_{i} you should increase value a_{x} by d_{i} for all x (l_{i} ≤ x ≤ r_{i}). Smart Beaver planned a tour around great Canadian lakes, so he asked you to help him solve the given problem. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 2·10^5) — the number of integers in the sequence and the number of operations, correspondingly. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^5). Then follow m lines, each describes an operation. Each line starts with an integer t_{i} (1 ≤ t_{i} ≤ 3) — the operation type: if t_{i} = 1, then next follow two integers x_{i} v_{i} (1 ≤ x_{i} ≤ n, 0 ≤ v_{i} ≤ 10^5); if t_{i} = 2, then next follow two integers l_{i} r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n); if t_{i} = 3, then next follow three integers l_{i} r_{i} d_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n, 0 ≤ d_{i} ≤ 10^5). The input limits for scoring 30 points are (subproblem E1): It is guaranteed that n does not exceed 100, m does not exceed 10000 and there will be no queries of the 3-rd type. The input limits for scoring 70 points are (subproblems E1+E2): It is guaranteed that there will be queries of the 1-st and 2-nd type only. The input limits for scoring 100 points are (subproblems E1+E2+E3): No extra limitations. -----Output----- For each query print the calculated sum modulo 1000000000 (10^9). -----Examples----- Input 5 5 1 3 1 2 4 2 1 4 2 1 5 2 2 4 1 3 10 2 1 5 Output 12 32 8 50 Input 5 4 1 3 1 2 4 3 1 4 1 2 2 4 1 2 10 2 1 5 Output 12 45
n, m = map(int, input().split()) d = [int(x) for x in input().split()] f = [1, 1] for i in range(n): f.append((f[-1] + f[-2]) % 1000000000) for i in range(m): s = [int(x) for x in input().split()] if s[0] == 1: x = s[1] - 1 v = s[2] d[x] = v elif s[0] == 2: r = s[2] - 1 l = s[1] - 1 ans = 0 for i in range(r - l + 1): ans = (ans % 10**9 + d[i + l] % 10**9 * f[i] % 10**9) % 10**9 print(ans) elif s[0] == 3: r = s[2] - 1 l = s[1] - 1 x = s[3] for i in range(l, r + 1): d[i] += x
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 NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP NUMBER NUMBER VAR VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR
They declared Sonam as bewafa. Although she is not, believe me! She asked a number of queries to people regrading their position in a test. Now its your duty to remove her bewafa tag by answering simple queries. All the students who give test can score from 1 to 10^18. Lower the marks, better the rank. Now instead of directly telling the marks of student they have been assigned groups where marks are distributed in continuous intervals, you have been given l(i) lowest mark of interval i and r(i) highest marks in interval i. So marks distribution in that interval is given as l(i), l(i)+1, l(i)+2 . . . r(i). Now Sonam ask queries in which she gives rank of the student (rank_{i}) and you have to tell marks obtained by that student. Simply, for each query output marks obtain by student whose rank is rank_{i}(1<=rank_{i}<=10^{18}). Note: rank1 is better than rank2 and rank2 is better than rank3 and so on and the first interval starts from 1. Example 1: Input: n=3, q=3 l[] = {1, 12, 22} r[] = {10, 20, 30} rank[] = {5, 15, 25} Output: 5 16 27 Intervals are from 1 to 10, second interval from 12 to 20 and third 22 to 30. In this test case, from 1 to 10 , they are given the ranks from 1 to 10 but in the second interval, it is starting from 12 , so we will have to give its rank 11 and so on like this. Rank: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15...... Marks: 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16..... So 5th rank will score 5 marks,15th rank will score 16 marks and 25th rank will score 27 marks. Your Task: You don't need to read input or print anything. Your task is to complete the function getTestMarks() which takes the array l[], r[], its size n and array rank[], StoreAnswer[], its size q as inputs and fill the StoreAnswer[] array. This array stores the answer of every query. Expected Time Complexity: O(n. log(n)) Expected Auxiliary Space: O(n.q) Constraints: 1<=n<=10^{5} 1<=q<=10^{5} 1<= l(i) < r(i) <=10^{18} 1<=rank_{i}<=10^{18}
def construct(groups: list): sum_ = 0 data = [] for group in groups: data.append((sum_ + 1, group[0], group[1])) sum_ += group[1] - group[0] + 1 return data def search(tgt: int, data: list): l = 0 r = len(data) while l < r: mid = l + (r - l) // 2 if data[mid][0] < tgt: l = mid + 1 else: r = mid if l < len(data) and data[l][0] == tgt: tgt_i = l else: tgt_i = l - 1 tgt_group = data[tgt_i] rank_in_group = tgt - tgt_group[0] return tgt_group[1] + rank_in_group class Solution: def getTestMarks(self, n, q, r, l, rank, storeAnswer): new_l = [] new_r = [] for i in r: if i != 0: new_r.append(i) for j in l: if j != 0: new_l.append(j) groups = list(zip(new_l, new_r)) data = construct(groups) for i in range(len(rank)): storeAnswer[i] = search(rank[i], data)
FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR
They declared Sonam as bewafa. Although she is not, believe me! She asked a number of queries to people regrading their position in a test. Now its your duty to remove her bewafa tag by answering simple queries. All the students who give test can score from 1 to 10^18. Lower the marks, better the rank. Now instead of directly telling the marks of student they have been assigned groups where marks are distributed in continuous intervals, you have been given l(i) lowest mark of interval i and r(i) highest marks in interval i. So marks distribution in that interval is given as l(i), l(i)+1, l(i)+2 . . . r(i). Now Sonam ask queries in which she gives rank of the student (rank_{i}) and you have to tell marks obtained by that student. Simply, for each query output marks obtain by student whose rank is rank_{i}(1<=rank_{i}<=10^{18}). Note: rank1 is better than rank2 and rank2 is better than rank3 and so on and the first interval starts from 1. Example 1: Input: n=3, q=3 l[] = {1, 12, 22} r[] = {10, 20, 30} rank[] = {5, 15, 25} Output: 5 16 27 Intervals are from 1 to 10, second interval from 12 to 20 and third 22 to 30. In this test case, from 1 to 10 , they are given the ranks from 1 to 10 but in the second interval, it is starting from 12 , so we will have to give its rank 11 and so on like this. Rank: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15...... Marks: 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16..... So 5th rank will score 5 marks,15th rank will score 16 marks and 25th rank will score 27 marks. Your Task: You don't need to read input or print anything. Your task is to complete the function getTestMarks() which takes the array l[], r[], its size n and array rank[], StoreAnswer[], its size q as inputs and fill the StoreAnswer[] array. This array stores the answer of every query. Expected Time Complexity: O(n. log(n)) Expected Auxiliary Space: O(n.q) Constraints: 1<=n<=10^{5} 1<=q<=10^{5} 1<= l(i) < r(i) <=10^{18} 1<=rank_{i}<=10^{18}
class Solution: def getTestMarks(self, n, q, r, l, rank, storeAnswer): score = [] for i in range(1, n + 1): score.append((l[i], r[i])) score.sort() cnt = [] for i in range(n): curr = score[i][1] - score[i][0] + 1 if i == 0: cnt.append(curr) else: cnt.append(cnt[i - 1] + curr) for i in range(1, q + 1): lo, hi = 0, n - 1 ans = -1 while lo <= hi: mi = (lo + hi) // 2 if cnt[mi] >= rank[i]: ans = mi hi = mi - 1 else: lo = mi + 1 if ans == -1: storeAnswer[i] = score[n - 1][1] + rank[i] - cnt[n - 1] else: before = cnt[ans - 1] if ans > 0 else 0 storeAnswer[i] = rank[i] - before + score[ans][0] - 1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER
They declared Sonam as bewafa. Although she is not, believe me! She asked a number of queries to people regrading their position in a test. Now its your duty to remove her bewafa tag by answering simple queries. All the students who give test can score from 1 to 10^18. Lower the marks, better the rank. Now instead of directly telling the marks of student they have been assigned groups where marks are distributed in continuous intervals, you have been given l(i) lowest mark of interval i and r(i) highest marks in interval i. So marks distribution in that interval is given as l(i), l(i)+1, l(i)+2 . . . r(i). Now Sonam ask queries in which she gives rank of the student (rank_{i}) and you have to tell marks obtained by that student. Simply, for each query output marks obtain by student whose rank is rank_{i}(1<=rank_{i}<=10^{18}). Note: rank1 is better than rank2 and rank2 is better than rank3 and so on and the first interval starts from 1. Example 1: Input: n=3, q=3 l[] = {1, 12, 22} r[] = {10, 20, 30} rank[] = {5, 15, 25} Output: 5 16 27 Intervals are from 1 to 10, second interval from 12 to 20 and third 22 to 30. In this test case, from 1 to 10 , they are given the ranks from 1 to 10 but in the second interval, it is starting from 12 , so we will have to give its rank 11 and so on like this. Rank: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15...... Marks: 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16..... So 5th rank will score 5 marks,15th rank will score 16 marks and 25th rank will score 27 marks. Your Task: You don't need to read input or print anything. Your task is to complete the function getTestMarks() which takes the array l[], r[], its size n and array rank[], StoreAnswer[], its size q as inputs and fill the StoreAnswer[] array. This array stores the answer of every query. Expected Time Complexity: O(n. log(n)) Expected Auxiliary Space: O(n.q) Constraints: 1<=n<=10^{5} 1<=q<=10^{5} 1<= l(i) < r(i) <=10^{18} 1<=rank_{i}<=10^{18}
class Solution: def fun(self, arr, num): i = 0 j = len(arr) - 1 mid = (i + j) // 2 while i <= j: mid = (i + j) // 2 if arr[mid] > num: j = mid - 1 elif arr[mid] < num: i = mid + 1 else: return mid return mid def getTestMarks(self, n, q, r, l, rank, storeAnswer): temp = [] temp.append(r[1] - l[1] + 1) for i in range(2, n + 1): temp.append(r[i] - l[i] + 1 + temp[-1]) miss = [] miss.append(0) for i in range(2, n + 1): miss.append(miss[-1] + l[i] - r[i - 1] - 1) for i in range(1, q + 1): t = self.fun(temp, rank[i]) if t + 1 < n and temp[t] < rank[i] and temp[t + 1] > rank[i]: t += 1 storeAnswer[i] = miss[t] + rank[i]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Note: All numbers will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n = 9 Output: [[1,2,6], [1,3,5], [2,3,4]]
class Solution: def combinationSum3(self, k, n): if n > 45: return [] res = [] def rev(start, s, count, one): if count == k and s == n: res.append(one[:]) return if count == k or s == n: return for i in range(start, 10): if s + i > n: return rev(i + 1, s + i, count + 1, one + [i]) rev(1, 0, 0, []) return res
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR LIST FUNC_DEF IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER LIST RETURN VAR
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Note: All numbers will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n = 9 Output: [[1,2,6], [1,3,5], [2,3,4]]
class Solution: def combinationSum3(self, k, n): res = [] self.dfs(range(1, 10), k, n, 0, [], res) return res def dfs(self, nums, k, n, index, path, res): if k == 0 and n == 0: res.append(path) return for i in range(index, len(nums)): self.dfs(nums, k - 1, n - nums[i], i + 1, path + [nums[i]], res)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR NUMBER LIST VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR VAR
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Note: All numbers will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n = 9 Output: [[1,2,6], [1,3,5], [2,3,4]]
class Solution: def combinationSum3(self, k, n): to_return = [] self.backtrack(to_return, [], k, n, 1) return to_return def backtrack(self, to_return, temp, k, n, start): total = sum(temp) if total > n: return if len(temp) == k and total == n: to_return.append(temp[:]) return for i in range(start, 10): temp.append(i) self.backtrack(to_return, temp, k, n, i + 1) temp.pop()
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Note: All numbers will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n = 9 Output: [[1,2,6], [1,3,5], [2,3,4]]
class Solution: def combinationSum3(self, k, n): res = [] self.dfs(res, [], 0, 0, 1, k, n) return res def dfs(self, res, cur, d, s, b, k, n): if d == k and s == n: res.append(cur) return for i in range(b, 10): self.dfs(res, cur + [i], d + 1, s + i, i + 1, k, n)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST NUMBER NUMBER NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Note: All numbers will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n = 9 Output: [[1,2,6], [1,3,5], [2,3,4]]
class Solution: result = [] def combinationSum3Util(self, candidates, current_subset, k, target, start): if target == 0 and k == 0: Solution.result.append(current_subset) elif target != 0 and k != 0: for i in range(start, len(candidates)): current_candidate_num = candidates[i] if i > start and candidates[i] == candidates[i - 1]: continue if target - current_candidate_num >= 0: next_subset = current_subset.copy() next_subset.append(current_candidate_num) self.combinationSum3Util( candidates, next_subset, k - 1, target - current_candidate_num, i + 1, ) else: return else: return def combinationSum3(self, k, n): Solution.result = [] if k == 0: return [[]] candidates = [x for x in range(1, 10)] current_subset = [] self.combinationSum3Util(candidates, current_subset, k, n, 0) return Solution.result
CLASS_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN RETURN FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER RETURN LIST LIST ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Note: All numbers will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n = 9 Output: [[1,2,6], [1,3,5], [2,3,4]]
class Solution: def combinationSum3(self, k, n): res, com = [], [] self.combination(n, res, com, 1, k) return res def combination(self, target, res, com, begin, k): for i in range(begin, target + 1 if target < 9 else 10): com.append(i) if i == target and k == 1: res.append(com[:]) self.combination(target - i, res, com, i + 1, k - 1) com.pop()
CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Note: All numbers will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n = 9 Output: [[1,2,6], [1,3,5], [2,3,4]]
class Solution: def combinationSum3(self, k, n): res = [] temp = [] idx = 1 self.backtracking(res, temp, n, k, idx) return res def backtracking(self, res, temp, n, k, idx): if len(temp) == k and n == 0: res.append(temp[:]) return for i in range(idx, 10): temp.append(i) self.backtracking(res, temp, n - i, k, i + 1) temp.pop()
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): def rem(s): l = [s[0]] i = 1 n = len(s) while i < n: if s[i] != s[i - 1]: l += [s[i]] else: p = s[i] j = i + 1 if j < n: while j < n: if s[j] != p: l.pop() l += [s[j]] i = j break j += 1 else: l.pop() i += 1 return "".join(l) b = S[:] while True: f = 1 if b == "" or len(b) == 1: return b if len(b) == 2: if b[0] == b[1]: return "" else: return b for i in range(1, len(b)): if b[i] == b[i - 1]: f = 0 break if f == 0: b = rem(b) else: return b
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR LIST VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR WHILE VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL STRING VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR NUMBER IF VAR STRING FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN STRING RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, s): sm = len(s) is_con = True while is_con: is_con = False buf = [] i = 0 while i < sm: j = i + 1 while j < sm and s[j] == s[i]: j += 1 if j == i + 1: buf.append(s[i]) i += 1 else: is_con = True i = j s = "".join(buf) sm = len(s) return s
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): ans = "" if len(S) <= 1: return S for i in range(0, len(S)): if i == 0: if S[i] != S[i + 1]: ans = ans + S[i] elif i == len(S) - 1: if S[i] != S[i - 1]: ans = ans + S[i] elif S[i] != S[i - 1] and S[i] != S[i + 1]: ans = ans + S[i] if len(S) != len(ans): return self.rremove(ans) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, s): def remove(s, old): if s == "": return s elif s == old: return s old = s arr = list(s) prev = 0 for i in range(1, len(s)): if arr[i] == arr[i - 1]: prev = 1 arr[i - 1] = "" elif prev == 1: arr[i - 1] = "" prev = 0 if prev == 1: arr[i] = "" s = "".join(arr) return remove(s, old) return remove(s, "")
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR STRING RETURN VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR STRING
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): new = [] i = 0 n = len(S) while i < len(S): temp = 0 while i < n - 1 and S[i] == S[i + 1]: temp = 1 i += 1 if temp == 0: new.append(S[i]) i += 1 if len(new) < n: return self.rremove(new) return "".join(new)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): if not S: return S global_dups = 0 final_string = "" i = 0 while i < len(S): duplicates = 0 j = i + 1 while j < len(S) and S[j] == S[i]: duplicates = 1 global_dups += 1 j += 1 i = j - 1 if not duplicates: final_string += S[i] i += 1 return Solution().rremove(final_string) if global_dups else final_string
CLASS_DEF FUNC_DEF IF VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_CALL FUNC_CALL VAR VAR VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, s): n = len(s) word = [] i = 0 while i < len(s): flag = 0 while i < n - 1 and s[i] == s[i + 1]: i += 1 flag = 1 if not flag: word.append(s[i]) i += 1 if len(word) < n: return self.rremove("".join(word)) return "".join(word)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FUNC_CALL STRING VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): if len(S) < 2: return S i = 0 j = 1 is_duplicate = False modified = False non_duplicates = [] while j < len(S): if S[j] == S[i]: j += 1 is_duplicate = True elif is_duplicate: i = j j += 1 is_duplicate = False modified = True else: non_duplicates.append(S[i]) i = j j += 1 if not is_duplicate: non_duplicates.append(S[i]) updated_str = "".join(non_duplicates) if modified: return self.rremove(updated_str) return updated_str
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR RETURN FUNC_CALL VAR VAR RETURN VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, s): s1 = "" while len(s1) != len(s): s1 = s s = self.fun(s) return s def fun(self, s): ans = "" n = len(s) i = 0 while i < n: if i < n - 1 and s[i] == s[i + 1]: while i < n - 1 and s[i] == s[i + 1]: i += 1 else: ans += s[i] i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): def rems(S): i = 0 ans = [] while i < len(S): if i < len(S) - 1: if S[i] == S[i + 1]: while i < len(S) - 1 and S[i] == S[i + 1]: i = i + 1 else: ans.append(S[i]) else: ans.append(S[i]) i = i + 1 return "".join(ans) ans = "" k = rems(S) while len(ans) != len(k): ans = k k = rems(ans) return k
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL STRING VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): l = len(S) a = [] d = False for i in range(len(S)): if i == 0: if S[i] != S[i + 1]: a.append(S[i]) elif i < len(S) - 1: if S[i] != S[i + 1] and S[i] != S[i - 1]: a.append(S[i]) elif S[-1] != S[-2]: a.append(S[-1]) k = "".join(a) for i in range(len(k) - 1): if k[i] == k[i + 1]: d = True break if d: return self.rremove(k) else: return k
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN FUNC_CALL VAR VAR RETURN VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): new = [i for i in S] st = [] i = 0 while i < len(S): if st and st[-1] == S[i]: while i < len(S) and S[i] == st[-1]: i += 1 st.pop() if i < len(S): st.append(S[i]) i += 1 if new == st: return "".join(new) else: return self.rremove("".join(st))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL STRING VAR RETURN FUNC_CALL VAR FUNC_CALL STRING VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): def remove(s): if s == "": return "" if len(s) == 1: return s if s[0] == s[1]: new_s = "" else: new_s = s[0] i = 1 while i < len(s) - 1: if s[i] != s[i - 1] and s[i] != s[i + 1]: new_s += s[i] i += 1 else: if s[-1] != s[-2]: new_s += s[-1] if s == new_s: return s else: return remove(new_s) return remove(S)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR STRING RETURN STRING IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, s): a = "" b = "" if len(s) < 2: return s for i in range(len(s) - 1): if s[i] == s[i + 1]: a = s[i] elif s[i] == a: a = "" else: b += s[i] if s[-1] != a: b += s[-1] if len(s) == len(b): return b else: return self.rremove(b)
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR STRING VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): s_list = list(S) ns = self._remove(s_list) return "".join(ns) def _remove(self, s_lst): ns = [] prv = None lprv = None for c in s_lst: if c != prv: ns.append(c) elif lprv != c: ns.pop() lprv = prv prv = c if len(ns) == len(s_lst): return ns return self._remove(ns)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, s): res = "" i = 0 while i < len(s): if i + 1 < len(s) and s[i] == s[i + 1]: i += 1 while i < len(s) and s[i] == s[i - 1]: i += 1 else: res += s[i] i += 1 return res if len(res) == len(s) else self.rremove(res)
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): if len(S) == 0: return "" length = len(S) dupl_nums = 1 store = [] i = 0 while i < length - 1: if S[i] == S[i + 1]: start_dupl = i dupl_nums = 2 k = i + 2 while k < len(S) and S[k] == S[i]: dupl_nums += 1 k += 1 if len(store) == 0: store.append(S[0:start_dupl]) else: store.append(S[next_start:start_dupl]) next_start = i + dupl_nums i += dupl_nums else: i += 1 if store: if next_start < length: store.append(S[next_start:]) new_string = "".join(store) return self.rremove(new_string) else: return S return S
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL VAR VAR RETURN VAR RETURN VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): ans = "" duplicate = True while duplicate: i = 0 duplicate = False while i < len(S): count = 1 while i < len(S) - 1 and S[i] == S[i + 1]: count += 1 i += 1 duplicate = True if count == 1: ans += S[i] i += 1 if duplicate: S = ans ans = "" return ans
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR STRING RETURN VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def remove(self, S): ans = "" n = len(S) i = 0 while i < n: if i < n - 1 and S[i] == S[i + 1]: while i < n - 1 and S[i] == S[i + 1]: i += 1 else: ans += S[i] i += 1 return ans def rremove(self, S): temp = "" while len(S) != len(temp): temp = S S = self.remove(S) return S
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): newS = "" if len(S) == 0: return "" if len(S) == 1: return S c = "%" S += "&" for i in range(len(S) - 1): if S[i] == S[i + 1]: c = S[i] newS += "#" elif S[i] == c: newS += "#" c = "%" else: newS += S[i] newS = newS.replace("#", "") S = S[:-1] if len(newS) == len(S): return S return self.rremove(newS)
CLASS_DEF FUNC_DEF ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER RETURN STRING IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR STRING VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR STRING IF VAR VAR VAR VAR STRING ASSIGN VAR STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Given a string s, remove all its adjacent duplicate characters recursively. Note: For some test cases, the resultant string would be an empty string. In that case, the function should return the empty string only. Example 1: Input: S = "geeksforgeek" Output: "gksforgk" Explanation: g(ee)ksforg(ee)k -> gksforgk Example 2: Input: S = "abccbccba" Output: "" Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string) Your Task: You don't need to read input or print anything. Your task is to complete the function rremove() which takes the string S as input parameter and returns the resultant string. Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1<=|S|<=10^{5}
class Solution: def rremove(self, S): q = [S] newS = "" while len(q): root = q.pop(0) flg = True newS = "" i = 0 while i < len(root): j = i + 1 fl = True while j < len(root) and root[i] == root[j]: j += 1 fl = False flg = False if fl: newS += root[i] i = j q.append(newS) if flg: break return newS
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): def func(ind1, ind2, k, arr, dp, n): if ind1 >= n or ind2 >= n: return 0 if k < 0: return 0 if ind1 == n - 1 and ind2 == n - 1: if k == arr[ind1][ind2]: return 1 return 0 if dp[ind1][ind2][k] != 0: return dp[ind1][ind2][k] down = func(ind1 + 1, ind2, k - arr[ind1][ind2], arr, dp, n) right = func(ind1, ind2 + 1, k - arr[ind1][ind2], arr, dp, n) dp[ind1][ind2][k] = down + right return dp[ind1][ind2][k] dp = [[([0] * (K + 1)) for i in range(N)] for j in range(N)] kk = func(0, 0, K, arr, dp, N) return kk
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): dp = {} def fun(i, j, su): if i >= N or j >= N: return 0 if i == N - 1 and j == N - 1: if su == arr[i][j]: return 1 else: return 0 if su < 0: return 0 if (i, j, su) in dp: return dp[i, j, su] dp[i, j, su] = fun(i + 1, j, su - arr[i][j]) + fun(i, j + 1, su - arr[i][j]) return dp[i, j, su] return fun(0, 0, K)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
import sys sys.setrecursionlimit(10**6) class Solution: dp = [[([-1] * 101) for i in range(101)] for i in range(101)] a = [([0] * 101) for i in range(101)] def go(self, n, m, k): if k < 0: return 0 if m < 0 or n < 0: return 0 if n == 0 and m == 0: self.dp[n][m][k] = 1 if k == self.a[n][m] else 0 return self.dp[n][m][k] if self.dp[n][m][k] != -1: return self.dp[n][m][k] left = self.go(n, m - 1, k - self.a[n][m]) up = self.go(n - 1, m, k - self.a[n][m]) self.dp[n][m][k] = left + up return self.dp[n][m][k] def numberOfPath(self, n, k, arr): for i in range(n): for j in range(n): self.a[i][j] = arr[i][j] for i in range(n): for j in range(n): for l in range(k + 1): self.dp[i][j][l] = -1 self.go(n - 1, n - 1, k) return self.dp[n - 1][n - 1][k]
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER RETURN VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
def pathCountDPRecDP(mat, m, n, k, dp): if m < 0 or n < 0 or k < 0: return 0 elif m == 0 and n == 0: return k == mat[m][n] if dp[m][n][k] != -1: return dp[m][n][k] dp[m][n][k] = pathCountDPRecDP(mat, m - 1, n, k - mat[m][n], dp) + pathCountDPRecDP( mat, m, n - 1, k - mat[m][n], dp ) return dp[m][n][k] class Solution: def numberOfPath(self, N, K, arr): dp = [ [[(-1) for co in range(K + 1)] for col in range(N + 1)] for row in range(N + 1) ] return int(pathCountDPRecDP(arr, N - 1, N - 1, K, dp))
FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): paths = {} for y in range(N - 1, -1, -1): for x in range(N - 1, -1, -1): coord = x, y coins = arr[x][y] if x < N - 1: x_paths = paths.get((x + 1, y), {}) else: x_paths = {} if y < N - 1: y_paths = paths.get((x, y + 1), {}) else: y_paths = {} my_paths = {} for k, path_count in x_paths.items(): my_paths[k] = my_paths.get(k, 0) + path_count for k, path_count in y_paths.items(): my_paths[k] = my_paths.get(k, 0) + path_count new_paths = {} if not my_paths and x == N - 1 and y == N - 1: new_paths[coins] = 1 else: for k, path_count in my_paths.items(): new_val = k + coins if new_val <= K: new_paths[new_val] = path_count paths[coord] = new_paths return paths[0, 0].get(K, 0)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR DICT ASSIGN VAR DICT IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR DICT IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR NUMBER
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, A): def _solve(r, c, left): if r >= N or c >= N: return 0 cell = A[r][c] if cell > left: return 0 if r == c == N - 1: return 1 if left == cell else 0 v = dp[r][c].get(left, -1) if v >= 0: return v ans = dp[r][c][left] = _solve(r + 1, c, left - cell) + _solve( r, c + 1, left - cell ) return ans dp = [[{} for _ in range(N)] for _ in range(N)] ans = _solve(0, 0, K) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN VAR ASSIGN VAR DICT VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR RETURN VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def pathCountDPRecDP(self, mat, m, n, k, dp): if m < 0 or n < 0 or k < 0: return 0 elif m == 0 and n == 0: return 1 if k == mat[m][n] else 0 if dp[m][n][k] != -1: return dp[m][n][k] dp[m][n][k] = self.pathCountDPRecDP( mat, m - 1, n, k - mat[m][n], dp ) + self.pathCountDPRecDP(mat, m, n - 1, k - mat[m][n], dp) return dp[m][n][k] def numberOfPath(self, N, K, arr): dp = [[[(-1) for col in range(110)] for col in range(N)] for row in range(N)] return self.pathCountDPRecDP(arr, N - 1, N - 1, K, dp) def numberOfPath1(self, N, K, arr): dx = [0, 1] dy = [1, 0] def inside(x, y): return x >= 0 and x < N and y >= 0 and y < N dp1 = [ [[(0) for i in range(K + 5)] for j in range(N + 5)] for k in range(N + 5) ] for i in range(N): for j in range(N): dp1[i][j][0] = 1 for s in range(1, K + 1): for x in range(N): for y in range(N): if x == 0 or y == 0: if s - arr[x][y] >= 0: dp1[x][y][s] = dp1[x][y][s - arr[x][y]] elif s - arr[x][y] >= 0: dp1[x][y][s] += dp1[x - 1][y][s - arr[x][y]] dp1[x][y][s] += dp1[x][y - 1][s - arr[x][y]] return sum(dp1[:][:][K])
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
def helper(i, j, s, arr, n, m, dp): if not i and not j: return 1 if s == arr[i][j] else 0 if i < 0 or j < 0 or s < 1: return 0 if dp[i][j][s] != -1: return dp[i][j][s] up = helper(i - 1, j, s - arr[i][j], arr, n, m, dp) dn = helper(i, j - 1, s - arr[i][j], arr, n, m, dp) dp[i][j][s] = up + dn return dp[i][j][s] class Solution: def numberOfPath(self, N, K, arr): si = len(arr) le = len(arr[0]) assert si == N and le == N dp = [[([-1] * (K + 1)) for _ in range(N)] for _ in range(N)] ans = helper(N - 1, N - 1, K, arr, N, N, dp) return ans
FUNC_DEF IF VAR VAR RETURN VAR VAR VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def solve(self, mat, m, n, k, dp): if m < 0 or n < 0 or k < 0: return 0 elif m == 0 and n == 0: if k == mat[m][n]: return 1 return 0 if (m, n, k) in dp: return dp[m, n, k] dp[m, n, k] = self.solve(mat, m - 1, n, k - mat[m][n], dp) + self.solve( mat, m, n - 1, k - mat[m][n], dp ) return dp[m, n, k] def numberOfPath(self, N, K, arr): dp = {} return self.solve(arr, N - 1, N - 1, K, dp)
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): dp = [[[(-1) for _ in range(K + 1)] for _ in range(N)] for _ in range(N)] return self.solve(arr, dp, N, K, 0, 0, 0) def solve(self, arr, dp, N, K, x, y, k): if x >= N or y >= N or x < 0 or y < 0 or k + arr[x][y] > K: return 0 if x == N - 1 and y == N - 1 and k + arr[x][y] == K: return 1 if dp[x][y][k] != -1: return dp[x][y][k] dp[x][y][k] = self.solve(arr, dp, N, K, x + 1, y, k + arr[x][y]) + self.solve( arr, dp, N, K, x, y + 1, k + arr[x][y] ) return dp[x][y][k]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): return self.paths(0, 0, K, arr, N, {}) def paths(self, cr, cc, K, arr, N, memo): if cr >= N or cc >= N: return 0 if cr == N - 1 and cc == N - 1: if K - arr[cr][cc] != 0: return 0 if cr == N - 1 and cc == N - 1: if K - arr[cr][cc] == 0: return 1 currentKey = str(cr) + "-" + str(cc) + "-" + str(K) if currentKey in memo: return memo[currentKey] one = self.paths(cr + 1, cc, K - arr[cr][cc], arr, N, memo) two = self.paths(cr, cc + 1, K - arr[cr][cc], arr, N, memo) memo[currentKey] = one + two return memo[currentKey]
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR DICT FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, M): return dfs(0, 0, M, N, K - M[0][0], {}) def dfs(r, c, M, N, K, memo): if (r, c, K) in memo: return memo[r, c, K] if K < 0: return 0 if (r, c) == (N - 1, N - 1): if K == 0: return 1 return 0 ans = 0 for nr, nc in [(r + 1, c), (r, c + 1)]: if nr == N: continue if nc == N: continue ans += dfs(nr, nc, M, N, K - M[nr][nc], memo) memo[r, c, K] = ans return ans
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER DICT FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR LIST BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): if not arr: return if N == 1 and K > arr[0][0]: return 0 dp = [ [[(-1) for col in range(K + 1)] for col in range(N + 1)] for row in range(N + 1) ] def pathCountDPRecDP(arr, m, n, k): if m < 0 or n < 0 or k < 0: return 0 elif m == 0 and n == 0: if k == arr[m][n]: return 1 else: return 0 if dp[m][n][k] != -1: return dp[m][n][k] dp[m][n][k] = pathCountDPRecDP( arr, m - 1, n, k - arr[m][n] ) + pathCountDPRecDP(arr, m, n - 1, k - arr[m][n]) return dp[m][n][k] return pathCountDPRecDP(arr, N - 1, N - 1, K)
CLASS_DEF FUNC_DEF IF VAR RETURN IF VAR NUMBER VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): m = len(arr) n = len(arr[0]) def check(i, j, K): if i < 0 or j < 0 or K < 1: return 0 if i == 0 and j == 0: if K == arr[i][j]: return 1 else: return 0 if dp[i][j][K] != -1: return dp[i][j][K] first = check(i - 1, j, K - arr[i][j]) second = check(i, j - 1, K - arr[i][j]) dp[i][j][K] = first + second return dp[i][j][K] dp = [[[(-1) for i in range(K + 1)] for i in range(n)] for i in range(n)] return check(m - 1, n - 1, K)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, n, k, l): dp = [[{} for _ in range(n)] for _ in range(n)] dp[-1][-1][l[-1][-1]] = 1 for i in range(n - 2, -1, -1): for t in dp[i + 1][-1]: x = t + l[i][-1] if x <= k: if x in dp[i][-1]: dp[i][-1][x] += dp[i + 1][-1][t] else: dp[i][-1][x] = dp[i + 1][-1][t] for t in dp[-1][i + 1]: x = t + l[-1][i] if x <= k: if x in dp[-1][i]: dp[-1][i][x] += dp[-1][i + 1][t] else: dp[-1][i][x] = dp[-1][i + 1][t] for i in range(n - 2, -1, -1): for j in range(n - 2, -1, -1): for t in dp[i + 1][j]: x = t + l[i][j] if x <= k: if x in dp[i][j]: dp[i][j][x] += dp[i + 1][j][t] else: dp[i][j][x] = dp[i + 1][j][t] for t in dp[i][j + 1]: x = t + l[i][j] if x <= k: if x in dp[i][j]: dp[i][j][x] += dp[i][j + 1][t] else: dp[i][j][x] = dp[i][j + 1][t] if k in dp[0][0]: return dp[0][0][k] return 0
CLASS_DEF FUNC_DEF ASSIGN VAR DICT VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER VAR RETURN NUMBER
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
def f(A, N, summ, x, y, dp): if x >= N or y >= N or summ < 0: return 0 if x == N - 1 and y == N - 1 and summ - A[x][y] == 0: return 1 if dp[x][y][summ] != -1: return dp[x][y][summ] ans = f(A, N, summ - A[x][y], x + 1, y, dp) + f(A, N, summ - A[x][y], x, y + 1, dp) dp[x][y][summ] = ans return ans class Solution: def numberOfPath(self, N, K, A): dp = [ [[(-1) for i in range(K + 1)] for i in range(N + 1)] for i in range(N + 1) ] ans = 0 try: ans = f(A, N, K, 0, 0, dp) except Exception as e: print(e) return ans
FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): def solve(i, j, n, k, arr, dp): if i >= n or j >= n or k < 0: return 0 if i == n - 1 and j == n - 1: if k == arr[i][j]: return 1 return 0 if (i, j, k) in ways: return ways[i, j, k] ways[i, j, k] = solve(i + 1, j, n, k - arr[i][j], arr, dp) + solve( i, j + 1, n, k - arr[i][j], arr, dp ) return ways[i, j, k] ways = {} return solve(0, 0, N, K, arr, ways)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def recur(self, m, n, k, a): global dp if m < 0 or n < 0 or k < 0: return 0 if m == 0 and n == 0: return k == a[0][0] if dp[m][n][k]: return dp[m][n][k] dp[m][n][k] = self.recur(m - 1, n, k - a[m][n], a) + self.recur( m, n - 1, k - a[m][n], a ) return dp[m][n][k] def numberOfPath(self, N, K, arr): global dp dp = [[[(0) for i in range(K + 1)] for j in range(N)] for k in range(N)] ans = self.recur(N - 1, N - 1, K, arr) return ans if ans else 0
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR NUMBER
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def memo(self, m, n, cost, grid, dp): if m == 0 and n == 0: return 1 if cost - grid[m][n] == 0 else 0 if m < 0 or n < 0 or cost < 0: return 0 if dp[m][n][cost] != -1: return dp[m][n][cost] dp[m][n][cost] = self.memo(m - 1, n, cost - grid[m][n], grid, dp) + self.memo( m, n - 1, cost - grid[m][n], grid, dp ) return dp[m][n][cost] def numberOfPath(self, N, K, arr): dp = [[([-1] * (K + 1)) for j in range(N)] for i in range(N)] return self.memo(N - 1, N - 1, K, arr, dp)
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def util(self, a, i, j, n, m, k): global dp if i >= n or j >= m or k < 0: return 0 if i == n - 1 and j == m - 1: if k - a[i][j] == 0: return 1 return 0 if dp[i][j][k] != -1: return dp[i][j][k] dp[i][j][k] = self.util(a, i + 1, j, n, m, k - a[i][j]) + self.util( a, i, j + 1, n, m, k - a[i][j] ) return dp[i][j][k] def numberOfPath(self, N, K, arr): global dp dp = [[[(-1) for x in range(K + 1)] for i in range(N)] for j in range(N)] return self.util(arr, 0, 0, N, N, K)
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): def dfs(arr, i, j, k): if i < 0 or j < 0 or k < 0: return 0 if i == 0 and j == 0: return k == arr[i][j] if dp[i][j][k] != -1: return dp[i][j][k] dp[i][j][k] = dfs(arr, i - 1, j, k - arr[i][j]) + dfs( arr, i, j - 1, k - arr[i][j] ) return dp[i][j][k] r = len(arr) c = len(arr[0]) dp = [[[(-1) for i in range(K + 1)] for j in range(c)] for m in range(r)] z = dfs(arr, r - 1, c - 1, K) if z == False: return 0 else: return z
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER RETURN VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, n, k, a): v = [([False] * n) for i in range(n)] d = [([] * n) for i in range(n)] dp = {} for i in range(n): for j in range(n): d[i].append(dp.copy()) d[n - 1][n - 1][a[n - 1][n - 1]] = 1 def solve(i, j, d, v): if i > n - 1 or j > n - 1: return {} if v[i][j]: return d[i][j] else: x = solve(i, j + 1, d, v) y = solve(i + 1, j, d, v) for z in x: if z + a[i][j] not in d[i][j]: d[i][j][z + a[i][j]] = x[z] else: d[i][j][z + a[i][j]] += x[z] for z in y: if z + a[i][j] not in d[i][j]: d[i][j][z + a[i][j]] = y[z] else: d[i][j][z + a[i][j]] += y[z] v[i][j] = True return d[i][j] d[0][0] = solve(0, 0, d, v) if k in d[0][0]: return d[0][0][k] return 0
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN DICT IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER VAR VAR IF VAR VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER VAR RETURN NUMBER
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): memo = [[([-1] * (K + 1)) for j in range(N)] for i in range(N)] return self.dfs(0, 0, N, arr, K, memo) def dfs(self, i, j, N, arr, k, memo): if i < 0 or i > N - 1 or j < 0 or j > N - 1: return 0 if memo[i][j][k] != -1: return memo[i][j][k] if k - arr[i][j] < 0: return 0 if k - arr[i][j] == 0 and i == N - 1 and j == N - 1: return 1 count = 0 count += self.dfs(i, j + 1, N, arr, k - arr[i][j], memo) count += self.dfs(i + 1, j, N, arr, k - arr[i][j], memo) memo[i][j][k] = count return count
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): dp = [[([-1] * (K + 1)) for _ in range(N)] for __ in range(N)] def recur(i, j, k, dp): if i >= N or j >= N or k < 0: return 0 if i == N - 1 and j == N - 1: if k == arr[i][j]: return 1 return 0 if dp[i][j][k] != -1: return dp[i][j][k] dp[i][j][k] = recur(i + 1, j, k - arr[i][j], dp) + recur( i, j + 1, k - arr[i][j], dp ) return dp[i][j][k] return recur(0, 0, K, dp)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): d = {} def calPathSum(i, j, s): if (i, j, s) in d: return d[i, j, s] if i < 0 or i >= N or j < 0 or j >= N: d[i, j, s] = 0 return 0 if i == N - 1 and j == N - 1: if s + arr[i][j] == K: d[i, j, s] = 1 return 1 else: d[i, j, s] = 0 return 0 else: p1 = calPathSum(i + 1, j, arr[i][j] + s) p2 = calPathSum(i, j + 1, arr[i][j] + s) d[i, j, s] = p1 + p2 return p1 + p2 return calPathSum(0, 1, 1) + calPathSum(1, 0, 1)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): t = [ [[(-1) for _ in range(100 + 1)] for j in range(N + 1)] for k in range(N + 1) ] def solve(r, c, rem): if r < 0 or c < 0 or r >= N or c >= N or rem < 0: return 0 if r == N - 1 and c == N - 1: return int(rem == arr[r][c]) if t[r][c][rem] != -1: return t[r][c][rem] t[r][c][rem] = solve(r + 1, c, rem - arr[r][c]) + solve( r, c + 1, rem - arr[r][c] ) return t[r][c][rem] return solve(0, 0, K)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def find(self, i, j, K, arr, N, dp): if i == N or j == N: return 0 if i == N - 1 and j == N - 1 and K - arr[i][j] == 0: return 1 if K <= 0: return 0 if dp[i][j][K] != -1: return dp[i][j][K] dp[i][j][K] = self.find(i + 1, j, K - arr[i][j], arr, N, dp) + self.find( i, j + 1, K - arr[i][j], arr, N, dp ) return dp[i][j][K] def numberOfPath(self, N, K, arr): dp = [] for i in range(N): dp1 = [] for j in range(N): dp2 = [] for k in range(K + 1): dp2.append(-1) dp1.append(dp2) dp.append(dp1) return self.find(0, 0, K, arr, N, dp) if __name__ == "__main__": ob = Solution() t = int(input()) for _ in range(t): K = int(input()) N = int(input()) l = list(map(int, input().split())) arr = list() c = 0 for i in range(0, N): temp = list() for j in range(0, N): temp.append(l[c]) c += 1 arr.append(temp) ans = ob.numberOfPath(N, K, arr) print(ans)
CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def f(self, cr, cc, n, k, arr, dp): if cr == n - 1 and cc == n - 1: if k == 0: return 1 return 0 if k < 0: return 0 state = cr, cc, k if state in dp: return dp[state] if cc + 1 < n: a1 = self.f(cr, cc + 1, n, k - arr[cr][cc + 1], arr, dp) else: a1 = 0 if cr + 1 < n: a2 = self.f(cr + 1, cc, n, k - arr[cr + 1][cc], arr, dp) else: a2 = 0 dp[state] = a1 + a2 return a1 + a2 def numberOfPath(self, N, K, arr): cr = 0 cc = 0 n = N k = K dp = {} return self.f(cr, cc, n, k - arr[cr][cc], arr, dp)
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR
Given a N x N matrix such that each of its cells contains some coins. Count the number of ways to collect exactly K coins while moving from top left corner of the matrix to the bottom right. From a cell (i, j), you can only move to (i+1, j) or (i, j+1). Example 1: Input: K = 12, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [3, 2, 1]] Output: 2 Explanation: There are 2 possible paths with exactly K coins, (1 + 2 + 6 + 2 + 1) and (1 + 2 + 3 + 5 + 1). Example 2: Input: K = 16, N = 3 arr[] = [[1, 2, 3], [4, 6, 5], [9, 8, 7]] Output: 0 Explanation: There are no possible paths that lead to sum=16 Your Task: You don't need to read input or print anything. Your task is to complete the function numberOfPath() which takes N, K and 2D matrix arr[][] as input parameters and returns the number of possible paths. Expected Time Complexity: O(n*n*k) Expected Auxiliary Space: O(n*n*k) Constraints: 1 <= K < 100 1 <= N < 100 1 <= arr_{ij} <= 200
class Solution: def numberOfPath(self, N, K, arr): memo = {} def helper(i, j, k): if i >= N or j >= N or k < 0: return 0 if i == N - 1 and j == N - 1: return 1 if k == arr[i][j] else 0 if (i, j, k) in memo: return memo[i, j, k] ways = helper(i + 1, j, k - arr[i][j]) + helper(i, j + 1, k - arr[i][j]) memo[i, j, k] = ways return ways return helper(0, 0, K)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on. Limak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth n and a duration for each level of recursion t_1, t_2, ..., t_{n}. Once Limak launches the firework in some cell, the firework starts moving upward. After covering t_1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering t_2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the n-th level of recursion, when all 2^{n} - 1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it's possible that some parts will be at the same place and at the same time — it is allowed and such parts do not crash. Before launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells? -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 30) — the total depth of the recursion. The second line contains n integers t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ 5). On the i-th level each of 2^{i} - 1 parts will cover t_{i} cells before exploding. -----Output----- Print one integer, denoting the number of cells which will be visited at least once by any part of the firework. -----Examples----- Input 4 4 2 2 3 Output 39 Input 6 1 1 1 1 1 3 Output 85 Input 1 3 Output 3 -----Note----- For the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered t_1 = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an 'X' character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39. [Image] For the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level. [Image]
from sys import stdin, stdout def ri(): return map(int, input().split()) w = 15 adding = [(0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1), (1, 0), (1, 1)] def fill(x, y, dir, depth): for i in range(t[depth]): x += adding[dir][0] y += adding[dir][1] v[x][y] = 1 def dfs(x, y, dir, depth): if depth == n: return if (dir, depth) in T[x][y]: return fill(x, y, dir, depth) ndepth = depth + 1 nx = x + adding[dir][0] * t[depth] ny = y + adding[dir][1] * t[depth] ndir = (dir + 1) % 8 dfs(nx, ny, ndir, ndepth) ndir = (8 + dir - 1) % 8 dfs(nx, ny, ndir, ndepth) T[x][y].add(tuple((dir, depth))) n = int(input()) t = list(ri()) v = [[(0) for i in range(n * w)] for j in range(n * w)] T = [[set() for i in range(n * w)] for j in range(n * w)] dfs(n * w // 2, n * w // 2, 0, 0) ans = 0 for i in range(n * w): for j in range(n * w): if v[i][j]: ans += 1 print(ans)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN IF VAR VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on. Limak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth n and a duration for each level of recursion t_1, t_2, ..., t_{n}. Once Limak launches the firework in some cell, the firework starts moving upward. After covering t_1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering t_2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the n-th level of recursion, when all 2^{n} - 1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it's possible that some parts will be at the same place and at the same time — it is allowed and such parts do not crash. Before launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells? -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 30) — the total depth of the recursion. The second line contains n integers t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ 5). On the i-th level each of 2^{i} - 1 parts will cover t_{i} cells before exploding. -----Output----- Print one integer, denoting the number of cells which will be visited at least once by any part of the firework. -----Examples----- Input 4 4 2 2 3 Output 39 Input 6 1 1 1 1 1 3 Output 85 Input 1 3 Output 3 -----Note----- For the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered t_1 = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an 'X' character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39. [Image] For the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level. [Image]
import time coord = set() flows = int(input()) start = time.time() t = list(map(int, input().split())) directions = { (0): (0, 1), (1): (1, 1), (2): (1, 0), (3): (1, -1), (4): (0, -1), (5): (-1, -1), (6): (-1, 0), (7): (-1, 1), } was = [ [[[(0) for j in range(300)] for i in range(300)] for x in range(31)] for y in range(8) ] def flight(n, x=0, y=0, direction=0): if was[direction][n][x][y]: return was[direction][n][x][y] = 1 if n: for _ in range(t[flows - n]): x += directions[direction][0] y += directions[direction][1] coord.add((x, y)) flight(n - 1, x, y, (direction + 1) % 8) flight(n - 1, x, y, (direction - 1) % 8) flight(flows) print(len(coord))
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on. Limak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth n and a duration for each level of recursion t_1, t_2, ..., t_{n}. Once Limak launches the firework in some cell, the firework starts moving upward. After covering t_1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering t_2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the n-th level of recursion, when all 2^{n} - 1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it's possible that some parts will be at the same place and at the same time — it is allowed and such parts do not crash. Before launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells? -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 30) — the total depth of the recursion. The second line contains n integers t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ 5). On the i-th level each of 2^{i} - 1 parts will cover t_{i} cells before exploding. -----Output----- Print one integer, denoting the number of cells which will be visited at least once by any part of the firework. -----Examples----- Input 4 4 2 2 3 Output 39 Input 6 1 1 1 1 1 3 Output 85 Input 1 3 Output 3 -----Note----- For the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered t_1 = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an 'X' character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39. [Image] For the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level. [Image]
dr = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]] n = int(input()) t = [int(i) for i in input().split()] visited = set() N = 150 * 2 + 10 covers = [([0] * N) for i in range(N)] def dfs(d, pos, path): mem = d, pos, path if mem in visited: return set() else: visited.add(mem) if d == n + 1: return for _ in range(t[d - 1]): pos = pos[0] + dr[path][0], pos[1] + dr[path][1] covers[pos[0]][pos[1]] = 1 dfs(d + 1, pos, (path + 1) % 8) dfs(d + 1, pos, (path - 1) % 8) dfs(1, (int(N / 2), int(N / 2)), 0) print(sum([sum(i) for i in covers]))
ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on. Limak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth n and a duration for each level of recursion t_1, t_2, ..., t_{n}. Once Limak launches the firework in some cell, the firework starts moving upward. After covering t_1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering t_2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the n-th level of recursion, when all 2^{n} - 1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it's possible that some parts will be at the same place and at the same time — it is allowed and such parts do not crash. Before launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells? -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 30) — the total depth of the recursion. The second line contains n integers t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ 5). On the i-th level each of 2^{i} - 1 parts will cover t_{i} cells before exploding. -----Output----- Print one integer, denoting the number of cells which will be visited at least once by any part of the firework. -----Examples----- Input 4 4 2 2 3 Output 39 Input 6 1 1 1 1 1 3 Output 85 Input 1 3 Output 3 -----Note----- For the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered t_1 = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an 'X' character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39. [Image] For the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level. [Image]
import sys plane = set() crackers = {((0, -1), (0, 1))} n = int(input()) a = [int(x) for x in input().split()] crack_dict = { (1, 0): ((1, -1), (1, 1)), (1, 1): ((1, 0), (0, 1)), (0, 1): ((1, 1), (-1, 1)), (-1, 1): ((0, 1), (-1, 0)), (-1, 0): ((-1, 1), (-1, -1)), (-1, -1): ((-1, 0), (0, -1)), (0, -1): ((-1, -1), (1, -1)), (1, -1): ((0, -1), (1, 0)), } def move(cracker): point, direc = cracker point2 = point[0] + direc[0], point[1] + direc[1] return point2, direc def crack(cracker): point, direc = cracker direc1, direc2 = crack_dict[direc] return (point, direc1), (point, direc2) for x in a: for i in range(x): crackers = set(move(cracker) for cracker in crackers) plane.update(cracker[0] for cracker in crackers) new_crackers = set() for cracker in crackers: new_crackers.update(crack(cracker)) crackers = new_crackers print(len(plane))
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on. Limak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth n and a duration for each level of recursion t_1, t_2, ..., t_{n}. Once Limak launches the firework in some cell, the firework starts moving upward. After covering t_1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering t_2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the n-th level of recursion, when all 2^{n} - 1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it's possible that some parts will be at the same place and at the same time — it is allowed and such parts do not crash. Before launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells? -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 30) — the total depth of the recursion. The second line contains n integers t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ 5). On the i-th level each of 2^{i} - 1 parts will cover t_{i} cells before exploding. -----Output----- Print one integer, denoting the number of cells which will be visited at least once by any part of the firework. -----Examples----- Input 4 4 2 2 3 Output 39 Input 6 1 1 1 1 1 3 Output 85 Input 1 3 Output 3 -----Note----- For the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered t_1 = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an 'X' character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39. [Image] For the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level. [Image]
k = int(input()) & 1 p = [] s = range(1, 6) for q in map(int, input().split()[::-1]): if k: p += [(x, -y) for x, y in p] p = [(x - q, y) for x, y in p] p += [(-x, 0) for x in s[:q]] else: p += [(y, -x) for x, y in p] p = [(x - q, y + q) for x, y in p] p += [(-x, x) for x in s[:q]] p = list(set(p)) k = 1 - k print(len(p))
ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on. Limak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth n and a duration for each level of recursion t_1, t_2, ..., t_{n}. Once Limak launches the firework in some cell, the firework starts moving upward. After covering t_1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering t_2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the n-th level of recursion, when all 2^{n} - 1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it's possible that some parts will be at the same place and at the same time — it is allowed and such parts do not crash. Before launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells? -----Input----- The first line of the input contains a single integer n (1 ≤ n ≤ 30) — the total depth of the recursion. The second line contains n integers t_1, t_2, ..., t_{n} (1 ≤ t_{i} ≤ 5). On the i-th level each of 2^{i} - 1 parts will cover t_{i} cells before exploding. -----Output----- Print one integer, denoting the number of cells which will be visited at least once by any part of the firework. -----Examples----- Input 4 4 2 2 3 Output 39 Input 6 1 1 1 1 1 3 Output 85 Input 1 3 Output 3 -----Note----- For the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered t_1 = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an 'X' character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39. [Image] For the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level. [Image]
from sys import * T = int(stdin.readline()) t = [int(s) for s in stdin.readline().split(" ")] a = [] for i in range(T - 1, -1, -1): if i % 2 == 0: a += [(e[0], -e[1]) for e in a] a = [(e[0] - t[i], e[1]) for e in a] a += [(-x - 1, 0) for x in range(t[i])] a = list(set(a)) if i % 2 == 1: a += [(e[1], -e[0]) for e in a] a = [(e[0] - t[i], e[1] + t[i]) for e in a] a += [(-x - 1, x + 1) for x in range(t[i])] a = list(set(a)) print(len(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. Example 1: Input: [1, 5, 2] Output: False Explanation: Initially, player 1 can choose between 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. Hence, player 1 will never be the winner and you need to return False. Example 2: Input: [1, 5, 233, 7] Output: True Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. Note: 1 Any scores in the given array are non-negative integers and will not exceed 10,000,000. If the scores of both players are equal, then player 1 is still the winner.
class Solution: def PredictTheWinner(self, nums): if not nums: return True n = len(nums) if n & 1 == 0: return True dp = [0] * n for i in range(n - 1, -1, -1): for j in range(i, n): if i == j: dp[i] = nums[i] else: dp[j] = max(nums[i] - dp[j], nums[j] - dp[j - 1]) return dp[n - 1] >= 0
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER NUMBER
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. Example 1: Input: [1, 5, 2] Output: False Explanation: Initially, player 1 can choose between 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. Hence, player 1 will never be the winner and you need to return False. Example 2: Input: [1, 5, 233, 7] Output: True Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. Note: 1 Any scores in the given array are non-negative integers and will not exceed 10,000,000. If the scores of both players are equal, then player 1 is still the winner.
class Solution: def PredictTheWinner(self, nums): l = len(nums) memo = [([None] * l) for _ in range(l)] def predict(start, end, memo): if start == end: return nums[start] if memo[start][end]: return memo[start][end] one = nums[start] - predict(start + 1, end, memo) two = nums[end] - predict(start, end - 1, memo) memo[start][end] = max(one, two) return memo[start][end] return predict(0, len(nums) - 1, memo) >= 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. Example 1: Input: [1, 5, 2] Output: False Explanation: Initially, player 1 can choose between 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. Hence, player 1 will never be the winner and you need to return False. Example 2: Input: [1, 5, 233, 7] Output: True Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. Note: 1 Any scores in the given array are non-negative integers and will not exceed 10,000,000. If the scores of both players are equal, then player 1 is still the winner.
class Solution: def PredictTheWinner(self, nums): memo = [[(0) for _ in range(len(nums))] for _ in range(len(nums))] self.count = 0 def winner(left, right, player): self.count += 1 if left == right: return player * nums[left] if memo[left][right] != 0: print(nums[left : right + 1]) return memo[left][right] a = player * nums[left] + winner(left + 1, right, -player) b = player * nums[right] + winner(left, right - 1, -player) if player == 1: res = max(a, b) else: res = min(a, b) memo[left][right] = res return res self.count = 0 return winner(0, len(nums) - 1, 1) >= 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF VAR NUMBER IF VAR VAR RETURN BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. Example 1: Input: [1, 5, 2] Output: False Explanation: Initially, player 1 can choose between 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. Hence, player 1 will never be the winner and you need to return False. Example 2: Input: [1, 5, 233, 7] Output: True Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. Note: 1 Any scores in the given array are non-negative integers and will not exceed 10,000,000. If the scores of both players are equal, then player 1 is still the winner.
class Solution: p1dict = {} p2dict = {} def play1(self, nums): if str(nums) in self.p1dict: return self.p1dict[str(nums)] if len(nums) == 1: return nums[0] ret = max(self.play2(nums[1:]) + nums[0], self.play2(nums[:-1]) + nums[-1]) self.p1dict[str(nums)] = ret return ret def play2(self, nums): if str(nums) in self.p2dict: return self.p2dict[str(nums)] if len(nums) == 1: return -nums[0] ret = min(self.play1(nums[1:]) - nums[0], self.play1(nums[:-1]) - nums[-1]) self.p2dict[str(nums)] = ret return ret def PredictTheWinner(self, nums): return self.play1(nums) >= 0
CLASS_DEF ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. Example 1: Input: [1, 5, 2] Output: False Explanation: Initially, player 1 can choose between 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. Hence, player 1 will never be the winner and you need to return False. Example 2: Input: [1, 5, 233, 7] Output: True Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. Note: 1 Any scores in the given array are non-negative integers and will not exceed 10,000,000. If the scores of both players are equal, then player 1 is still the winner.
class Solution: def PredictTheWinner(self, nums): N = len(nums) dp = [([0] * N) for _ in range(N)] pre_sum = [nums[0]] * (N + 1) pre_sum[-1] = 0 for i in range(1, N): pre_sum[i] = pre_sum[i - 1] + nums[i] for i in range(N): dp[i][i] = nums[i] for l in range(1, N): for i in range(N - l): dp[i][i + l] = max( pre_sum[i + l] - pre_sum[i - 1] - dp[i + 1][i + l], pre_sum[i + l] - pre_sum[i - 1] - dp[i][i + l - 1], ) return dp[0][N - 1] * 2 >= pre_sum[N - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. Example 1: Input: [1, 5, 2] Output: False Explanation: Initially, player 1 can choose between 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. Hence, player 1 will never be the winner and you need to return False. Example 2: Input: [1, 5, 233, 7] Output: True Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. Note: 1 Any scores in the given array are non-negative integers and will not exceed 10,000,000. If the scores of both players are equal, then player 1 is still the winner.
class Solution: def PredictTheWinner(self, nums): cache = {} def whose_turn(i, j): return (len(nums) - (j - i + 1)) % 2 == 0 def traverse(i, j): if (i, j) in cache: return cache[i, j] p1_turn = whose_turn(i, j) if i == j: return (nums[i], 0) if p1_turn else (0, nums[i]) if i + 1 == j: winning_move = max(nums[i], nums[j]) losing_move = min(nums[i], nums[j]) cache[i, j] = ( (winning_move, losing_move) if p1_turn else (losing_move, winning_move) ) return cache[i, j] p1_left, p2_left = traverse(i + 1, j) p1_right, p2_right = traverse(i, j - 1) if p1_turn: p1_move = max(p1_left + nums[i], p1_right + nums[j]) p2_move = ( p2_left if p1_left + nums[i] >= p1_right + nums[j] else p2_right ) else: p1_move = ( p1_left if p2_left + nums[i] >= p2_right + nums[j] else p1_right ) p2_move = max(p2_left + nums[i], p2_right + nums[j]) cache[i, j] = p1_move, p2_move return cache[i, j] p1_final, p2_final = traverse(0, len(nums) - 1) return p1_final >= p2_final
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR VAR VAR NUMBER NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. Example 1: Input: [1, 5, 2] Output: False Explanation: Initially, player 1 can choose between 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. Hence, player 1 will never be the winner and you need to return False. Example 2: Input: [1, 5, 233, 7] Output: True Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. Note: 1 Any scores in the given array are non-negative integers and will not exceed 10,000,000. If the scores of both players are equal, then player 1 is still the winner.
class Solution: def PredictTheWinner(self, nums): def helper(nums, start, end, mem): if start == end: return nums[start] if (start, end) not in mem: mem[start, end] = max( nums[start] - helper(nums, start + 1, end, mem), nums[end] - helper(nums, start, end - 1, mem), ) return mem[start, end] return helper(nums, 0, len(nums) - 1, {}) >= 0
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER DICT NUMBER
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. Example 1: Input: [1, 5, 2] Output: False Explanation: Initially, player 1 can choose between 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. Hence, player 1 will never be the winner and you need to return False. Example 2: Input: [1, 5, 233, 7] Output: True Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. Note: 1 Any scores in the given array are non-negative integers and will not exceed 10,000,000. If the scores of both players are equal, then player 1 is still the winner.
class Solution: def PredictTheWinner(self, nums): _cache = {} return Solution.Moves(self, nums, 0, len(nums) - 1, 1, _cache) >= 0 def Moves(self, nums, ini, fin, pl, _cache={}): memo = str((ini, fin, pl)) if memo in list(_cache.keys()): return _cache[memo] if ini == fin: if pl == 1: _cache[memo] = nums[ini] return _cache[memo] else: _cache[memo] = -nums[ini] return _cache[memo] else: if pl == 1: _cache[memo] = max( Solution.Moves(self, nums, ini + 1, fin, 2, _cache) + nums[ini], Solution.Moves(self, nums, ini, fin - 1, 2, _cache) + nums[fin], ) return _cache[memo] if pl == 2: _cache[memo] = min( Solution.Moves(self, nums, ini + 1, fin, 1, _cache) - nums[ini], Solution.Moves(self, nums, ini, fin - 1, 1, _cache) - nums[fin], ) return _cache[memo]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER FUNC_DEF DICT ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR RETURN VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR RETURN VAR VAR
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. Example 1: Input: [1, 5, 2] Output: False Explanation: Initially, player 1 can choose between 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. Hence, player 1 will never be the winner and you need to return False. Example 2: Input: [1, 5, 233, 7] Output: True Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. Note: 1 Any scores in the given array are non-negative integers and will not exceed 10,000,000. If the scores of both players are equal, then player 1 is still the winner.
class Solution: def PredictTheWinner(self, nums): n = len(nums) if n == 0: return False dp = [([0] * n) for i in range(n)] for i in range(n): dp[i][i] = nums[i] for period in range(1, n): for i in range(n - period): j = i + period dp[i][j] = max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]) return dp[0][n - 1] >= 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. Example 1: Input: [1, 5, 2] Output: False Explanation: Initially, player 1 can choose between 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. Hence, player 1 will never be the winner and you need to return False. Example 2: Input: [1, 5, 233, 7] Output: True Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. Note: 1 Any scores in the given array are non-negative integers and will not exceed 10,000,000. If the scores of both players are equal, then player 1 is still the winner.
class Solution: def PredictTheWinner(self, nums): total = sum(nums) dic = {} def score(i, j, t): if i > j: return 0 if (i, j) in dic: return dic[i, j] ret = max( t - score(i + 1, j, t - nums[i]), t - score(i, j - 1, t - nums[j]) ) dic[i, j] = ret return ret l = len(nums) sc = score(0, l - 1, total) return sc >= total - sc
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR VAR
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins. Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score. Example 1: Input: [1, 5, 2] Output: False Explanation: Initially, player 1 can choose between 1 and 2. If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. Hence, player 1 will never be the winner and you need to return False. Example 2: Input: [1, 5, 233, 7] Output: True Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win. Note: 1 Any scores in the given array are non-negative integers and will not exceed 10,000,000. If the scores of both players are equal, then player 1 is still the winner.
class Solution: def PredictTheWinner(self, nums): dic = {} def solve(nums): if len(nums) <= 1: return sum(nums) tnums = tuple(nums) if tnums in dic: return dic[tnums] dic[tnums] = max(nums[0] - solve(nums[1:]), nums[-1] - solve(nums[:-1])) return dic[tnums] return solve(nums) >= 0
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: total = sum(balls) k = len(balls) def theSame(added): res = 0 for i in range(k): if added[i] == 0: res -= 1 elif balls[i] == added[i]: res += 1 return res == 0 def combination(this, pick): pick = min(this - pick, pick) res = 1 i = this j = 1 while i > pick: res *= i res /= j i -= 1 j += 1 return res def helper(i, added, cur): if cur == total // 2: if theSame(added): res = 1 for i in range(k): res *= combination(balls[i], added[i]) return res return 0 if i == k: return 0 if cur > total // 2: return 0 res = 0 for t in range(balls[i] + 1): added[i] = t res += helper(i + 1, added, cur + t) added[i] = 0 return res added = [0] * k return helper(0, added, 0) / combination(total, total // 2)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: self.total = 0 self.valid = 0 t = sum(balls) >> 1 n = len(balls) def check(s): return sum([(x != 0) for x in s]) == sum( [(balls[i] - s[i] != 0) for i in range(n)] ) fac = [1] for i in range(1, t + 1): fac.append(fac[-1] * i) def update(s): x = y = fac[-1] cnt1 = cnt2 = 0 for i, c in enumerate(s): x //= fac[c] y //= fac[balls[i] - c] cnt1 += c > 0 cnt2 += balls[i] - c > 0 ret = x * y self.total += ret if cnt1 == cnt2: self.valid += ret def dfs(state, i): s, cnt = state if cnt == t: update(s) return if i == n: return for x in range(balls[i] + 1): if cnt + x > t: break s[i] = x dfs((s, cnt + x), i + 1) s[i] = 0 s = [0] * n dfs((s, 0), 0) print(self.valid) print(self.total) return self.valid / self.total
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution(object): def __init__(self): self.fact = [1, 1, 2, 6, 24, 120, 720] self.total = 0.0 self.match = 0.0 def dfs(self, balls, i=0, n=0, c=0, w=1.0): if i == len(balls): self.total += w * (n == 0) self.match += w * (n == 0) * (c == 0) return for b1, b2 in zip(range(balls[i] + 1), reversed(range(balls[i] + 1))): self.dfs( balls, i + 1, n + b1 - b2, c + (b2 == 0) - (b1 == 0), w / self.fact[b1] / self.fact[b2], ) def getProbability(self, balls): self.dfs(balls) return self.match / self.total
CLASS_DEF VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF NUMBER NUMBER NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: def factorial(k): if k <= 1: return 1 return factorial(k - 1) * k def calc(balls): bs = [b for b in balls if b > 0] ans = factorial(sum(bs)) for b in bs: ans /= factorial(b) return ans sols = [] n = sum(balls) // 2 def generate(sol, s, i): nonlocal sols if s > n: return if i == len(balls): if s == n: sols += [sol] return for j in range(0, balls[i] + 1): generate(sol + [j], s + j, i + 1) generate([], 0, 0) count = 0 for sol in sols: l1 = sol l2 = [(y - x) for x, y in zip(l1, balls)] l1 = [num for num in l1 if num > 0] l2 = [num for num in l2 if num > 0] if len(l1) == len(l2): count += calc(l1) * calc(l2) return count / calc(balls)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR LIST VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: firstHalf, secondHalf = [(0) for _ in range(len(balls))], [ (0) for _ in range(len(balls)) ] self.good, self.all = 0, 0 mem_factorial = {} def factorial(v): if v not in mem_factorial: mem_factorial[v] = v * factorial(v - 1) if v != 0 else 1 return mem_factorial[v] def permutation(arr): prod = 1 for v in arr: prod *= factorial(v) return factorial(sum(arr)) / prod def dfs(i): if i == len(balls): if sum(firstHalf) != sum(secondHalf): return p1, p2 = permutation(firstHalf), permutation(secondHalf) self.all += p1 * p2 self.good += ( p1 * p2 if sum(v > 0 for v in firstHalf) == sum(v > 0 for v in secondHalf) else 0 ) else: for j in range(balls[i] + 1): firstHalf[i], secondHalf[i] = j, balls[i] - j dfs(i + 1) firstHalf[i], secondHalf[i] = 0, 0 dfs(0) return self.good / self.all
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: def calc(d): n = len(d) s = sum(d.values()) result = math.factorial(s) for k, v in list(d.items()): result /= math.factorial(v) return result t, e = 0, 0 def choose(i, k, d1, d2): nonlocal t nonlocal e if k == 0 and i <= n: for j in range(i, n): d2[j] = balls[j] t_ = calc(d1) * calc(d2) t += t_ e += t_ if len(d1) == len(d2) else 0 return if k < 0 or i == n: return for j in range(balls[i] + 1): if k - j < 0: break if j > 0: d1[i] = j if balls[i] - j > 0: d2[i] = balls[i] - j else: d2.pop(i) choose(i + 1, k - j, d1, d2) if i in d1: d1.pop(i) if i in d2: d2.pop(i) n = len(balls) k = sum(balls) // 2 choose(0, k, {}, {}) return e / t
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN IF VAR NUMBER VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR DICT DICT RETURN BIN_OP VAR VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: N = len(balls) sm = sum(balls) half = sm // 2 @lru_cache(None) def rec2(cur, na, nb): if na > half or nb > half: return 0 if cur == N: return int(na == nb) ans = 0 for i in range(balls[cur] + 1): remplacea, remplaceb = half - na, half - nb choice = math.comb(remplacea, i) * math.comb(remplaceb, balls[cur] - i) ans += rec2(cur + 1, na + i, nb + balls[cur] - i) * choice return ans @lru_cache(None) def rec(cur, na, nb, uniquea, uniqueb): if na > half or nb > half: return 0 if cur == N: if na != nb or uniquea != uniqueb: return 0 return 1 gg = 0 for i in range(balls[cur] + 1): toa, tob = na + i, nb + balls[cur] - i ua, ub = uniquea + int(i > 0), uniqueb + int(balls[cur] - i > 0) remplacea, remplaceb = half - na, half - nb choice = math.comb(remplacea, i) * math.comb(remplaceb, balls[cur] - i) gg += rec(cur + 1, toa, tob, ua, ub) * choice return gg gg = rec(0, 0, 0, 0, 0) permutation = math.factorial(sm) / math.factorial(half) al = rec2(0, 0, 0) return gg / al
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN BIN_OP VAR VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: balls_flattened = [] for i, count in enumerate(balls): balls_flattened.extend([i] * count) def has_same_distinct_color(perm): n = len(perm) return len(set(perm[: n // 2])) == len(set(perm[n // 2 :])) total = [0] same_distinct_color = [0] def get_perm(cur, balls): if not balls: total[0] += 1 same_distinct_color[0] += has_same_distinct_color(cur) return for i in range(len(balls)): if i > 0 and balls[i] == balls[i - 1]: continue get_perm(cur + [balls[i]], balls[:i] + balls[i + 1 :]) get_perm([], balls_flattened) return same_distinct_color[0] / total[0] def getProbability(self, balls: List[int]) -> float: factorial_cache = [1] for i in range(1, sum(balls) + 1): factorial_cache.append(factorial_cache[-1] * i) def get_perm_count(arr): total = factorial_cache[sum(arr)] for a in arr: total //= factorial_cache[a] return total total_perms = get_perm_count(balls) valid_perms = [0] def find_valid_split(n, start, distinct1, group1, distinct2, group2): if n == 0: if distinct1 == distinct2: valid_perms[0] += get_perm_count(group1) * get_perm_count(group2) return for i in range(start, len(group1)): if group1[i] == 0: continue new_num1 = group1[i] - 1 new_distinct1 = distinct1 if new_num1 > 0 else distinct1 - 1 new_num2 = group2[i] + 1 new_distinct2 = distinct2 if new_num2 > 1 else distinct2 + 1 if new_distinct1 < new_distinct2: continue new_group1 = group1[:i] + [new_num1] + group1[i + 1 :] new_group2 = group2[:i] + [new_num2] + group2[i + 1 :] find_valid_split( n - 1, i, new_distinct1, new_group1, new_distinct2, new_group2 ) find_valid_split(sum(balls) // 2, 0, len(balls), balls, 0, [0] * len(balls)) return valid_perms[0] / total_perms
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR VAR NUMBER NUMBER VAR NUMBER FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR RETURN BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NUMBER IF VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP LIST NUMBER FUNC_CALL VAR VAR RETURN BIN_OP VAR NUMBER VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: total = 0 valid = 0 @lru_cache(None) def getFactor(i): ans = 1 for i in range(2, i + 1): ans *= i return ans def getComb(nums): a = getFactor(sum(nums.values())) duplicate = 1 for val in nums.values(): duplicate *= getFactor(val) return a // duplicate def dfs(i, a, b): nonlocal total nonlocal valid if i == len(balls): if sum(a.values()) != sum(b.values()): return p1, p2 = getComb(a), getComb(b) total += p1 * p2 if len(a) == len(b): valid += p1 * p2 else: for j in range(balls[i] + 1): a[i] = j b[i] = balls[i] - j if a[i] == 0: del a[i] if b[i] == 0: del b[i] dfs(i + 1, a, b) dfs(0, {}, {}) return valid / total
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_CALL VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER DICT DICT RETURN BIN_OP VAR VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: @lru_cache(None) def select(n, r): if r > n: return 0 if r == 0: return 1 if r == 1: return n return select(n - 1, r) + select(n - 1, r - 1) @lru_cache(None) def dputil(i, j, pos, n): if pos == -1: if n == 0: return 1 else: return 0 p2 = 1 << pos if i & p2 and j & p2: ans = 0 for x in range(1, balls[pos]): diff = n - 2 * x + balls[pos] ans += dputil(i, j, pos - 1, diff) * select(balls[pos], x) return ans if i & p2: return dputil(i, j, pos - 1, n - balls[pos]) else: return dputil(i, j, pos - 1, n + balls[pos]) def numsplits(n): cnt = 0 while n: cnt += n % 2 n = n // 2 return cnt k = len(balls) tot = sum(balls) k2 = 1 << k valid = 0 for i in range(k2): for j in range(k2): if i | j != k2 - 1 or numsplits(i) != numsplits(j): continue valid += dputil(i, j, k - 1, 0) return float(valid) / float(select(tot, tot // 2))
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NONE FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR IF BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR NONE FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: n = sum(balls) k = len(balls) total = 0 valid = 0 fact = [1] * 50 for i in range(1, 50): fact[i] = fact[i - 1] * i def dfs(d, b1, b2, c1, c2, p1, p2): nonlocal total nonlocal valid if b1 > n // 2 or b2 > n // 2: return if d == k: count = fact[b1] / p1 * fact[b2] / p2 total += count valid += count * (c1 == c2) return for s1 in range(balls[d] + 1): s2 = balls[d] - s1 dfs( d + 1, b1 + s1, b2 + s2, c1 + (s1 > 0), c2 + (s2 > 0), p1 * fact[s1], p2 * fact[s2], ) dfs(0, 0, 0, 0, 0, 1, 1) return valid / total
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER RETURN BIN_OP VAR VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: total = sum(balls) def factorial(n): if n == 0: return 1 if n < 3: return n return n * factorial(n - 1) self.match = 0 self.total = 0 def helper(p, left1, left2, cnt1, cnt2, per1, per2): if left1 == 0 and left2 == 0: self.total += per1 * per2 self.match += per1 * per2 * (cnt1 == cnt2) elif left1 >= 0 and left2 >= 0: for k in range(balls[p] + 1): helper( p + 1, left1 - k, left2 - balls[p] + k, cnt1 + (k > 0), cnt2 + (balls[p] - k > 0), per1 / factorial(k), per2 / factorial(balls[p] - k), ) helper( 0, total // 2, total // 2, 0, 0, factorial(total // 2), factorial(total // 2), ) return self.match / self.total
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: firsthalf, secondhalf = [0] * len(balls), [0] * len(balls) self.good, self.all = 0, 0 @lru_cache(None) def fac(n): if n == 0: return 1 return n * fac(n - 1) def permutation(arr): prod = 1 for v in arr: prod *= fac(v) return fac(sum(arr)) / prod def dfs(i): if i == len(balls): if sum(firsthalf) != sum(secondhalf): return p1, p2 = permutation(firsthalf), permutation(secondhalf) self.all += p1 * p2 self.good += ( p1 * p2 if sum(v > 0 for v in firsthalf) == sum(v > 0 for v in secondhalf) else 0 ) else: for j in range(balls[i] + 1): firsthalf[i], secondhalf[i] = j, balls[i] - j dfs(i + 1) dfs(0) return self.good / self.all
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NONE FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: k, n = len(balls), sum(balls) total = 2 * math.comb(n, n // 2) def shuffle(color, box1, box2): if color == k: if sum(box1) == sum(box2) and box1.count(0) == box2.count(0): ans = 0 for box in [box1, box2]: p = 1 for c, num in enumerate(box): p *= math.comb(balls[c], num) ans += p return ans else: return 0 total_p = 0 bc = balls[color] for b in range(0, bc + 1): box1[color], box2[color] = b, bc - b total_p += shuffle(color + 1, box1, box2) box1[color], box2[color] = 0, 0 return total_p p = shuffle(0, [0] * k, [0] * k) return p / total
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR RETURN BIN_OP VAR VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: @lru_cache(None) def C(n, m): return math.factorial(n) / math.factorial(m) / math.factorial(n - m) def choose(i, k, d1, d2, cnt): if k == 0 and i <= n: return cnt, cnt if d1 == d2 + n - i else 0 if k < 0 or i == n: return 0, 0 total = 0 equal = 0 for j in range(balls[i] + 1): if k - j < 0: break t, e = choose( i + 1, k - j, d1 + (1 if j > 0 else 0), d2 + (1 if j < balls[i] else 0), cnt * C(balls[i], j), ) total += t equal += e return total, equal n = len(balls) k = sum(balls) t, e = choose(0, k // 2, 0, 0, 1) return e / t
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR NONE FUNC_DEF IF VAR NUMBER VAR VAR RETURN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER RETURN BIN_OP VAR VAR VAR
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: self.n = len(balls) self.mem = {(0): 1} self.mem2 = {} self.balls = balls rv = self.dfs(0, [], []) return rv / self.multinomial(balls) def dfs(self, idx, lefts, rights): if idx >= self.n: if not lefts or not rights: return 0 if len(lefts) != len(rights): return 0 if sum(lefts) != sum(rights): return 0 return self.multinomial(lefts) * self.multinomial(rights) rv = 0 for i in range(0, self.balls[idx] + 1): x1 = i x2 = self.balls[idx] - x1 if x1 == 0: rights.append(x2) rv += self.dfs(idx + 1, lefts, rights) rights.pop() elif x2 == 0: lefts.append(x1) rv += self.dfs(idx + 1, lefts, rights) lefts.pop() else: lefts.append(x1) rights.append(x2) rv += self.dfs(idx + 1, lefts, rights) rights.pop() lefts.pop() return rv def multinomial(self, arr): if not arr: return 0 arr = arr[:] arr.sort() key = tuple(arr) if key in self.mem2: return self.mem2[key] res = self.frac(sum(arr)) for x in arr: res //= self.frac(x) self.mem2[key] = res return res def frac(self, x): if x in self.mem: return self.mem[x] rv = x * self.frac(x - 1) self.mem[x] = rv return rv
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER LIST LIST RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR