description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Consider a sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$. Less formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers). Perform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \ldots, a_{2^k}])$ denote the number of candies we get in this process. For example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then: After the first operation the sequence becomes $[(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \ge 10$ and $9 + 4 \ge 10$. After the second operation the sequence becomes $[(5 + 4)\bmod 10, (7 + 3)\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \ge 10$. After the final operation sequence becomes $[(9 + 0) \bmod 10]$ $=$ $[9]$. Therefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total. You are given a sequence of digits of length $n$ $s_1, s_2, \ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the sequence. The second line contains $n$ digits $s_1, s_2, \ldots, s_n$ ($0 \le s_i \le 9$). The third line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries. Each of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \le l_i \le r_i \le n$) — $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$. -----Output----- Output $q$ lines, in $i$-th line output single integer — $f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])$, answer to the $i$-th query. -----Examples----- Input 8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7 Output 3 1 0 Input 6 0 1 2 3 3 5 3 1 2 1 4 3 6 Output 0 0 1 -----Note----- The first example illustrates an example from the statement. $f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \ge 10$ $\to$ $[(0 + 8) \bmod 10]$ $=$ $[8]$, so we get only $1$ candy. $f([9]) = 0$ as we don't perform operations with it.
import sys input = sys.stdin.readline N = int(input()) a = list(map(int, input().split())) d = {} def calc(l, r, x, y): if (l, r) in d: return d[l, r] elif l + 1 == r: d[l, r] = int(x + y >= 10), (x + y) % 10 return d[l, r] else: w, k1 = calc(l, (l + r) // 2, x, a[(l + r) // 2]) z, k2 = calc((l + r) // 2 + 1, r, a[(l + r) // 2 + 1], y) d[l, r] = w + z + int(k1 + k2 >= 10), (k1 + k2) % 10 return d[l, r] q = int(input()) for i in range(q): l, r = map(int, input().split()) l, r = l - 1, r - 1 if l == r: print(0) else: b, v = calc(l, r, a[l], a[r]) print(b)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider a sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$. Less formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers). Perform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \ldots, a_{2^k}])$ denote the number of candies we get in this process. For example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then: After the first operation the sequence becomes $[(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \ge 10$ and $9 + 4 \ge 10$. After the second operation the sequence becomes $[(5 + 4)\bmod 10, (7 + 3)\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \ge 10$. After the final operation sequence becomes $[(9 + 0) \bmod 10]$ $=$ $[9]$. Therefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total. You are given a sequence of digits of length $n$ $s_1, s_2, \ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the sequence. The second line contains $n$ digits $s_1, s_2, \ldots, s_n$ ($0 \le s_i \le 9$). The third line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries. Each of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \le l_i \le r_i \le n$) — $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$. -----Output----- Output $q$ lines, in $i$-th line output single integer — $f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])$, answer to the $i$-th query. -----Examples----- Input 8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7 Output 3 1 0 Input 6 0 1 2 3 3 5 3 1 2 1 4 3 6 Output 0 0 1 -----Note----- The first example illustrates an example from the statement. $f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \ge 10$ $\to$ $[(0 + 8) \bmod 10]$ $=$ $[8]$, so we get only $1$ candy. $f([9]) = 0$ as we don't perform operations with it.
n = int(input()) a = tuple(map(int, input().split())) block_size = int(pow(n, 0.5)) b = [(0) for i in range(n // block_size + 2)] for i in range(0, n, block_size): b[i // block_size] = sum(a[i : i + block_size]) q = int(input()) for _ in range(q): l, r = map(lambda x: int(x) - 1, input().split()) l1, l2 = l // block_size, r // block_size s = sum(b[l1:l2]) - sum(a[l1 * block_size : l]) + sum(a[l2 * block_size : r + 1]) print(s // 10)
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 FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Consider a sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$. Less formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers). Perform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \ldots, a_{2^k}])$ denote the number of candies we get in this process. For example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then: After the first operation the sequence becomes $[(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \ge 10$ and $9 + 4 \ge 10$. After the second operation the sequence becomes $[(5 + 4)\bmod 10, (7 + 3)\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \ge 10$. After the final operation sequence becomes $[(9 + 0) \bmod 10]$ $=$ $[9]$. Therefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total. You are given a sequence of digits of length $n$ $s_1, s_2, \ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the sequence. The second line contains $n$ digits $s_1, s_2, \ldots, s_n$ ($0 \le s_i \le 9$). The third line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries. Each of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \le l_i \le r_i \le n$) — $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$. -----Output----- Output $q$ lines, in $i$-th line output single integer — $f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])$, answer to the $i$-th query. -----Examples----- Input 8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7 Output 3 1 0 Input 6 0 1 2 3 3 5 3 1 2 1 4 3 6 Output 0 0 1 -----Note----- The first example illustrates an example from the statement. $f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \ge 10$ $\to$ $[(0 + 8) \bmod 10]$ $=$ $[8]$, so we get only $1$ candy. $f([9]) = 0$ as we don't perform operations with it.
N = int(input()) s = [int(i) for i in input().split()] for i in range(1, N): s[i] = s[i - 1] + s[i] q = int(input()) for i in range(q): l, r = input().split() l = int(l) r = int(r) if l == 1: print(s[r - 1] // 10) else: print((s[r - 1] - s[l - 2]) // 10)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER
Consider a sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$. Less formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers). Perform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \ldots, a_{2^k}])$ denote the number of candies we get in this process. For example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then: After the first operation the sequence becomes $[(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \ge 10$ and $9 + 4 \ge 10$. After the second operation the sequence becomes $[(5 + 4)\bmod 10, (7 + 3)\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \ge 10$. After the final operation sequence becomes $[(9 + 0) \bmod 10]$ $=$ $[9]$. Therefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total. You are given a sequence of digits of length $n$ $s_1, s_2, \ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the sequence. The second line contains $n$ digits $s_1, s_2, \ldots, s_n$ ($0 \le s_i \le 9$). The third line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries. Each of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \le l_i \le r_i \le n$) — $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$. -----Output----- Output $q$ lines, in $i$-th line output single integer — $f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])$, answer to the $i$-th query. -----Examples----- Input 8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7 Output 3 1 0 Input 6 0 1 2 3 3 5 3 1 2 1 4 3 6 Output 0 0 1 -----Note----- The first example illustrates an example from the statement. $f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \ge 10$ $\to$ $[(0 + 8) \bmod 10]$ $=$ $[8]$, so we get only $1$ candy. $f([9]) = 0$ as we don't perform operations with it.
n = int(input()) lt = [int(x) for x in input().split()] pref = [0] * n pref[0] = lt[0] for i in range(1, len(lt)): pref[i] = pref[i - 1] + lt[i] q = int(input()) while q != 0: q -= 1 l, r = map(int, input().split()) l -= 1 r -= 1 sm = 0 if l == 0: sm = pref[r] else: sm = pref[r] - pref[l - 1] print(sm // 10)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Consider a sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$. Less formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers). Perform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \ldots, a_{2^k}])$ denote the number of candies we get in this process. For example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then: After the first operation the sequence becomes $[(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \ge 10$ and $9 + 4 \ge 10$. After the second operation the sequence becomes $[(5 + 4)\bmod 10, (7 + 3)\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \ge 10$. After the final operation sequence becomes $[(9 + 0) \bmod 10]$ $=$ $[9]$. Therefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total. You are given a sequence of digits of length $n$ $s_1, s_2, \ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the sequence. The second line contains $n$ digits $s_1, s_2, \ldots, s_n$ ($0 \le s_i \le 9$). The third line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries. Each of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \le l_i \le r_i \le n$) — $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$. -----Output----- Output $q$ lines, in $i$-th line output single integer — $f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])$, answer to the $i$-th query. -----Examples----- Input 8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7 Output 3 1 0 Input 6 0 1 2 3 3 5 3 1 2 1 4 3 6 Output 0 0 1 -----Note----- The first example illustrates an example from the statement. $f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \ge 10$ $\to$ $[(0 + 8) \bmod 10]$ $=$ $[8]$, so we get only $1$ candy. $f([9]) = 0$ as we don't perform operations with it.
n = int(input()) a = list(map(int, input().split())) ps = [] sum = 0 for i in range(0, n): sum += a[i] ps.append(sum) q = int(input()) while q > 0: l, r = map(int, input().split()) sum = 0 if l == 1: sum = ps[r - 1] else: sum = ps[r - 1] - ps[l - 2] print(int(sum / 10)) q -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER
Consider a sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$. Less formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers). Perform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \ldots, a_{2^k}])$ denote the number of candies we get in this process. For example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then: After the first operation the sequence becomes $[(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \ge 10$ and $9 + 4 \ge 10$. After the second operation the sequence becomes $[(5 + 4)\bmod 10, (7 + 3)\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \ge 10$. After the final operation sequence becomes $[(9 + 0) \bmod 10]$ $=$ $[9]$. Therefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total. You are given a sequence of digits of length $n$ $s_1, s_2, \ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the sequence. The second line contains $n$ digits $s_1, s_2, \ldots, s_n$ ($0 \le s_i \le 9$). The third line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries. Each of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \le l_i \le r_i \le n$) — $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$. -----Output----- Output $q$ lines, in $i$-th line output single integer — $f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])$, answer to the $i$-th query. -----Examples----- Input 8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7 Output 3 1 0 Input 6 0 1 2 3 3 5 3 1 2 1 4 3 6 Output 0 0 1 -----Note----- The first example illustrates an example from the statement. $f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \ge 10$ $\to$ $[(0 + 8) \bmod 10]$ $=$ $[8]$, so we get only $1$ candy. $f([9]) = 0$ as we don't perform operations with it.
ara = [] n = int(input()) sum = 0 bar = 0 ind = 1 a = input() ara.append(0) for i in a.split(): bar += int(i) ara.append(bar) q = int(input()) while q >= 1: start, End = input().split() start = int(start) End = int(End) sum = ara[End] - ara[start - 1] print(int(sum / 10)) q -= 1
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER
Consider a sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$. Less formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers). Perform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \ldots, a_{2^k}])$ denote the number of candies we get in this process. For example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then: After the first operation the sequence becomes $[(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \ge 10$ and $9 + 4 \ge 10$. After the second operation the sequence becomes $[(5 + 4)\bmod 10, (7 + 3)\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \ge 10$. After the final operation sequence becomes $[(9 + 0) \bmod 10]$ $=$ $[9]$. Therefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total. You are given a sequence of digits of length $n$ $s_1, s_2, \ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the sequence. The second line contains $n$ digits $s_1, s_2, \ldots, s_n$ ($0 \le s_i \le 9$). The third line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries. Each of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \le l_i \le r_i \le n$) — $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$. -----Output----- Output $q$ lines, in $i$-th line output single integer — $f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])$, answer to the $i$-th query. -----Examples----- Input 8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7 Output 3 1 0 Input 6 0 1 2 3 3 5 3 1 2 1 4 3 6 Output 0 0 1 -----Note----- The first example illustrates an example from the statement. $f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \ge 10$ $\to$ $[(0 + 8) \bmod 10]$ $=$ $[8]$, so we get only $1$ candy. $f([9]) = 0$ as we don't perform operations with it.
N = 100000.0 n = 0 t = [0] * int(2 * N) def build(): i = n - 1 while i > 0: t[i] = t[i << 1] + t[i << 1 | 1] i -= 1 def modify(p, value): p += n t[p] = value while p > 1: t[p >> 1] = t[p] + t[p ^ 1] p >>= 1 def query(l, r): res = 0 l += n r += n while l < r: if l & 1: res += t[l] l += 1 if r & 1: r -= 1 res += t[r] l >>= 1 r >>= 1 return res n = int(input()) x = [int(i) for i in input().split()] k = 0 for i in x: t[n + k] = i k += 1 build() for i in range(int(input())): candies = 0 x, y = input().split() x, y = int(x) - 1, int(y) - 1 h = 0 v2 = [] l = query(x, y + 1) v = l - l % 10 candies = v // 10 print(candies)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider a sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$. Less formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers). Perform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \ldots, a_{2^k}])$ denote the number of candies we get in this process. For example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then: After the first operation the sequence becomes $[(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \ge 10$ and $9 + 4 \ge 10$. After the second operation the sequence becomes $[(5 + 4)\bmod 10, (7 + 3)\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \ge 10$. After the final operation sequence becomes $[(9 + 0) \bmod 10]$ $=$ $[9]$. Therefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total. You are given a sequence of digits of length $n$ $s_1, s_2, \ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the sequence. The second line contains $n$ digits $s_1, s_2, \ldots, s_n$ ($0 \le s_i \le 9$). The third line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries. Each of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \le l_i \le r_i \le n$) — $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$. -----Output----- Output $q$ lines, in $i$-th line output single integer — $f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])$, answer to the $i$-th query. -----Examples----- Input 8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7 Output 3 1 0 Input 6 0 1 2 3 3 5 3 1 2 1 4 3 6 Output 0 0 1 -----Note----- The first example illustrates an example from the statement. $f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \ge 10$ $\to$ $[(0 + 8) \bmod 10]$ $=$ $[8]$, so we get only $1$ candy. $f([9]) = 0$ as we don't perform operations with it.
VALUE = 0 CANDIES = 1 n = int(input()) a = [int(i) for i in input().split(" ")] pref = [0] for i in range(n): pref.append(pref[-1] + a[i]) q = int(input()) o = "" for i in range(q): l, r = map(int, input().split(" ")) o += f"{(pref[r] - pref[l - 1]) // 10}\n" print(o)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR VAR
Consider a sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$. Less formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers). Perform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \ldots, a_{2^k}])$ denote the number of candies we get in this process. For example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then: After the first operation the sequence becomes $[(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \ge 10$ and $9 + 4 \ge 10$. After the second operation the sequence becomes $[(5 + 4)\bmod 10, (7 + 3)\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \ge 10$. After the final operation sequence becomes $[(9 + 0) \bmod 10]$ $=$ $[9]$. Therefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total. You are given a sequence of digits of length $n$ $s_1, s_2, \ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the sequence. The second line contains $n$ digits $s_1, s_2, \ldots, s_n$ ($0 \le s_i \le 9$). The third line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries. Each of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \le l_i \le r_i \le n$) — $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$. -----Output----- Output $q$ lines, in $i$-th line output single integer — $f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])$, answer to the $i$-th query. -----Examples----- Input 8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7 Output 3 1 0 Input 6 0 1 2 3 3 5 3 1 2 1 4 3 6 Output 0 0 1 -----Note----- The first example illustrates an example from the statement. $f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \ge 10$ $\to$ $[(0 + 8) \bmod 10]$ $=$ $[8]$, so we get only $1$ candy. $f([9]) = 0$ as we don't perform operations with it.
n = int(input()) p = input().rstrip().split(" ") l = [] S = 0 for i in range(0, len(p)): if i == 0: l.append(int(p[i])) else: l.append(int(p[i]) + int(l[i - 1])) o = int(input()) for i in range(0, o): u = input().rstrip().split(" ") if int(u[0]) == int(u[1]): print(0) elif int(u[0]) == 1: print(l[int(u[1]) - 1] // 10) else: T = l[int(u[1]) - 1] - l[int(u[0]) - 2] print(T // 10)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Consider a sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$. Less formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers). Perform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \ldots, a_{2^k}])$ denote the number of candies we get in this process. For example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then: After the first operation the sequence becomes $[(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \ge 10$ and $9 + 4 \ge 10$. After the second operation the sequence becomes $[(5 + 4)\bmod 10, (7 + 3)\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \ge 10$. After the final operation sequence becomes $[(9 + 0) \bmod 10]$ $=$ $[9]$. Therefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total. You are given a sequence of digits of length $n$ $s_1, s_2, \ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the sequence. The second line contains $n$ digits $s_1, s_2, \ldots, s_n$ ($0 \le s_i \le 9$). The third line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries. Each of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \le l_i \le r_i \le n$) — $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$. -----Output----- Output $q$ lines, in $i$-th line output single integer — $f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])$, answer to the $i$-th query. -----Examples----- Input 8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7 Output 3 1 0 Input 6 0 1 2 3 3 5 3 1 2 1 4 3 6 Output 0 0 1 -----Note----- The first example illustrates an example from the statement. $f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \ge 10$ $\to$ $[(0 + 8) \bmod 10]$ $=$ $[8]$, so we get only $1$ candy. $f([9]) = 0$ as we don't perform operations with it.
from sys import stdin, stdout def main(): n = int(stdin.readline()) arr = [int(i) for i in stdin.readline().split()] st = [0] * (n + 1) st[0] = arr[0] for i in range(1, n): st[i] = st[i - 1] + arr[i] q = int(stdin.readline()) for i in range(q): a, b = stdin.readline().split() if int(a) == 1: stdout.write(str(int(st[int(b) - 1] / 10))) stdout.write("\n") else: stdout.write(str(int((st[int(b) - 1] - st[int(a) - 2]) / 10))) stdout.write("\n") main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, N, Sum): T = [[(0) for i in range(Sum + 1)] for i in range(N + 1)] for i in range(N + 1): for j in range(Sum + 1): if i == 0: T[i][j] = 0 if j == 0 and i != 0: T[i][j] = 1 elif coins[i - 1] <= j: T[i][j] = T[i][j - coins[i - 1]] + T[i - 1][j] else: T[i][j] = T[i - 1][j] return T[N][Sum]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, N, Sum): d = {} def f(n, s=Sum): k = n, s if n == 0: if s % coins[n] == 0: return 1 else: return 0 if k in d: return d[k] else: nt = f(n - 1, s) t = 0 if s >= coins[n]: t = f(n, s - coins[n]) d[k] = t + nt return d[k] return f(N - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, N, Sum): table = [[(0) for _ in range(Sum + 1)] for _ in range(N)] for k in range(N): table[k][0] = 1 for i in range(N): for j in range(Sum + 1): if i == 0 and j == 0: table[i][j] = 1 if coins[i] > j: table[i][j] = table[i - 1][j] else: table[i][j] = table[i - 1][j] + table[i][j - coins[i]] return table[-1][-1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR NUMBER NUMBER
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, N, Sum): dp = [[(1) for i in range(Sum + 1)] for j in range(N + 1)] for i in range(1, Sum + 1): dp[0][i] = 0 for i in range(1, N + 1): for j in range(Sum + 1): if coins[i - 1] <= j: dp[i][j] = dp[i][j - coins[i - 1]] + dp[i - 1][j] else: dp[i][j] = dp[i - 1][j] return dp[i][j]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
import sys class Solution: def count(self, coins, n, sum_): import sys sys.setrecursionlimit(10**9 + 7) dp = [[(-1) for j in range(sum_ + 1)] for i in range(n)] return self.solve(n - 1, sum_, coins, dp) def solve(self, n, ts, coins, dp): if n == 0: if ts % coins[n] == 0: dp[n][ts] = 1 return 1 else: dp[n][ts] = 0 return 0 if dp[n][ts] != -1: return dp[n][ts] pick = 0 npick = self.solve(n - 1, ts, coins, dp) if coins[n] <= ts: pick = self.solve(n, ts - coins[n], coins, dp) dp[n][ts] = pick + npick return dp[n][ts]
IMPORT CLASS_DEF FUNC_DEF IMPORT EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF IF VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, n, sum_): dp = [[(0) for j in range(sum_ + 1)] for i in range(n)] for j in range(sum_ + 1): if j % coins[0] == 0: dp[0][j] = 1 for i in range(1, n): for j in range(sum_ + 1): npick = dp[i - 1][j] pick = 0 if coins[i] <= j: pick = dp[i][j - coins[i]] dp[i][j] = pick + npick return dp[n - 1][sum_]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, N, Sum): coins.sort() dp = [([0] * (Sum + 1)) for _ in range(N + 1)] for r in range(N + 1): dp[r][0] = 1 for r in range(1, N + 1): for c in range(Sum + 1): if coins[r - 1] <= c: dp[r][c] = dp[r - 1][c] + dp[r][c - coins[r - 1]] else: dp[r][c] = dp[r - 1][c] return dp[-1][-1]
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, N, Sum): dp = [0] * (Sum + 1) dp[0] = 1 for j in range(len(coins)): for i in range(coins[j], Sum + 1): dp[i] += dp[i - coins[j]] return dp[sum]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def solution(self, ind, arr, T, dp): if ind == 0: return T % arr[ind] == 0 if dp[ind][T] != -1: return dp[ind][T] notPick = 0 + self.solution(ind - 1, arr, T, dp) pick = 0 if T >= arr[ind]: pick = self.solution(ind, arr, T - arr[ind], dp) dp[ind][T] = pick + notPick return dp[ind][T] def count(self, coins, N, Sum): dp = [([-1] * (Sum + 1)) for rows in range(N + 1)] return self.solution(N - 1, coins, Sum, dp)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, N, Sum): dp = [([0] * (Sum + 1)) for _ in range(N + 1)] for i in range(N + 1): for j in range(Sum + 1): if j == 0: dp[i][j] = 1 elif i == 0: dp[i][j] == 0 else: val = coins[i - 1] if val <= j: c1 = dp[i][j - val] c2 = dp[i - 1][j] dp[i][j] = c1 + c2 else: dp[i][j] = dp[i - 1][j] return dp[N][Sum]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, N, Sum): return self.bottom_up_count(coins, N, Sum) def bottom_up_count(self, coins, N, Sum): dp = [([0] * N) for _ in range(Sum + 1)] for i in range(N): dp[0][i] = 1 for i in range(1, Sum + 1): for j in range(N): x = dp[i - coins[j]][j] if i - coins[j] >= 0 else 0 y = dp[i][j - 1] if j - 1 >= 0 else 0 dp[i][j] = x + y return dp[Sum][N - 1]
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, N, Sum): t = [[(-1) for i in range(Sum + 1)] for j in range(N + 1)] def f(coins, n, s, t): if n == 0 and s != 0: return 0 if s == 0: return 1 if t[n][s] != -1: return t[n][s] if coins[n - 1] > s: t[n][s] = f(coins, n - 1, s, t) else: t[n][s] = f(coins, n, s - coins[n - 1], t) + f(coins, n - 1, s, t) return t[n][s] return f(coins, N, Sum, t)
CLASS_DEF FUNC_DEF ASSIGN 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 RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, N, Sum): h = {(0): 1} if Sum == 0: return 1 if not coins: return 0 for coin in coins: for num in range(1, Sum + 1): if num - coin in h: ways = h[num - coin] else: ways = 0 h[num] = h[num] + ways if num in h else ways return h[Sum]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR RETURN NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, n, sum): dp = [[(-1) for i in range(sum + 1)] for j in range(n + 1)] def memo(i, target): if target == 0: return 1 if i < 0: return 0 if dp[i][target] != -1: return dp[i][target] if coins[i] <= target: dp[i][target] = memo(i, target - coins[i]) + memo(i - 1, target) else: dp[i][target] = memo(i - 1, target) return dp[i][target] return memo(n - 1, sum)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, a, N, t): dp = [(0) for i in range(t + 1)] temp = [(0) for i in range(t + 1)] for i in range(t + 1): if i % a[0] == 0: dp[i] = 1 for i in range(1, len(a)): for j in range(t + 1): x = dp[j] y = 0 if a[i] <= j: y = temp[j - a[i]] temp[j] = x + y dp = temp return dp[t]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def count(self, coins, N, Sum): table = [(0) for k in range(sum + 1)] table[0] = 1 for i in range(0, N): for j in range(coins[i], sum + 1): table[j] += table[j - coins[i]] return table[Sum]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def answer(self, coins, i, sum, dp): if sum == 0: return 1 if i == -1: return 0 if dp[i][sum] != -1: return dp[i][sum] pick = 0 if sum >= coins[i]: pick = self.answer(coins, i, sum - coins[i], dp) notpick = self.answer(coins, i - 1, sum, dp) dp[i][sum] = pick + notpick return dp[i][sum] def count(self, coins, N, Sum): dp = [[(-1) for i in range(Sum + 1)] for j in range(N)] return self.answer(coins, N - 1, Sum, dp)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def solve(self, idx, coins, N, target): if target == 0: return 1 if target < 0 or idx >= N: return 0 incl = 0 if coins[idx] <= target: incl = self.solve(idx, coins, N, target - coins[idx]) excl = self.solve(idx + 1, coins, N, target) return incl + excl def solve_memo(self, idx, coins, N, target, F): if target == 0: return 1 if target < 0 or idx >= N: return 0 if F[idx][target] != -1: return F[idx][target] incl = 0 if coins[idx] <= target: incl = self.solve_memo(idx, coins, N, target - coins[idx], F) excl = self.solve_memo(idx + 1, coins, N, target, F) F[idx][target] = incl + excl return F[idx][target] def solve_tab(self, coins, N, Sum): F = [[(0) for _ in range(Sum + 1)] for _ in range(N + 1)] for i in range(N + 1): F[i][0] = 1 for idx in range(N - 1, -1, -1): for target in range(1, Sum + 1): incl = 0 if target - coins[idx] >= 0: incl = F[idx][target - coins[idx]] excl = F[idx + 1][target] F[idx][target] = incl + excl return F[0][Sum] def solve_opt(self, coins, N, Sum): curr = [(0) for _ in range(Sum + 1)] nxt = [(0) for _ in range(Sum + 1)] for idx in range(N - 1, -1, -1): curr[0] = 1 for target in range(1, Sum + 1): incl = 0 if target - coins[idx] >= 0: incl = curr[target - coins[idx]] excl = nxt[target] curr[target] = incl + excl nxt = curr[:] return nxt[Sum] def count(self, coins, N, Sum): return self.solve_opt(coins, N, Sum)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR
Given an integer array coins[ ] of size N representing different denominations of currency and an integer sum, find the number of ways you can make sum by using different combinations from coins[ ]. Note: Assume that you have an infinite supply of each type of coin. Example 1: Input: sum = 4 , N = 3 coins[] = {1,2,3} Output: 4 Explanation: Four Possible ways are: {1,1,1,1},{1,1,2},{2,2},{1,3}. Example 2: Input: Sum = 10 , N = 4 coins[] ={2,5,3,6} Output: 5 Explanation: Five Possible ways are: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which accepts an array coins[ ] its size N and sum as input parameters and returns the number of ways to make change for given sum of money. Expected Time Complexity: O(sum*N) Expected Auxiliary Space: O(sum) Constraints: 1 <= sum, N <= 10^{3}
class Solution: def solve(coins, N, Sum, dp): if N == 0: return 0 if Sum == 0: return 1 if dp[N][Sum] != -1: return dp[N][Sum] if coins[N - 1] > Sum: dp[N][Sum] = Solution.solve(coins, N - 1, Sum, dp) return dp[N][Sum] dp[N][Sum] = Solution.solve(coins, N - 1, Sum, dp) + Solution.solve( coins, N, Sum - coins[N - 1], dp ) return dp[N][Sum] def count(self, coins, N, Sum): dp = [[(-1) for _ in range(Sum + 1)] for _ in range(N + 1)] return Solution.solve(coins, N, Sum, dp)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given two integers N and K, the task is to find the count of palindromic strings of length lesser than or equal to N, with first K characters of lowercase English language, such that each character in a string doesn’t appear more than twice. Note: Anwer can be very large, so, output answer modulo 10^{9}+7 Example 1: Input: N = 3, K = 2 Output: 6 Explanation: The possible strings are: "a", "b", "aa", "bb", "aba", "bab". Example 2: Input: N = 4, K = 3 Output: 18 Explanation: The possible strings are: "a", "b", "c", "aa", "bb", "cc", "aba", "aca", "bab", "bcb", "cac", "cbc", "abba", "acca", "baab", "bccb", "caac", "cbbc". Your task: You do not need to read any input or print anything. The task is to complete the function palindromicStrings(), which takes two integers as input and returns the count. Expected Time Complexity: O(K^{2}) Expected Auxiliary Space: O(K^{2}) Constraints: 1 ≤ K ≤ 26 1 ≤ N ≤ 52 N ≤ 2*K
class Solution: def palindromicStrings(self, N, K): if N <= 1: return K dp = [(0) for _ in range(N + 1)] mod = 10**9 + 7 dp[0] = 0 dp[1] = K dp[2] = K for i in range(3, N + 1): p = (i - 1) // 2 dp[i] = dp[i - 2] * (k - p) % mod Sum = 0 for i in range(1, N + 1): Sum += dp[i] % mod return Sum % mod
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR
Given two integers N and K, the task is to find the count of palindromic strings of length lesser than or equal to N, with first K characters of lowercase English language, such that each character in a string doesn’t appear more than twice. Note: Anwer can be very large, so, output answer modulo 10^{9}+7 Example 1: Input: N = 3, K = 2 Output: 6 Explanation: The possible strings are: "a", "b", "aa", "bb", "aba", "bab". Example 2: Input: N = 4, K = 3 Output: 18 Explanation: The possible strings are: "a", "b", "c", "aa", "bb", "cc", "aba", "aca", "bab", "bcb", "cac", "cbc", "abba", "acca", "baab", "bccb", "caac", "cbbc". Your task: You do not need to read any input or print anything. The task is to complete the function palindromicStrings(), which takes two integers as input and returns the count. Expected Time Complexity: O(K^{2}) Expected Auxiliary Space: O(K^{2}) Constraints: 1 ≤ K ≤ 26 1 ≤ N ≤ 52 N ≤ 2*K
def rec(N, K, dp): if N == 0: return 1 if K == 0: return 0 if N == 1: return K dp[N][K] = rec(N - 2, K - 1, dp) * K return dp[N][K] class Solution: def palindromicStrings(self, N, K): mod = int(1000000000.0 + 7) dp = [[(0) for i in range(K + 1)] for j in range(N + 1)] c = 0 for i in range(1, N + 1): c += rec(i, K, dp) return c % mod
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR
Given two integers N and K, the task is to find the count of palindromic strings of length lesser than or equal to N, with first K characters of lowercase English language, such that each character in a string doesn’t appear more than twice. Note: Anwer can be very large, so, output answer modulo 10^{9}+7 Example 1: Input: N = 3, K = 2 Output: 6 Explanation: The possible strings are: "a", "b", "aa", "bb", "aba", "bab". Example 2: Input: N = 4, K = 3 Output: 18 Explanation: The possible strings are: "a", "b", "c", "aa", "bb", "cc", "aba", "aca", "bab", "bcb", "cac", "cbc", "abba", "acca", "baab", "bccb", "caac", "cbbc". Your task: You do not need to read any input or print anything. The task is to complete the function palindromicStrings(), which takes two integers as input and returns the count. Expected Time Complexity: O(K^{2}) Expected Auxiliary Space: O(K^{2}) Constraints: 1 ≤ K ≤ 26 1 ≤ N ≤ 52 N ≤ 2*K
class Solution: def palindromicStrings(self, n, k): if n < 3: return k * n dp = [(0) for i in range(n + 1)] dp[1] = dp[2] = k s = 2 * k for i in range(3, n + 1): v = (i - 1) // 2 dp[i] = dp[i - 2] * (k - v) % 1000000007 s += dp[i] % 1000000007 return s % 1000000007
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER
Given two integers N and K, the task is to find the count of palindromic strings of length lesser than or equal to N, with first K characters of lowercase English language, such that each character in a string doesn’t appear more than twice. Note: Anwer can be very large, so, output answer modulo 10^{9}+7 Example 1: Input: N = 3, K = 2 Output: 6 Explanation: The possible strings are: "a", "b", "aa", "bb", "aba", "bab". Example 2: Input: N = 4, K = 3 Output: 18 Explanation: The possible strings are: "a", "b", "c", "aa", "bb", "cc", "aba", "aca", "bab", "bcb", "cac", "cbc", "abba", "acca", "baab", "bccb", "caac", "cbbc". Your task: You do not need to read any input or print anything. The task is to complete the function palindromicStrings(), which takes two integers as input and returns the count. Expected Time Complexity: O(K^{2}) Expected Auxiliary Space: O(K^{2}) Constraints: 1 ≤ K ≤ 26 1 ≤ N ≤ 52 N ≤ 2*K
class Solution: def palindromicStrings(self, N, K): mod = 10**9 + 7 sumi = 0 t = 1 for i in range(1, N + 1): t = 1 if i % 2 == 0: t2 = i // 2 else: t2 = i // 2 + 1 for j in range(t2): t *= K - j t %= mod sumi += t sumi %= mod return sumi
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
from sys import maxsize, stdin, stdout def solve(): pass test = 1 test = int(input()) for t in range(0, test): n, m = list(map(int, input().split())) brr = [list(map(int, input().split())) for i in range(n)] drr = [list(map(int, input().split())) for i in range(n)] dp = [([0] * m) for j in range(n)] dp[0] = brr[0] for i in range(1, n): v1 = min(dp[i - 1]) ind = dp[i - 1].index(v1) for j in range(m): if j != ind: dp[i][j] = min( v1 + brr[i][j], dp[i - 1][j] + max(0, brr[i][j] - drr[i - 1][j]) ) else: dp[i][j] = dp[i - 1][j] + max(0, brr[i][j] - drr[i - 1][j]) print(min(dp[n - 1]))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
t = int(input()) for T in range(t): n, m = [int(x) for x in input().split()] cost, disc, dp = [], [], [] for i in range(n): cost.append([int(x) for x in input().split()]) for i in range(n): disc.append([int(x) for x in input().split()]) val = 0 dp = [[(0) for j in range(m)] for i in range(n)] for i in range(m): dp[0][i] = cost[0][i] for i in range(1, n): val = min(dp[i - 1]) for j in range(m): v1 = max(0, cost[i][j] - disc[i - 1][j]) dp[i][j] = min(dp[i - 1][j] + v1, val + cost[i][j]) print(min(dp[n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
mod = 1000000007 inf = mod def solve(n, m, dp, discounts, prices): prev = inf * inf for i in range(m): dp[0][i] = prices[0][i] prev = min(prev, dp[0][i]) for i in range(1, n): prev2 = inf * inf for j in range(m): d = max(0, prices[i][j] - discounts[i - 1][j]) dp[i][j] = dp[i - 1][j] + d dp[i][j] = min(dp[i][j], prev + prices[i][j]) prev2 = min(prev2, dp[i][j]) prev = prev2 return min(dp[n - 1]) tests = int(input()) for _ in range(tests): n, m = map(int, input().split()) prices = [] dp = [] discounts = [] for i in range(n): temp = list(map(int, input().split())) temp2 = [0] * m dp.append(temp2) prices.append(temp) for i in range(n): temp = list(map(int, input().split())) discounts.append(temp) print(solve(n, m, dp, discounts, prices))
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
t = int(input()) for _ in range(t): n, m = list(map(int, input().split(" "))) cst = list() for _ in range(n): cst.append(list(map(int, input().split(" ")))) dsc = list() for _ in range(n): dsc.append(list(map(int, input().split(" ")))) dp = list() dp.append(cst[0][:]) for _ in range(n - 1): dp.append([(0) for x in range(m)]) for i in range(1, n): mn = min(dp[i - 1][:]) for j in range(m): h = cst[i][j] - dsc[i - 1][j] if h < 0: h = 0 dp[i][j] = min(mn + cst[i][j], dp[i - 1][j] + h) print(min(dp[n - 1][:]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
t = int(input()) for _ in range(t): n, m = map(int, input().split()) cost = [] for i in range(n): e = list(map(int, input().split())) cost.append(e) disc = [] for i in range(n): e = list(map(int, input().split())) disc.append(e) dp = [([0] * m) for i in range(n)] curmin = float("inf") for j in range(m): dp[0][j] = cost[0][j] curmin = min(curmin, dp[0][j]) for i in range(1, n): for j in range(m): dp[i][j] = min( curmin + cost[i][j], dp[i - 1][j] + max(0, cost[i][j] - disc[i - 1][j]) ) curmin = float("inf") for j in range(m): curmin = min(dp[i][j], curmin) print(curmin)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
t = int(input()) for i in range(t): n, m = map(int, input().split()) prices = [] for j in range(n): arr = [int(k) for k in input().split()] prices += [arr] discounts = [] for j in range(n): arr = [int(k) for k in input().split()] discounts += [arr] dp = [[([-1] * 2) for j in range(m)] for j in range(n)] mi = 10**10 prevmi = 10**10 prevprevmi = 10**10 for x in range(m): for z in range(2): dp[0][x][z] = prices[0][x] if prices[0][x] <= mi: prevprevmi = prevmi prevmi = mi mi = prices[0][x] elif prices[0][x] > mi and prices[0][x] <= prevmi: prevprevmi = prevmi prevmi = prices[0][x] elif prices[0][x] > prevmi and prices[0][x] <= prevprevmi: prevprevmi = prices[0][x] for x in range(1, n): mi2 = 10**15 prevmi2 = 10**15 prevprevmi2 = 10**15 for y in range(m): discprice = prices[x][y] - discounts[x - 1][y] if discprice < 0: discprice = 0 dp[x][y][1] = min(dp[x - 1][y][0], dp[x - 1][y][1]) + discprice if dp[x][y][1] <= mi2: prevprevmi2 = prevmi2 prevmi2 = mi2 mi2 = dp[x][y][1] elif dp[x][y][1] > mi2 and dp[x][y][1] <= prevmi2: prevprevmi2 = prevmi2 prevmi2 = dp[x][y][1] elif dp[x][y][1] > prevmi2 and dp[x][y][1] <= prevprevmi2: prevprevmi2 = dp[x][y][1] if sorted([dp[x - 1][y][0], dp[x - 1][y][1]]) == [mi, prevmi]: dp[x][y][0] = prevprevmi + prices[x][y] elif dp[x - 1][y][0] == mi or dp[x - 1][y][1] == mi: dp[x][y][0] = prevmi + prices[x][y] else: dp[x][y][0] = mi + prices[x][y] if dp[x][y][0] <= mi2: prevprevmi2 = prevmi2 prevmi2 = mi2 mi2 = dp[x][y][0] elif dp[x][y][0] > mi2 and dp[x][y][0] <= prevmi2: prevprevmi2 = prevmi2 prevmi2 = dp[x][y][0] elif dp[x][y][0] > prevmi2 and dp[x][y][0] <= prevprevmi2: prevprevmi2 = dp[x][y][0] prevprevmi = prevprevmi2 prevmi = prevmi2 mi = mi2 print(mi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER LIST VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
t = int(input()) while t > 0: t -= 1 n, m = map(int, input().split()) mat = [] for i in range(n): temp = list(map(int, input().split())) mat.append(temp) dis = [] for i in range(n): dis.append(list(map(int, input().split()))) for i in range(1, n): minPrev = min(mat[i - 1]) for j in range(m): dp = max(0, mat[i][j] - dis[i - 1][j]) mat[i][j] = min(mat[i - 1][j] + dp, mat[i][j] + minPrev) print(min(mat[n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
t = int(input()) for i in range(t): n, m = map(int, input().split()) pri = [list(map(int, input().split())) for x in range(n)] dis = [list(map(int, input().split())) for x in range(n)] a = [([0] * m) for x in range(n)] a[0] = pri[0][:] min1 = min(pri[0]) for j in range(1, n): min2 = 10000000000000.0 for k in range(m): di = max(0, pri[j][k] - dis[j - 1][k]) a[j][k] = min(min1 + pri[j][k], a[j - 1][k] + di) min2 = min(min2, a[j][k]) min1 = min2 print(min(a[n - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
for i in range(int(input())): N, M = list(map(int, input().split(" "))) C = [list(map(int, input().split(" "))) for _ in range(N)] D = [list(map(int, input().split(" "))) for _ in range(N)] dp = [([0] * M) for _ in range(N)] for j in range(M): dp[0][j] = C[0][j] min_value = min(dp[0]) for i in range(1, N): for j in range(M): dp[i][j] = min( min_value + C[i][j], dp[i - 1][j] + max(0, C[i][j] - D[i - 1][j]) ) min_value = min(dp[i]) print(min_value)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
t = int(input()) INF = 10**20 while t > 0: n, m = map(int, input().split()) price, discount = [], [] for i in range(n): price.append([]) discount.append([]) for i in range(n): l = list(map(int, input().split())) price[i] = l for i in range(n): l = list(map(int, input().split())) discount[i] = l dp = [[0] * m] * n ans = min(price[0]) for i in range(m): val = price[0][i] dp[0][i] = val for i in range(1, n): ans2 = INF for j in range(m): val = ( price[i][j] - discount[i - 1][j] if price[i][j] - discount[i - 1][j] >= 0 else 0 ) dp[i][j] = min(dp[i - 1][j] + val, ans + price[i][j]) ans2 = min(ans2, dp[i][j]) ans = ans2 M = dp[n - 1][0] for r in dp: x = min(r) M = min(x, M) print(M) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
def helper(item, shop, discount): if item == n: return 0 elif dp[item][shop] != float("inf"): return dp[item][shop] else: for i in range(m): if shop == i: dp[item][shop] = min( dp[item][shop], prices[item][shop] - discount + helper(item + 1, shop, discounts[item][shop]), ) else: dp[item][shop] = min( dp[item][shop], prices[item][i] + helper(item + 1, i, discounts[item][i]), ) return dp[item][shop] t = int(input()) while t: n, m = list(map(int, input().split())) prices = [] for i in range(n): prices.append(list(map(int, input().split()))) discounts = [] for i in range(n): discounts.append(list(map(int, input().split()))) dp = [[(0) for i in range(m)] for j in range(n)] mn = float("inf") for j in range(m): dp[0][j] = prices[0][j] mn = min(mn, dp[0][j]) for i in range(1, n): for j in range(m): dp[i][j] = min( mn + prices[i][j], dp[i - 1][j] + max(0, prices[i][j] - discounts[i - 1][j]), ) mn = min(dp[i]) print(mn) t -= 1
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR FUNC_CALL VAR STRING RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
import sys data = [int(i) for i in sys.stdin.read().split()] data.reverse() def read(): return data.pop() T = read() for test in range(T): n, m = read(), read() P = [[read() for j in range(m)] for i in range(n)] D = [[read() for j in range(m)] for i in range(n)] DP = [[[(0) for k in range(2)] for j in range(m)] for i in range(n + 1)] mn = 0 for i in range(n - 1, -1, -1): cnt = 10**18 for j in range(m): toAdd = min(mn, DP[i + 1][j][1]) DP[i][j][0] = toAdd + P[i][j] if i: DP[i][j][1] = toAdd + max(0, P[i][j] - D[i - 1][j]) cnt = min(cnt, DP[i][j][0]) mn = cnt print(mn)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
t = int(input()) for l in range(t): n, m = list(map(int, input().split())) price = [list(map(int, input().split())) for x in range(n)] discount = [list(map(int, input().split())) for x in range(n)] ans = [([0] * m) for x in range(n)] for i in range(n): curr_min = 100000000000000 for j in range(m): if i == 0: ans[i][j] = price[i][j] else: cost1 = price[i][j] + prev_min cost2 = max(0, price[i][j] - discount[i - 1][j]) + ans[i - 1][j] ans[i][j] = min(cost1, cost2) curr_min = min(ans[i][j], curr_min) prev_min = curr_min print(curr_min)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
for _ in range(int(input())): n, m = map(int, input().split()) prices = [list(map(int, input().split())) for i in range(n)] dis = [list(map(int, input().split())) for i in range(n)] prev = min(prices[0][:]) for i in range(1, n): cur_min = float("inf") for j in range(m): prices[i][j] = min( prev + prices[i][j], prices[i - 1][j] + max(0, prices[i][j] - dis[i - 1][j]), ) cur_min = min(cur_min, prices[i][j]) prev = max(cur_min, 0) print(min(prices[-1][:]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Alice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the i^{th} item in one shop, she gets a discount coupon for the i+1^{th} item in the same shop. But if she purchases the i+1^{th} item from a different shop, she cannot use the discount coupon. For example, if she buys the i^{th} item in Shop j and gets a discount of X rupees. Let the price of i+1^{th} item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1^{th} item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1^{th} item from shop k she cannot use the discount coupon and hence, will have to pay Z rupees. Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back. Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items. ------ Input: ------ First line of input contains a single integer T, the number of test cases. Each test case starts with a line consisting of two space separated integers N & M. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the price of i^{th} item in the j^{th} shop. The next N lines consist of M space separated integers each. The j^{th} number on the i^{th} line denotes the value of discount coupon given after purchasing i^{th} item in the j^{th} shop. ------ Output: ------ For each test case, output on a separate line the least amount that has to be spent to get all the N items. ------ Constraints: ------ 1 ≤ T ≤ 10 2 ≤ N, M ≤ 100000 0 ≤ Prices, Discounts ≤ 1000000 1 ≤ T * N * M ≤ 1000000 ------ Scoring: ------ You will be awarded 40 points for solving the problem correctly for M = 2. Another 20 points for solving the problem correctly for M ≤ 100. Remaining 40 points will be awarded for solving the problem correctly for M > 100. ----- Sample Input 1 ------ 2 2 2 3 4 1 2 1 0 0 1 2 4 1 2 3 4 4 3 2 1 2 3 2 1 1 2 1 1 ----- Sample Output 1 ------ 3 2
def main(): test_case = int(input()) for itr in range(test_case): item, shop = map(int, input().split()) costMatrix = [] discountMatrix = [] for itr_s in range(item): costMatrix.append(list(map(int, input().split()))) for itr_s in range(item): discountMatrix.append(list(map(int, input().split()))) print(solve(shop, item, costMatrix, discountMatrix)) def solve(shop, item, costMatrix, discountMatrix): ans = -1 for i in range(item): tempminval = -1 for j in range(shop): if i == 0: costMatrix[i][j] = costMatrix[i][j] else: minval = costMatrix[i - 1][j] + discount( costMatrix[i][j], discountMatrix[i - 1][j] ) if j == 0: for it in range(shop): tempval = costMatrix[i - 1][it] if tempval < tempminval or tempminval == -1: tempminval = tempval minval = min(minval, costMatrix[i][j] + tempminval) costMatrix[i][j] = minval if i == item - 1: ans = costMatrix[i][j] if ans == -1 or ans > costMatrix[i][j] else ans return ans def discount(cost, discount): return cost - discount if cost > discount else 0 main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
class Solution: def maxProfit(self, prices): n = len(prices) if n < 2: return 0 sells = [0] * n buys = [0] * n buys[0] = -prices[0] for i in range(1, n): sells[i] = max(sells[i - 1], buys[i - 1] + prices[i]) buys[i] = max(buys[i - 1], (sells[i - 2] if i > 1 else 0) - prices[i]) return sells[n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR BIN_OP VAR NUMBER
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
class Solution: def maxProfit(self, prices): if not prices or len(prices) == 1: return 0 valid_status = [0, [prices[0], -prices[0]], 0] for t in range(1, len(prices)): valid_status[1][0] = prices[t] cool_down_temp = sum(valid_status[1]) if valid_status[0] > sum(valid_status[1]): valid_status[1] = [prices[t], valid_status[0] - prices[t]] if valid_status[2] > valid_status[0]: valid_status[0] = valid_status[2] valid_status[2] = cool_down_temp return max(valid_status[0], valid_status[2])
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER LIST VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER LIST VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
class Solution: def maxProfit(self, prices): if not prices: return 0 stock = -prices[0] noStock = 0 noStockSell = 0 for idx in range(len(prices)): price = prices[idx] print(noStock, stock, noStockSell) noStock, stock, noStockSell = [ max(price + stock, noStock), max(noStockSell - price, stock), noStock, ] return noStock
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
class Solution: def maxProfit(self, prices): if not prices: return 0 sell = hold = 0 buy = -prices[0] for i in range(1, len(prices)): sell, hold, buy = ( max(buy + prices[i], 0), max(hold, sell), max(hold - prices[i], buy), ) return max(sell, hold)
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
class Solution: def maxProfit(self, prices): if not prices: return 0 max_buy = [0] * len(prices) max_sell = [0] * len(prices) max_rest = [0] * len(prices) max_buy[0] = -prices[0] max_sell[0] = 0 max_rest[0] = 0 for i in range(1, len(prices)): max_buy[i] = max(max_rest[i - 1] - prices[i], max_buy[i - 1]) max_sell[i] = max(max_buy[i - 1] + prices[i], max_sell[i - 1]) max_rest[i] = max(max_sell[i - 1], max_rest[i - 1]) return max(max_buy[-1], max_sell[-1], max_rest[-1])
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
class Solution: def maxProfit(self, prices): return self.max_profit_rec(prices, {}, 0, 0) def max_profit_rec(self, prices, memo, idx, state): if idx >= len(prices): return 0 if (idx, state) in memo: return memo[idx, state] memo[idx, state] = 0 if state == 0: memo[idx, state] = max( self.max_profit_rec(prices, memo, idx + 1, 1) - prices[idx], self.max_profit_rec(prices, memo, idx + 1, 0), ) elif state == 1: memo[idx, state] = max( self.max_profit_rec(prices, memo, idx + 1, 2) + prices[idx], self.max_profit_rec(prices, memo, idx + 1, 1), ) else: memo[idx, state] = self.max_profit_rec(prices, memo, idx + 1, 0) return memo[idx, state]
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR DICT NUMBER NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
class Solution: def maxProfit(self, prices): if not prices: return 0 prices.insert(0, 0) prices.insert(0, 0) buy = [0] * len(prices) sell = [0] * len(prices) buy[2] = -prices[2] for i in range(3, len(prices)): buy[i] = max(sell[i - 2] - prices[i], buy[i - 1]) sell[i] = max(buy[i - 1] + prices[i], sell[i - 1]) return sell[-1]
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
class Solution: def maxProfit(self, prices): n = len(prices) if n < 2: return 0 buy, sell = [0] * n, [0] * n buy[0], buy[1] = -prices[0], -min(prices[0:2]) sell[1] = max(0, buy[0] + prices[1]) for i in range(2, n): sell[i] = max(sell[i - 1], buy[i - 1] + prices[i]) buy[i] = max(buy[i - 1], sell[i - 2] - prices[i]) return sell[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR NUMBER
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
class Solution: def maxProfit(self, prices): if len(prices) < 2: return 0 prevSell, sell, prevBuy, buy = 0, 0, 0, -prices[0] for price in prices: prevBuy = buy buy = max(prevBuy, prevSell - price) prevSell = sell sell = max(prevSell, prevBuy + price) return sell
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
class Solution: def maxProfit(self, prices): if prices is None or len(prices) == 0: return 0 dp = [([-9999999] * len(prices)) for _ in range(3)] dp[0][0], dp[1][0], dp[2][0] = -prices[0], 0, 0 for i in range(1, len(prices)): dp[0][i] = max(dp[0][i - 1], dp[2][i - 1] - prices[i]) dp[1][i] = max(dp[0][i - 1] + prices[i], dp[1][i - 1]) dp[2][i] = max(dp[2][i - 1], dp[0][i - 1], dp[1][i - 1]) return max(dp[1][-1], dp[2][-1])
CLASS_DEF FUNC_DEF IF VAR NONE FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
class Solution: def maxProfit(self, prices): free = 0 have = cool = float("-inf") for p in prices: free, have, cool = max(free, cool), max(have, free - p), have + p return max(free, cool)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) Example: Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
class Solution: def maxProfit(self, prices): n = len(prices) if not n: return 0 buys = [None] * n sells = [None] * n sells[0], buys[0] = 0, -prices[0] for x in range(1, n): delta = prices[x] - prices[x - 1] sells[x] = max(buys[x - 1] + prices[x], sells[x - 1] + delta) buys[x] = max( buys[x - 1] - delta, sells[x - 2] - prices[x] if x > 1 else -9999 ) return max(sells)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the $1$-st mirror again. You need to calculate the expected number of days until Creatnx becomes happy. This number should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains one integer $n$ ($1\le n\le 2\cdot 10^5$) — the number of mirrors. The second line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). -----Output----- Print the answer modulo $998244353$ in a single line. -----Examples----- Input 1 50 Output 2 Input 3 10 20 50 Output 112 -----Note----- In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability $\frac{1}{2}$. So, the expected number of days until Creatnx becomes happy is $2$.
n = input() n = int(n) md = 998244353 L = list(map(int, input().split())) def inv(x): nonlocal md return pow(x, md - 2, md) ml = 1 ans = 0 for el in reversed(L): ml *= 100 ml *= inv(el) ml %= md ans += ml ans %= md print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the $1$-st mirror again. You need to calculate the expected number of days until Creatnx becomes happy. This number should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains one integer $n$ ($1\le n\le 2\cdot 10^5$) — the number of mirrors. The second line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). -----Output----- Print the answer modulo $998244353$ in a single line. -----Examples----- Input 1 50 Output 2 Input 3 10 20 50 Output 112 -----Note----- In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability $\frac{1}{2}$. So, the expected number of days until Creatnx becomes happy is $2$.
input() p = list(map(int, input().split())) mod = 998244353 f = 0 for i in p: f = 100 * (f + 1) * pow(i, mod - 2, mod) % mod print(f)
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the $1$-st mirror again. You need to calculate the expected number of days until Creatnx becomes happy. This number should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains one integer $n$ ($1\le n\le 2\cdot 10^5$) — the number of mirrors. The second line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). -----Output----- Print the answer modulo $998244353$ in a single line. -----Examples----- Input 1 50 Output 2 Input 3 10 20 50 Output 112 -----Note----- In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability $\frac{1}{2}$. So, the expected number of days until Creatnx becomes happy is $2$.
prime = 998244353 def power(x, y, prime): b = bin(y)[2:] start = x answer = 1 for i in range(len(b)): b2 = b[len(b) - 1 - i] if b2 == "1": answer = answer * start % prime start = start * start % prime return answer def gcd(a, b): if a > b: a, b = b, a if b % a == 0: return a return gcd(b % a, a) def add(f1, f2): p1, q1 = f1 p2, q2 = f2 num = p1 * q2 + p2 * q1 den = q1 * q2 g = gcd(num, den) return [num // g, den // g] def mult(f1, f2): p1, q1 = f1 p2, q2 = f2 num = p1 * p2 den = q1 * q2 g = gcd(num, den) return [num // g, den // g] def process(A): num = 100 den = 1 for p in A[:-1]: den = p * den num = 100 * (num + den) num, den = num % prime, den % prime den = den * A[-1] % prime P, Q = num % prime, den % prime Qinv = power(Q, prime - 2, prime) answer = P * Qinv % prime return answer n = int(input()) A = [int(x) for x in input().split()] print(process(A))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN LIST BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN LIST BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the $1$-st mirror again. You need to calculate the expected number of days until Creatnx becomes happy. This number should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains one integer $n$ ($1\le n\le 2\cdot 10^5$) — the number of mirrors. The second line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). -----Output----- Print the answer modulo $998244353$ in a single line. -----Examples----- Input 1 50 Output 2 Input 3 10 20 50 Output 112 -----Note----- In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability $\frac{1}{2}$. So, the expected number of days until Creatnx becomes happy is $2$.
prime = 998244353 def power(x, y, prime): b = bin(y)[2:] start = x answer = 1 for i in range(len(b)): b2 = b[len(b) - 1 - i] if b2 == "1": answer = answer * start % prime start = start * start % prime return answer def process(A): P = 100 Q = 1 for p in A[:-1]: Q = p * Q % prime P = 100 * (P + Q) % prime Q = Q * A[-1] % prime Qinv = power(Q, prime - 2, prime) answer = P * Qinv % prime return answer n = int(input()) A = [int(x) for x in input().split()] print(process(A))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Creatnx has $n$ mirrors, numbered from $1$ to $n$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $i$-th mirror will tell Creatnx that he is beautiful with probability $\frac{p_i}{100}$ for all $1 \le i \le n$. Creatnx asks the mirrors one by one, starting from the $1$-st mirror. Every day, if he asks $i$-th mirror, there are two possibilities: The $i$-th mirror tells Creatnx that he is beautiful. In this case, if $i = n$ Creatnx will stop and become happy, otherwise he will continue asking the $i+1$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the $1$-st mirror again. You need to calculate the expected number of days until Creatnx becomes happy. This number should be found by modulo $998244353$. Formally, let $M = 998244353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$. -----Input----- The first line contains one integer $n$ ($1\le n\le 2\cdot 10^5$) — the number of mirrors. The second line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq 100$). -----Output----- Print the answer modulo $998244353$ in a single line. -----Examples----- Input 1 50 Output 2 Input 3 10 20 50 Output 112 -----Note----- In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability $\frac{1}{2}$. So, the expected number of days until Creatnx becomes happy is $2$.
def solve(): s = input() if "aa" in s or "bb" in s or "cc" in s: print(-1) return syms = s + "@" ans = ["@"] for i, sym in enumerate(s): if sym != "?": ans.append(sym) continue for x in "abc": if x != ans[-1] and x != syms[i + 1]: ans.append(x) break print("".join(ans[1:])) def solveb(): n = int(input()) perm = [int(x) for x in input().split()] num___idx = [(-1) for _ in range(n + 1)] for i, num in enumerate(perm): num___idx[num] = i curr_max = -1 curr_min = 2 * n num___pretty = [(0) for _ in range(n + 1)] for num in range(1, n + 1): curr_max = max(num___idx[num], curr_max) curr_min = min(num___idx[num], curr_min) if curr_max - curr_min + 1 == num: num___pretty[num] = 1 print(*num___pretty[1:], sep="") def solvec(): n = int(input()) rank___problems_nr = [int(x) for x in input().split()] weird_prefsums = [0] last_num = rank___problems_nr[0] for num in rank___problems_nr: if num != last_num: last_num = num weird_prefsums.append(weird_prefsums[-1]) weird_prefsums[-1] += 1 gold = weird_prefsums[0] silvers = 0 i = 1 for i in range(1, len(weird_prefsums)): x = weird_prefsums[i] if x - gold > gold: silvers = x - gold break bronzes = 0 for j in range(i, len(weird_prefsums)): x = weird_prefsums[j] if x > n / 2: break if x - gold - silvers > gold: bronzes = x - gold - silvers if bronzes == 0 or silvers == 0: print(0, 0, 0) return print(gold, silvers, bronzes) def solved(): a, b, c, d = (int(x) for x in input().split()) ab_len = min(a, b) a -= ab_len b -= ab_len cd_len = min(c, d) c -= cd_len d -= cd_len if a == 1 and cd_len == 0 and d == 0 and c == 0: print("YES") print("0 1 " * ab_len + "0") return if d == 1 and ab_len == 0 and a == 0 and b == 0: print("YES") print("3 " + "2 3 " * cd_len) return if a > 0 or d > 0: print("NO") return cb_len = min(b, c) b -= cb_len c -= cb_len if b > 1 or c > 1: print("NO") return print("YES") print("1 " * b + "0 1 " * ab_len + "2 1 " * cb_len + "2 3 " * cd_len + "2" * c) def get_me(prob, mod): return 100 * pow(prob, mod - 2, mod) % mod def solvee(): n = int(input()) mod = 998244353 idx___prob = [int(x) for x in input().split()] curr_me = get_me(idx___prob[0], mod) for prob in idx___prob[1:]: me = get_me(prob, mod) curr_me *= me curr_me %= mod curr_me += me curr_me %= mod print(curr_me) def __starting_point(): solvee() __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF STRING VAR STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR LIST STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR STRING IF VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR STRING RETURN IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING VAR RETURN IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Vasya wrote down two strings s of length n and t of length m consisting of small English letters 'a' and 'b'. What is more, he knows that string t has a form "abab...", namely there are letters 'a' on odd positions and letters 'b' on even positions. Suddenly in the morning, Vasya found that somebody spoiled his string. Some letters of the string s were replaced by character '?'. Let's call a sequence of positions i, i + 1, ..., i + m - 1 as occurrence of string t in s, if 1 ≤ i ≤ n - m + 1 and t_1 = s_{i}, t_2 = s_{i} + 1, ..., t_{m} = s_{i} + m - 1. The boy defines the beauty of the string s as maximum number of disjoint occurrences of string t in s. Vasya can replace some letters '?' with 'a' or 'b' (letters on different positions can be replaced with different letter). Vasya wants to make some replacements in such a way that beauty of string s is maximum possible. From all such options, he wants to choose one with the minimum number of replacements. Find the number of replacements he should make. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of s. The second line contains the string s of length n. It contains small English letters 'a', 'b' and characters '?' only. The third line contains a single integer m (1 ≤ m ≤ 10^5) — the length of t. The string t contains letters 'a' on odd positions and 'b' on even positions. -----Output----- Print the only integer — the minimum number of replacements Vasya has to perform to make the beauty of string s the maximum possible. -----Examples----- Input 5 bb?a? 1 Output 2 Input 9 ab??ab??? 3 Output 2 -----Note----- In the first sample string t has a form 'a'. The only optimal option is to replace all characters '?' by 'a'. In the second sample using two replacements we can make string equal to "aba?aba??". It is impossible to get more than two occurrences.
n = int(input()) s = input() m = int(input()) a = [0] * (n + 2) b = [0] * (n + 2) q = [0] * (n + 1) dp = [(0, 0)] * (n + 2) for i in range(0, n): b[i] = b[i - 2] + (s[i] == "b") a[i] = a[i - 2] + (s[i] == "a") q[i] = q[i - 1] + (s[i] == "?") for i in range(n - m, -1, -1): dp[i] = dp[i + 1] i_b = 1 if m % 2 == 1 else 2 i_a = 1 if m % 2 == 0 else 2 if not (b[i + m - i_b] - b[i - 2] or a[i + m - i_a] - a[i - 1]): t, r = dp[i + m] dp[i] = min((t - 1, r + q[i + m - 1] - q[i - 1]), dp[i]) print(dp[0][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Vasya wrote down two strings s of length n and t of length m consisting of small English letters 'a' and 'b'. What is more, he knows that string t has a form "abab...", namely there are letters 'a' on odd positions and letters 'b' on even positions. Suddenly in the morning, Vasya found that somebody spoiled his string. Some letters of the string s were replaced by character '?'. Let's call a sequence of positions i, i + 1, ..., i + m - 1 as occurrence of string t in s, if 1 ≤ i ≤ n - m + 1 and t_1 = s_{i}, t_2 = s_{i} + 1, ..., t_{m} = s_{i} + m - 1. The boy defines the beauty of the string s as maximum number of disjoint occurrences of string t in s. Vasya can replace some letters '?' with 'a' or 'b' (letters on different positions can be replaced with different letter). Vasya wants to make some replacements in such a way that beauty of string s is maximum possible. From all such options, he wants to choose one with the minimum number of replacements. Find the number of replacements he should make. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of s. The second line contains the string s of length n. It contains small English letters 'a', 'b' and characters '?' only. The third line contains a single integer m (1 ≤ m ≤ 10^5) — the length of t. The string t contains letters 'a' on odd positions and 'b' on even positions. -----Output----- Print the only integer — the minimum number of replacements Vasya has to perform to make the beauty of string s the maximum possible. -----Examples----- Input 5 bb?a? 1 Output 2 Input 9 ab??ab??? 3 Output 2 -----Note----- In the first sample string t has a form 'a'. The only optimal option is to replace all characters '?' by 'a'. In the second sample using two replacements we can make string equal to "aba?aba??". It is impossible to get more than two occurrences.
match = 0 nonmatch = 0 count = 0 def calc_match(s, t, p): global match global nonmatch global count if p == len(s) - len(t): return if p + len(t) < len(s): if s[p + len(t)] == "?": count -= 1 elif s[p + len(t)] == t[-1]: match -= 1 else: nonmatch -= 1 match, nonmatch = nonmatch, match if p + len(t) < len(s): if s[p] == "?": count += 1 elif s[p] == "a": match += 1 else: nonmatch += 1 def init_match(s, t): global match global nonmatch global count p = len(s) - len(t) for i in range(len(t)): if s[p + i] == "?": count += 1 elif s[p + i] == t[i]: match += 1 else: nonmatch += 1 n = int(input()) s = input() m = int(input()) t = "" for i in range(m): if i % 2 == 0: t = t + "a" else: t = t + "b" init_match(s, t) dp = [] for i in range(n + 3): dp.append((0, 0)) p = n - m while p >= 0: calc_match(s, t, p) if nonmatch == 0: if dp[p + 1][0] == dp[p + m][0] + 1: dp[p] = dp[p + 1][0], min(dp[p + 1][1], dp[p + m][1] + count) elif dp[p + 1][0] > dp[p + m][0] + 1: dp[p] = dp[p + 1] else: dp[p] = dp[p + m][0] + 1, dp[p + m][1] + count else: dp[p] = dp[p + 1] p -= 1 print(dp[0][1])
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Example 1: Input: S = "aabe" Output: 3 Explanation: The substring "aab" can be rearranged to "aba" which is the longest palindrome possible for this String. Example 2: Input: S = "adbabd" Output: 6 Explanation: The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is "abddba". Thus, output length of the string is 6. Your Task: You don't need to read input or print anything. Your task is to complete the function longestSubstring() which takes a String S as input and returns the length of largest possible Palindrome. Expected Time Complexity: O(|S|*26) Expected Auxiliary Space: O(|S|*26) Constraints: 1 ≤ |S| ≤ 10^{5}
class Solution: def longestSubstring(self, S): n = len(S) index = dict() answer = 0 mask = 0 index[mask] = -1 for i in range(n): temp = ord(S[i]) - 97 mask ^= 1 << temp if mask in index.keys(): answer = max(answer, i - index[mask]) else: index[mask] = i for j in range(26): mask2 = mask ^ 1 << j if mask2 in index.keys(): answer = max(answer, i - index[mask2]) return answer
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Example 1: Input: S = "aabe" Output: 3 Explanation: The substring "aab" can be rearranged to "aba" which is the longest palindrome possible for this String. Example 2: Input: S = "adbabd" Output: 6 Explanation: The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is "abddba". Thus, output length of the string is 6. Your Task: You don't need to read input or print anything. Your task is to complete the function longestSubstring() which takes a String S as input and returns the length of largest possible Palindrome. Expected Time Complexity: O(|S|*26) Expected Auxiliary Space: O(|S|*26) Constraints: 1 ≤ |S| ≤ 10^{5}
class Solution: def longestSubstring(self, s): d = {} d[0] = -1 x = ans = 0 for i in range(len(s)): x = x ^ 1 << ord(s[i]) - ord("a") if x in d: ans = max(ans, i - d[x]) else: d[x] = i for j in range(0, 26): m = x ^ 1 << j if m in d: ans = max(ans, i - d[m]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Example 1: Input: S = "aabe" Output: 3 Explanation: The substring "aab" can be rearranged to "aba" which is the longest palindrome possible for this String. Example 2: Input: S = "adbabd" Output: 6 Explanation: The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is "abddba". Thus, output length of the string is 6. Your Task: You don't need to read input or print anything. Your task is to complete the function longestSubstring() which takes a String S as input and returns the length of largest possible Palindrome. Expected Time Complexity: O(|S|*26) Expected Auxiliary Space: O(|S|*26) Constraints: 1 ≤ |S| ≤ 10^{5}
class Solution: def longestSubstring(self, s): n = len(s) masks = {(0): -1} def check(mask1, idx): res = 0 if mask1 in masks: res = max(res, idx - masks[mask1]) for i in range(26): mask2 = mask1 ^ 1 << i if mask2 in masks: res = max(res, idx - masks[mask2]) return res mask, ans = 0, 0 for i, c in enumerate(s): mask ^= 1 << ord(c) - ord("a") ans = max(ans, check(mask, i)) if mask not in masks: masks[mask] = i return ans if __name__ == "__main__": t = int(input()) for _ in range(t): S = input() ob = Solution() print(ob.longestSubstring(S))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Example 1: Input: S = "aabe" Output: 3 Explanation: The substring "aab" can be rearranged to "aba" which is the longest palindrome possible for this String. Example 2: Input: S = "adbabd" Output: 6 Explanation: The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is "abddba". Thus, output length of the string is 6. Your Task: You don't need to read input or print anything. Your task is to complete the function longestSubstring() which takes a String S as input and returns the length of largest possible Palindrome. Expected Time Complexity: O(|S|*26) Expected Auxiliary Space: O(|S|*26) Constraints: 1 ≤ |S| ≤ 10^{5}
class Solution: def longestSubstring(self, S): mask = 0 d = {(0): -1} ans = 0 for i, c in enumerate(S): bit = ord(c) - ord("a") mask ^= 1 << bit if mask in d: ans = max(ans, i - d[mask]) for j in range(26): if mask ^ 1 << j in d: ans = max(ans, i - d[mask ^ 1 << j]) if mask not in d: d[mask] = i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Example 1: Input: S = "aabe" Output: 3 Explanation: The substring "aab" can be rearranged to "aba" which is the longest palindrome possible for this String. Example 2: Input: S = "adbabd" Output: 6 Explanation: The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is "abddba". Thus, output length of the string is 6. Your Task: You don't need to read input or print anything. Your task is to complete the function longestSubstring() which takes a String S as input and returns the length of largest possible Palindrome. Expected Time Complexity: O(|S|*26) Expected Auxiliary Space: O(|S|*26) Constraints: 1 ≤ |S| ≤ 10^{5}
class Solution: def longestSubstring(self, s): d = {} d[0] = -1 x = ans = 0 for i in range(len(s)): x = x ^ 1 << ord(s[i]) - ord("a") if x in d: ans = max(ans, i - d[x]) else: d[x] = i for j in range(0, 26): m = x ^ 1 << j if m in d: ans = max(ans, i - d[m]) return ans def palindrome(S): dict_ = {} for i in S: if i in dict_: dict_[i] += 1 else: dict_[i] = 1 max = 0 for i in dict_: if dict_[i] % 2 != 0: max += 1 if max > 1: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Example 1: Input: S = "aabe" Output: 3 Explanation: The substring "aab" can be rearranged to "aba" which is the longest palindrome possible for this String. Example 2: Input: S = "adbabd" Output: 6 Explanation: The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is "abddba". Thus, output length of the string is 6. Your Task: You don't need to read input or print anything. Your task is to complete the function longestSubstring() which takes a String S as input and returns the length of largest possible Palindrome. Expected Time Complexity: O(|S|*26) Expected Auxiliary Space: O(|S|*26) Constraints: 1 ≤ |S| ≤ 10^{5}
class Solution: def longestSubstring(self, s): d = {} d[0] = -1 x = ans = 0 for i in range(len(s)): x = x ^ 1 << ord(s[i]) - ord("a") if x in d: ans = max(ans, i - d[x]) else: d[x] = i for j in range(0, 26): m = x ^ 1 << j if m in d: ans = max(ans, i - d[m]) return ans if __name__ == "__main__": t = int(input()) for _ in range(t): S = input() ob = Solution() print(ob.longestSubstring(S))
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Example 1: Input: S = "aabe" Output: 3 Explanation: The substring "aab" can be rearranged to "aba" which is the longest palindrome possible for this String. Example 2: Input: S = "adbabd" Output: 6 Explanation: The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is "abddba". Thus, output length of the string is 6. Your Task: You don't need to read input or print anything. Your task is to complete the function longestSubstring() which takes a String S as input and returns the length of largest possible Palindrome. Expected Time Complexity: O(|S|*26) Expected Auxiliary Space: O(|S|*26) Constraints: 1 ≤ |S| ≤ 10^{5}
class Solution: def longestSubstring(self, S): d = {} d[0] = -1 x = ans = 0 for i in range(len(S)): x = x ^ 1 << ord(S[i]) - ord("a") if x in d: ans = max(ans, i - d[x]) else: d[x] = i for j in range(0, 26): m = x ^ 1 << j if m in d: ans = max(ans, i - d[m]) return ans def permute(self, xs, low=0): if low + 1 >= len(xs): yield xs else: for p in self.permute(xs, low + 1): yield p for i in range(low + 1, len(xs)): xs[low], xs[i] = xs[i], xs[low] for p in self.permute(xs, low + 1): yield p xs[low], xs[i] = xs[i], xs[low] def isPalindrom(self, S): return S == S[::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN VAR VAR NUMBER
Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Example 1: Input: S = "aabe" Output: 3 Explanation: The substring "aab" can be rearranged to "aba" which is the longest palindrome possible for this String. Example 2: Input: S = "adbabd" Output: 6 Explanation: The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is "abddba". Thus, output length of the string is 6. Your Task: You don't need to read input or print anything. Your task is to complete the function longestSubstring() which takes a String S as input and returns the length of largest possible Palindrome. Expected Time Complexity: O(|S|*26) Expected Auxiliary Space: O(|S|*26) Constraints: 1 ≤ |S| ≤ 10^{5}
class Solution: def longestSubstring(self, S): length = 0 mask = 0 bitMap = {} bitMap[mask] = -1 for i in range(len(S)): index = ord(S[i]) - 97 mask = mask ^ 1 << index if mask in bitMap: length = max(length, i - bitMap[mask]) else: bitMap[mask] = i for j in range(26): mask2 = mask ^ 1 << j if mask2 in bitMap: length = max(length, i - bitMap[mask2]) return length
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Example 1: Input: S = "aabe" Output: 3 Explanation: The substring "aab" can be rearranged to "aba" which is the longest palindrome possible for this String. Example 2: Input: S = "adbabd" Output: 6 Explanation: The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is "abddba". Thus, output length of the string is 6. Your Task: You don't need to read input or print anything. Your task is to complete the function longestSubstring() which takes a String S as input and returns the length of largest possible Palindrome. Expected Time Complexity: O(|S|*26) Expected Auxiliary Space: O(|S|*26) Constraints: 1 ≤ |S| ≤ 10^{5}
class Solution: def is_palin(self, mapper, n): odd = 0 for i in mapper.values(): if i & 1: odd += 1 if odd == 0: return True elif n & 1 and odd == 1: return True else: return False def front(self, mapper, s, n): for i in range(n): mapper[s[i]] -= 1 if self.is_palin(mapper, n - i - 1): return n - i - 1 return 1 def ending(self, mapper, s, n): for i in range(n - 1, -1, -1): mapper[s[i]] -= 1 if self.is_palin(mapper, i): return i return 1 def both_side(self, mapper, s, n): i = 0 j = n - 1 while i < j: mapper[s[i]] -= 1 mapper[s[j]] -= 1 n -= 2 if self.is_palin(mapper, n): return n i += 1 j -= 1 return 1 def longestSubstring(self, s): n = len(s) mapper = dict() ans = 1 l = len(s) f = {(0): -1} x = 0 for i in range(l): z = ord(s[i]) - 97 x = x ^ 1 << z if x in f: ans = max(ans, i - f[x]) for j in range(26): t = x ^ 1 << j if t in f: ans = max(ans, i - f[t]) if x not in f: f[x] = i return ans maxi = -1000000000.0 for i in range(n): mapper = dict() for j in range(i, n): mapper[s[j]] = 1 + mapper.get(s[j], 0) if self.is_palin(mapper, j - i + 1): maxi = max(maxi, j - i + 1) return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Example 1: Input: S = "aabe" Output: 3 Explanation: The substring "aab" can be rearranged to "aba" which is the longest palindrome possible for this String. Example 2: Input: S = "adbabd" Output: 6 Explanation: The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is "abddba". Thus, output length of the string is 6. Your Task: You don't need to read input or print anything. Your task is to complete the function longestSubstring() which takes a String S as input and returns the length of largest possible Palindrome. Expected Time Complexity: O(|S|*26) Expected Auxiliary Space: O(|S|*26) Constraints: 1 ≤ |S| ≤ 10^{5}
class Solution: def longestSubstring(self, s): n = len(s) masks = {(0): -1} def check(mask1, idx): res = 0 if mask1 in masks: res = max(res, idx - masks[mask1]) for i in range(26): mask2 = mask1 ^ 1 << i if mask2 in masks: res = max(res, idx - masks[mask2]) return res mask, ans = 0, 0 for i, c in enumerate(s): mask ^= 1 << ord(c) - ord("a") ans = max(ans, check(mask, i)) if mask not in masks: masks[mask] = i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Example 1: Input: S = "aabe" Output: 3 Explanation: The substring "aab" can be rearranged to "aba" which is the longest palindrome possible for this String. Example 2: Input: S = "adbabd" Output: 6 Explanation: The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is "abddba". Thus, output length of the string is 6. Your Task: You don't need to read input or print anything. Your task is to complete the function longestSubstring() which takes a String S as input and returns the length of largest possible Palindrome. Expected Time Complexity: O(|S|*26) Expected Auxiliary Space: O(|S|*26) Constraints: 1 ≤ |S| ≤ 10^{5}
class Solution: def longestSubstring(self, S): Dict = {(0): -1} res = 0 x = 0 N = len(S) for i in range(N): num = ord(S[i]) - ord("a") x = x ^ 1 << num if x not in Dict: Dict[x] = i else: idx = Dict[x] res = max(res, i - Dict[x]) for j in range(26): m = x ^ 1 << j if m in Dict: res = max(res, i - Dict[m]) return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given a string S which only contains lowercase alphabets. Find the length of the longest substring of S such that the characters in it can be rearranged to form a palindrome. Example 1: Input: S = "aabe" Output: 3 Explanation: The substring "aab" can be rearranged to "aba" which is the longest palindrome possible for this String. Example 2: Input: S = "adbabd" Output: 6 Explanation: The whole string “adbabd” can be rearranged to form a palindromic substring. One possible arrangement is "abddba". Thus, output length of the string is 6. Your Task: You don't need to read input or print anything. Your task is to complete the function longestSubstring() which takes a String S as input and returns the length of largest possible Palindrome. Expected Time Complexity: O(|S|*26) Expected Auxiliary Space: O(|S|*26) Constraints: 1 ≤ |S| ≤ 10^{5}
class Solution: def longestSubstring(self, S): n = len(S) max_len = 0 mp = {} mask = 0 mp[mask] = -1 for i in range(n): idx = ord(S[i]) - ord("a") mask = mask ^ 1 << idx if mask in mp: length = i - mp[mask] max_len = max(max_len, length) else: mp[mask] = i for j in range(26): mask2 = mask ^ 1 << j if mask2 in mp: length = i - mp[mask2] max_len = max(max_len, length) return max_len
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: dp = [] def gen(self, s, t, n, m, c): if m == 0 or n == 0: return c if s[n - 1] == t[m - 1]: c = self.gen(s, t, n - 1, m - 1, c + 1) l = self.gen(s, t, n - 1, m, 0) r = self.gen(s, t, n, m - 1, 0) return max(l, r, c) def longestCommonSubstr(self, s, t, n, m): dp = [] for i in range(n + 1): b = [] for j in range(m + 1): b.append(-1) dp.append(b) for i in range(n + 1): dp[i][0] = 0 for i in range(m + 1): dp[0][i] = 0 maxi = 0 for i in range(1, n + 1): for j in range(1, m + 1): if s[i - 1] == t[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 if dp[i][j] > maxi: maxi = dp[i][j] else: dp[i][j] = 0 return maxi
CLASS_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, S1, S2, n, m): table = [([0] * (m + 1)) for i in range(n + 1)] maxlen = 0 for i in range(1, n + 1): for j in range(1, m + 1): if S1[i - 1] == S2[j - 1]: table[i][j] = table[i - 1][j - 1] + 1 if table[i][j] > maxlen: maxlen = table[i][j] else: table[i][j] = 0 return maxlen
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, str1, str2, n, m): dp = [] p = [] result = 0 for i in range(n + 1): for j in range(m + 1): p.append(0) dp.append(p) p = [] for i in range(n + 1): for j in range(m + 1): if i == 0 or j == 0: dp[i][j] = 0 elif str1[i - 1] == str2[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 result = max(result, dp[i][j]) else: dp[i][j] = 0 return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
import sys class Solution: def longestCommonSubstr(self, s1, s2, n, m): dp = [[(0) for j in range(m + 1)] for i in range(n + 1)] import sys ans = -1 for i in range(1, n + 1): for j in range(1, m + 1): if s1[i - 1] == s2[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] else: dp[i][j] = 0 ans = max(dp[i][j], ans) return ans def solve(self, n, m, s1, s2, dp): if n < 0 or m < 0: return 0 if dp[n][m] != -1: return dp[n][m] if s1[n] == s2[m]: x = 1 + self.solve(n - 1, m - 1, s1, s2, dp) else: x = max( max(self.solve(n - 1, m, s1, s2, dp), self.solve(n, m - 1, s1, s2, dp)), x, ) dp[n][m] = x return x
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IMPORT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, s1, s2, x, y): x = len(s1) y = len(s2) maxi = 0 dp = [[(-1) for j in range(y + 1)] for i in range(x + 1)] for i in range(x + 1): for j in range(y + 1): if i == 0 or j == 0: dp[i][j] = 0 continue if s1[i - 1] == s2[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] maxi = max(maxi, dp[i][j]) else: dp[i][j] = 0 return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, text1, text2, n, m): ma = 0 dp = [(0) for i in range(len(text2) + 1)] cur = [(0) for i in range(len(text2) + 1)] for i in range(1, len(text1) + 1): for j in range(1, len(text2) + 1): if text1[i - 1] == text2[j - 1]: cur[j] = 1 + dp[j - 1] ma = max(ma, cur[j]) else: cur[j] = 0 dp = cur[:] return ma
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, S1, S2, n, m): l = [] for i in range(len(S1)): for j in range(i + 1, len(S1) + 1): k = S1[i:j] if k in S2: l.append(k) else: break if len(l) == 0: return 0 l.sort(key=len) return len(l[-1])
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, S1, S2, n, m): t = [[(0) for i in range(m + 1)] for j in range(n + 1)] for i in range(m + 1): t[0][i] = 0 for j in range(n + 1): t[j][0] = 0 maxx = 0 for i in range(1, n + 1): for j in range(1, m + 1): if S1[i - 1] == S2[j - 1]: t[i][j] = 1 + t[i - 1][j - 1] maxx = max(maxx, t[i][j]) else: t[i][j] = 0 return maxx
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, s1, s2, n1, n2): def lcs(i1, i2, n1, n2, s1, s2, dp): ans = 0 for i1 in range(n1): for i2 in range(n2): if s1[i1] == s2[i2]: if i1 > 0 and i2 > 0: dp[i1][i2] = dp[i1 - 1][i2 - 1] + 1 else: dp[i1][i2] = 1 ans = max(ans, dp[i1][i2]) else: dp[i1][i2] = 0 return ans dp = [] for i in range(n1): dp.append([-1] * n2) return lcs(0, 0, n1, n2, s1, s2, dp)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, s1, s2, n, m): res = [[(0) for a in range(n + 1)] for b in range(m + 1)] for i in range(m): for j in range(n): if s1[j] == s2[i]: res[i + 1][j + 1] = 1 + res[i][j] max1 = 0 for i in res: max1 = max(max1, max(i)) return max1
CLASS_DEF FUNC_DEF ASSIGN 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 IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, S1, S2, n, m): prev = [0] * (m + 1) curr = [0] * (m + 1) maxm = 0 for i in range(1, n + 1): for j in range(1, m + 1): if S1[i - 1] == S2[j - 1]: curr[j] = 1 + prev[j - 1] maxm = max(maxm, curr[j]) else: curr[j] = 0 prev = curr[:] return maxm
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, S1, S2, n, m): if m == 0 or n == 0: return 0 if m > n: return self.longestCommonSubstr(S2, S1, m, n) longest = 0 count = [[(0) for _ in range(n + 1)] for _ in range(m + 1)] for row in range(1, m + 1): for col in range(1, n + 1): if S2[row - 1] == S1[col - 1]: count[row][col] = count[row - 1][col - 1] + 1 longest = max(longest, count[row][col]) return longest
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, S1, S2, n, m): table = [[(0) for _ in range(m + 1)] for _ in range(n + 1)] memo = [0] for i in range(n): for j in range(m): if S1[i] == S2[j]: table[i + 1][j + 1] = 1 + table[i][j] memo.append(table[i + 1][j + 1]) return max(memo)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, X, Y, m, n): dp = [[(0) for k in range(n)] for l in range(m)] result = 0 for i in range(m): for j in range(n): if X[i] == Y[j]: dp[i][j] = (dp[i - 1][j - 1] if i > 0 and j > 0 else 0) + 1 else: dp[i][j] = 0 result = max(result, dp[i][j]) return result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, str1, str2, n, m): m = len(str1) n = len(str2) matrix = [([0] * (n + 1)) for _ in range(m + 1)] max_length = 0 end_position = 0 for i in range(1, m + 1): for j in range(1, n + 1): if str1[i - 1] == str2[j - 1]: matrix[i][j] = matrix[i - 1][j - 1] + 1 if matrix[i][j] > max_length: max_length = matrix[i][j] end_position = i longest_substring = str1[end_position - max_length : end_position] return len(longest_substring)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, s1, s2, n, m): c = 0 mx = 0 i = 0 j = 0 while j < n: t = s1[i : j + 1] if t in s2: c += 1 j += 1 else: if c > mx: mx = c i += 1 j = i c = 0 if c > mx: mx = c return mx
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, S1, S2, n, m): string1, string2 = S1, S2 prev_dp = [(1 if string1[0] == char else 0) for char in string2] ans = max(prev_dp) for i in range(1, n): current_dp = [0] * m current_dp[0] = 1 if string1[i] == string2[0] else 0 ans = max(ans, current_dp[0]) for j in range(1, m): current_dp[j] = 0 if string1[i] != string2[j] else 1 + prev_dp[j - 1] ans = max(ans, current_dp[j]) prev_dp = current_dp return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
import sys class Solution: def longestCommonSubstr(self, S1, S2, n, m): t = [[(0) for j in range(m + 1)] for i in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): if S1[i - 1] == S2[j - 1]: t[i][j] = 1 + t[i - 1][j - 1] else: t[i][j] = 0 mx = -sys.maxsize for i in range(n + 1): for j in range(m + 1): mx = max(mx, t[i][j]) return mx
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, s1, s2, n, m): res = 0 matrix = [] for i in range(n + 1): matrix.append([0] * (m + 1)) for i in range(1, n + 1): for j in range(1, m + 1): if s1[i - 1] == s2[j - 1]: matrix[i][j] = matrix[i - 1][j - 1] + 1 res = max(matrix[i][j], res) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, text1, text2, n, m): ans = 0 Max = 0 dp = [[(0) for i in range(m + 1)] for j in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): if text1[i - 1] == text2[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] Max = max(Max, dp[i][j]) else: dp[i][j] = 0 return Max
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given two strings. The task is to find the length of the longest common substring. Example 1: Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6 Output: 4 Explanation: The longest common substring is "CDGH" which has length 4. Example 2: Input: S1 = "ABC", S2 "ACB", n = 3, m = 3 Output: 1 Explanation: The longest common substrings are "A", "B", "C" all having length 1. Your Task: You don't need to read input or print anything. Your task is to complete the function longestCommonSubstr() which takes the string S1, string S2 and their length n and m as inputs and returns the length of the longest common substring in S1 and S2. Expected Time Complexity: O(n*m). Expected Auxiliary Space: O(n*m). Constraints: 1<=n, m<=1000
class Solution: def longestCommonSubstr(self, S1, S2, n, m): ans = 0 dp = [[(0) for i in range(m + 1)] for j in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): if S1[i - 1] == S2[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] ans = max(ans, dp[i][j]) return ans def dfs(self, S1, S2, n, m, dp): if n == 0 or m == 0: return 0 elif dp[n][m] != -1: return dp[n][m] if S1[n - 1] == S2[m - 1]: dp[n][m] = 1 + self.dfs(S1, S2, n - 1, m - 1, dp) else: return 0 return dp[n][m]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN VAR VAR VAR