description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
import sys def input(): return sys.stdin.readline().strip() cases = int(input()) for _ in range(cases): n, q = map(int, input().split()) instructions = input() minA = [0] * (n + 1) maxA = [0] * (n + 1) prefix = [0] current = 0 for i, s in enumerate(instructions): if s == "+": current += 1 else: current -= 1 index = i + 1 minA[index] = min(minA[index - 1], current) maxA[index] = max(current, maxA[index - 1]) prefix.append(current) current = 0 minD = [prefix[-1]] * (n + 1) maxD = [prefix[-1]] * (n + 1) for index in range(n - 1, 0, -1): minD[index] = min(minD[index + 1], prefix[index]) maxD[index] = max(prefix[index], maxD[index + 1]) for _ in range(q): start, end = map(int, input().split()) rangeStart = [minA[start - 1], maxA[start - 1]] ignored = prefix[end] - prefix[start - 1] rangeEnd = [minD[end] - ignored, maxD[end] - ignored] maxComb = max(rangeEnd[1], rangeStart[1]) minComb = min(rangeEnd[0], rangeStart[0]) print(maxComb - minComb + 1)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN 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 LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
from sys import stdin t = int(stdin.readline()) for _ in range(t): n, m = map(int, stdin.readline().split()) s = stdin.readline()[:-1] maxt = 0 mint = 0 maxil = [0] * (n + 1) minil = [0] * (n + 1) val = [0] * (n + 1) x = 0 for i in range(n): if s[i] == "+": x += 1 else: x -= 1 val[i + 1] = x maxt = max(maxt, x) mint = min(mint, x) maxil[i + 1] = maxt minil[i + 1] = mint maxt = 0 mint = 0 maxir = [0] * (n + 1) minir = [0] * (n + 1) var = [0] * (n + 1) x = 0 for i in range(n - 1, -1, -1): if s[i] == "+": x -= 1 else: x += 1 var[i] = x maxt = max(maxt, x) mint = min(mint, x) maxir[i] = maxt minir[i] = mint for i in range(m): l, r = map(int, stdin.readline().split()) lrange = min(minil[l - 1], val[l - 1] - (var[r] - minir[r])) rrange = max(maxil[l - 1], val[l - 1] + (maxir[r] - var[r])) print(rrange - lrange + 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 NUMBER ASSIGN VAR NUMBER ASSIGN 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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN 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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
import sys input = sys.stdin.readline for z in range(int(input())): n, m = map(int, input().split()) s = input() a = [0] for i in range(n): if s[i] == "+": a.append(a[-1] + 1) else: a.append(a[-1] - 1) nminl = [0] nmaxl = [0] nminr = [a[-1]] * (n + 1) nmaxr = [a[-1]] * (n + 1) for i in range(1, n + 1): nminl.append(min(nminl[-1], a[i])) nmaxl.append(max(nmaxl[-1], a[i])) for i in range(n - 1, -1, -1): nminr[i] = min(nminr[i + 1], a[i]) nmaxr[i] = max(nmaxr[i + 1], a[i]) for i in range(m): l, r = map(int, input().split()) num2 = a[l - 1] - a[r] if r != n: ni = min(nminl[l - 1], num2 + nminr[min(n, r + 1)]) na = max(nmaxl[l - 1], num2 + nmaxr[min(n, r + 1)]) else: ni = nminl[l - 1] na = nmaxl[l - 1] print(na - ni + 1)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
from sys import stdin input = stdin.buffer.readline t = int(input()) for i in range(t): n, m = map(int, input().split()) s = list(input()) arr = [] x = 0 for i in range(n): if s[i] == 43: x = x + 1 else: x = x - 1 arr.append(x) minn = 0 maxx = 0 lr = [] for i in range(n): minn = min(minn, arr[i]) maxx = max(maxx, arr[i]) lr.append((minn, maxx)) minn = arr[n - 1] maxx = arr[n - 1] rl = [] for i in range(n - 1, -1, -1): minn = min(minn, arr[i]) maxx = max(maxx, arr[i]) rl.append((minn, maxx)) rl = rl[::-1] q = [] for i in range(m): l, r = map(int, input().split()) q.append((l, r)) for qq in q: l = qq[0] r = qq[1] if l == 1: x = 0, 0 xx = 0 else: x = lr[l - 2] xx = arr[l - 2] y = rl[r - 1] yy = arr[r - 1] zz = xx - yy y = y[0] + zz, y[1] + zz dp = [] dp.append(x) dp.append(y) dp.sort() if dp[0][1] < dp[1][0]: print(dp[0][1] - dp[0][0] + 1 + (dp[1][1] - dp[1][0] + 1)) else: a = min(dp[0][0], dp[1][0]) b = max(dp[0][1], dp[1][1]) print(b - a + 1)
ASSIGN 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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
import sys mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline class SegmentTree: def __init__(self, A, initialize=True, segfunc=min, ident=2000000000): self.N = len(A) self.LV = (self.N - 1).bit_length() self.N0 = 1 << self.LV self.segfunc = segfunc self.ident = ident if initialize: self.data = ( [self.ident] * self.N0 + A + [self.ident] * (self.N0 - self.N) ) for i in range(self.N0 - 1, 0, -1): self.data[i] = segfunc(self.data[i * 2], self.data[i * 2 + 1]) else: self.data = [self.ident] * (self.N0 * 2) def update(self, i, x): i += self.N0 - 1 self.data[i] = x for _ in range(self.LV): i >>= 1 self.data[i] = self.segfunc(self.data[i * 2], self.data[i * 2 + 1]) def get(self, i): return self.data[i + self.N0 - 1] def query(self, l, r): l += self.N0 - 1 r += self.N0 - 1 ret_l = self.ident ret_r = self.ident while l < r: if l & 1: ret_l = self.segfunc(ret_l, self.data[l]) l += 1 if r & 1: ret_r = self.segfunc(self.data[r - 1], ret_r) r -= 1 l >>= 1 r >>= 1 return self.segfunc(ret_l, ret_r) def binsearch(self, l, r, check): if not check(self.query(l, r)): return r l += self.N0 - 1 val = self.ident while True: if check(self.segfunc(val, self.data[l])): break if l & 1: val = self.segfunc(val, self.data[l]) l += 1 l >>= 1 while l < self.N0: newval = self.segfunc(val, self.data[l * 2]) if not check(newval): val = newval l = (l << 1) + 1 else: l <<= 1 return l - self.N0 + 1 for _ in range(int(input())): N, M = map(int, input().split()) S = input().rstrip("\n") cs = [0] * (N + 1) for i, s in enumerate(S): if s == "+": cs[i + 1] = cs[i] + 1 else: cs[i + 1] = cs[i] - 1 ST_min = SegmentTree(cs) ST_max = SegmentTree(cs, segfunc=max, ident=-2 * 10**9) for _ in range(M): l, r = map(int, input().split()) x = cs[l - 1] mi = min(ST_min.query(1, l + 1), x + ST_min.query(r + 1, N + 2) - cs[r]) ma = max(ST_max.query(1, l + 1), x + ST_max.query(r + 1, N + 2) - cs[r]) print(ma - mi + 1) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST VAR VAR VAR BIN_OP LIST VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER 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 FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
import sys input = sys.stdin.readline for _ in range(int(input())): n, m = list(map(int, input().split())) s = input() sums = [0] * (n + 1) pref_mins = [0] * (n + 1) pref_maxs = [0] * (n + 1) for i in range(n): sums[i + 1] = sums[i] + (1 if s[i] == "+" else -1) pref_mins[i + 1] = min(pref_mins[i], sums[i + 1]) pref_maxs[i + 1] = max(pref_maxs[i], sums[i + 1]) suf_mins = [0] * (n + 1) suf_maxs = [0] * (n + 1) suf_maxs[n] = sums[n] suf_mins[n] = sums[n] for i in reversed(range(n)): suf_maxs[i] = max(sums[i], suf_maxs[i + 1]) suf_mins[i] = min(sums[i], suf_mins[i + 1]) for i in range(m): l, r = list(map(int, input().split())) sum = sums[r] - sums[l - 1] ans = ( max(pref_maxs[l - 1], suf_maxs[r] - sum) - min(pref_mins[l - 1], suf_mins[r] - sum) + 1 ) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR 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 VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
import sys for tc in range(int(input())): n, m = map(int, input().split()) p = input() pp = p[::-1] a = [[0, 0, 0] for i in range(n + 1)] ap = [[0, 0, 0] for i in range(n + 1)] x = 0 mxx = 0 mnx = 0 for i in range(n): if p[i] == "+": x += 1 mxx = max(mxx, x) else: x -= 1 mnx = min(mnx, x) a[i + 1][0] = x a[i + 1][1] = mxx a[i + 1][2] = mnx xp = 0 mnxp = 0 mxxp = 0 for i in range(n): if pp[i] == "+": xp -= 1 mnxp = min(mnxp, xp) else: xp += 1 mxxp = max(mxxp, xp) ap[i + 1][0] = xp ap[i + 1][1] = mxxp ap[i + 1][2] = mnxp for i in range(m): l, r = map(int, sys.stdin.readline().strip().split()) xs = a[l - 1][0] mxs = a[l - 1][1] mns = a[l - 1][2] r = n - r xr = ap[r][0] mxr = ap[r][1] mnr = ap[r][2] d = xs - xr mxr += d mnr += d mx = max(mxs, mxr) mn = min(mns, mnr) print(mx - mn + 1)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
import sys input = sys.stdin.readline class SegTree(object): def __init__(self, n, arr): self.n = n self.arr = arr self.tree = [[0, 0, 0] for i in range(2 * n)] def construct(self): for i in range(self.n): if self.arr[i] == 1: self.tree[self.n + i][1] = self.arr[i] else: self.tree[self.n + i][0] = self.arr[i] self.tree[self.n + i][2] = self.arr[i] for i in range(self.n - 1, 0, -1): self.tree[i] = self.function(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, index, value): start = index + self.n self.tree[start] = value start = start // 2 while start > 0: self.tree[start] = self.function( self.tree[2 * start], self.tree[2 * start + 1] ) start = start // 2 def calc(self, low, high): low += self.n high += self.n ansl = [0, 0, 0] ansr = [0, 0, 0] while low < high: if low % 2: ansl = self.function(ansl, self.tree[low]) low += 1 if high % 2: high -= 1 ansr = self.function(self.tree[high], ansr) low = low // 2 high = high // 2 return self.function(ansl, ansr) def function(self, a, b): return [min(a[0], b[0] + a[2]), max(a[1], b[1] + a[2]), b[2] + a[2]] for nt in range(int(input())): n, m = map(int, input().split()) s = input() arr = [] for i in s: if i == "+": arr.append(1) else: arr.append(-1) tree = SegTree(n, arr) tree.construct() for i in range(m): a, b = map(int, input().split()) r1 = tree.calc(0, a - 1) x = r1[2] r2 = tree.calc(b, n) maxx, minn = x + r2[1], x + r2[0] print(max(r1[1], maxx) - min(minn, r1[0]) + 1)
IMPORT ASSIGN VAR VAR CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN LIST FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR 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 NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
def pref(ins): x = 0 Min = 0 Max = 0 Fin_ = [0] Min_ = [0] Max_ = [0] for I in ins: if I == "-": x -= 1 Min = min(Min, x) else: x += 1 Max = max(Max, x) Fin_.append(x) Min_.append(Min) Max_.append(Max) return Fin_, Min_, Max_ def suff(ins): x = 0 Min = 0 Max = 0 Min_ = [0] Max_ = [0] for I in ins[::-1]: if I == "-": x -= 1 Min -= 1 Max = max(0, Max - 1) else: x += 1 Min = min(0, Min + 1) Max += 1 Min_.append(Min) Max_.append(Max) return Min_, Max_ t = int(input().strip()) answers = "" for _ in range(t): n, m = map(int, input().strip().split()) A = input().strip() f_1, m_1, M_1 = pref(A) m_2, M_2 = suff(A) for __ in range(m): l, r = map(int, input().strip().split()) Min = min(m_1[l - 1], f_1[l - 1] + m_2[n - r]) Max = max(M_1[l - 1], f_1[l - 1] + M_2[n - r]) answers += str(Max - Min + 1) + "\n" print(answers)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") for _ in range(int(input())): n, m = map(int, input().split()) op = input() pre = [[0, 0, 0]] suf = [[0, 0, 0]] out = 0 for i in op: if i == "-": out -= 1 else: out += 1 pre.append([out, min(pre[-1][1], out), max(pre[-1][2], out)]) out = 0 for i in reversed(op): if i == "+": out -= 1 else: out += 1 suf.append([out, min(suf[-1][1], out), max(suf[-1][2], out)]) for i in range(m): l, r = map(int, input().split()) l -= 1 minpre = pre[l][1] maxpre = pre[l][2] minsuf = -suf[n - r][0] + suf[n - r][1] + pre[l][0] maxsuf = -suf[n - r][0] + suf[n - r][2] + pre[l][0] print(max(maxpre, maxsuf) - min(minpre, minsuf) + 1)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
import sys T = int(input()) for t in range(T): N, Q = map(int, sys.stdin.readline().strip().split()) s = sys.stdin.readline() val = [] now = 0 for i in range(N): if s[i] == "+": now += 1 else: now -= 1 val.append(now) suffix_min, suffix_max = [None] * N, [None] * N suffix_max[-1] = suffix_min[-1] = val[-1] for i in range(N - 2, -1, -1): suffix_max[i] = max(suffix_max[i + 1], val[i]) suffix_min[i] = min(suffix_min[i + 1], val[i]) prefix_min, prefix_max = [min(val[0], 0)] * N, [max(0, val[0])] * N for i in range(1, N): prefix_min[i] = min(prefix_min[i - 1], val[i]) prefix_max[i] = max(prefix_max[i - 1], val[i]) for q in range(Q): q_l, q_r = map(int, sys.stdin.readline().strip().split()) q_l -= 1 q_r -= 1 sum_l_r = val[q_r] - (0 if q_l == 0 else val[q_l - 1]) l_min = 0 if q_l == 0 else prefix_min[q_l - 1] l_max = 0 if q_l == 0 else prefix_max[q_l - 1] r_min = 0 if q_r == N - 1 else suffix_min[q_r + 1] - sum_l_r r_max = 0 if q_r == N - 1 else suffix_max[q_r + 1] - sum_l_r maks = max(r_max, l_max) mini = min(r_min, l_min) sys.stdout.write(str(maks - mini + 1) + "\n")
IMPORT 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NONE VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP LIST FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP LIST FUNC_CALL VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
import sys input = sys.stdin.readline def query(l, r): if r == n: alpha = 0 else: alpha = ls[l - 1] - ls[r] a, b = ls1[l - 1] c, d = ls2[r + 1] c, d = c + alpha, d + alpha print(max(a, b, c, d) - min(a, b, c, d) + 1) return for _ in range(int(input())): n, m = map(int, input().split()) string = input()[:-1] ls1 = [(0, 0) for _ in range(n + 2)] ls2 = [(0, 0) for _ in range(n + 2)] ls = [0] * (n + 2) for i in range(1, n + 1): ls[i] = ls[i - 1] if string[i - 1] == "+": ls[i] += 1 else: ls[i] -= 1 min_ = min(ls[i], ls1[i - 1][0]) max_ = max(ls[i], ls1[i - 1][1]) ls1[i] = min_, max_ ls2[n] = ls[n], ls[n] for i in reversed(range(0, n)): min_ = min(ls[i], ls2[i + 1][0]) max_ = max(ls[i], ls2[i + 1][1]) ls2[i] = min_, max_ for __ in range(m): l, r = map(int, input().split()) query(l, r)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN 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 NUMBER ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
You are given a program that consists of $n$ instructions. Initially a single variable $x$ is assigned to $0$. Afterwards, the instructions are of two types: increase $x$ by $1$; decrease $x$ by $1$. You are given $m$ queries of the following format: query $l$ $r$ — how many distinct values is $x$ assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases. Then the description of $t$ testcases follows. The first line of each testcase contains two integers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^5$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $n$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $m$ lines contains two integers $l$ and $r$ ($1 \le l \le r \le n$) — the description of the query. The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase print $m$ integers — for each query $l$, $r$ print the number of distinct values variable $x$ is assigned to if all the instructions between the $l$-th one and the $r$-th one inclusive are ignored and the rest are executed without changing the order. -----Examples----- Input 2 8 4 -+--+--+ 1 8 2 8 2 5 1 1 4 10 +-++ 1 1 1 2 2 2 1 3 2 3 3 3 1 4 2 4 3 4 4 4 Output 1 2 4 4 3 3 4 2 3 2 1 2 2 2 -----Note----- The instructions that remain for each query of the first testcase are: empty program — $x$ was only equal to $0$; "-" — $x$ had values $0$ and $-1$; "---+" — $x$ had values $0$, $-1$, $-2$, $-3$, $-2$ — there are $4$ distinct values among them; "+--+--+" — the distinct values are $1$, $0$, $-1$, $-2$.
from sys import stdin, stdout def get(): return stdin.readline().strip() def getf(): return [int(i) for i in get().split()] def put(a, end="\n"): stdout.write(str(a) + end) def putf(a, sep=" ", end="\n"): stdout.write(sep.join([str(i) for i in a]) + end) def intr(a, b, c, d): if c <= a and b <= d: return max(0, b - a + 1) if a <= c and d <= b: return max(0, d - c + 1) if a <= c: return max(0, b - c + 1) return max(0, d - a + 1) def main(): q = int(get()) for i0 in range(q): n, q = getf() d = {"+": 1, "-": -1} s = get() a = [d[i] for i in s] p = [0] for i in range(n): p += [p[i] + a[i]] px = [p[0]] + [0] * n pn = [p[0]] + [0] * n pxr = [0] * n + [p[n]] pnr = [0] * n + [p[n]] for i in range(1, n + 1): if p[i] > px[i - 1]: px[i] = p[i] else: px[i] = px[i - 1] if p[i] < pn[i - 1]: pn[i] = p[i] else: pn[i] = pn[i - 1] for i in range(n - 1, -1, -1): if p[i] > pxr[i + 1]: pxr[i] = p[i] else: pxr[i] = pxr[i + 1] if p[i] < pnr[i + 1]: pnr[i] = p[i] else: pnr[i] = pnr[i + 1] for i in range(q): x, y = getf() x -= 1 if x == 0 and y == n: put(1) elif y == n: u1, d1 = px[x], pn[x] put(u1 - d1 + 1) elif x == 0: u2, d2 = pxr[y], pnr[y] put(u2 - d2 + 1) else: u1, d1 = px[x], pn[x] k = p[y] - p[x] u2, d2 = pxr[y] - k, pnr[y] - k d = intr(d1, u1, d2, u2) put(u1 - d1 + 1 + u2 - d2 + 1 - d) main()
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF STRING STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR VAR LIST BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): rows = len(matrix) cols = len(matrix[0]) dp = [([0] * cols) for _ in range(rows)] for i in range(0, rows): for j in range(0, cols): if i == 0 and j == 0: dp[i][j] = 1 elif i == 0: if matrix[i][j - 1] != 2: dp[i][j] = dp[i][j - 1] elif j == 0: if matrix[i - 1][j] != 1: dp[i][j] = dp[i - 1][j] else: if matrix[i - 1][j] != 1: dp[i][j] += dp[i - 1][j] if matrix[i][j - 1] != 2: dp[i][j] += dp[i][j - 1] path = dp[rows - 1][cols - 1] % 1000000007 dp = [([0] * cols) for _ in range(rows)] for i in range(0, rows): for j in range(0, cols): if i == 0 and j == 0: dp[i][j] = matrix[i][j] elif i == 0: if matrix[i][j - 1] != 2 and dp[i][j - 1] != 0: dp[i][j] = dp[i][j - 1] + matrix[i][j] elif j == 0: if matrix[i - 1][j] != 1 and dp[i - 1][j] != 0: dp[i][j] = dp[i - 1][j] + matrix[i][j] else: if matrix[i - 1][j] != 1 and dp[i - 1][j] != 0: dp[i][j] = dp[i - 1][j] + matrix[i][j] if matrix[i][j - 1] != 2 and dp[i][j - 1] != 0: dp[i][j] = max(dp[i][j - 1] + matrix[i][j], dp[i][j]) adv = dp[rows - 1][cols - 1] return [path, adv]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN LIST VAR VAR
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): n = len(matrix) dp = [[[0, 0] for i in range(n)] for j in range(n)] for i in range(n - 1, -1, -1): for j in range(n - 1, -1, -1): if i == n - 1 and j == n - 1: dp[i][j][0] = 0 dp[i][j][1] = 1 elif i == n - 1: if matrix[i][j] == 1 or matrix[i][j] == 3: dp[i][j][0] = max(dp[i][j][0], dp[i][j + 1][0]) dp[i][j][1] = dp[i][j + 1][1] elif matrix[i][j] == 2: dp[i][j][0] = 0 dp[i][j][1] = 0 elif j == n - 1: if matrix[i][j] == 2 or matrix[i][j] == 3: dp[i][j][0] = max(dp[i][j][0], dp[i + 1][j][0]) dp[i][j][1] = dp[i + 1][j][1] elif matrix[i][j] == 1: dp[i][j][0] = 0 dp[i][j][1] = 0 elif matrix[i][j] == 1: dp[i][j][0] = dp[i][j + 1][0] dp[i][j][1] = dp[i][j + 1][1] elif matrix[i][j] == 2: dp[i][j][0] = dp[i + 1][j][0] dp[i][j][1] = dp[i + 1][j][1] else: dp[i][j][0] = max(dp[i + 1][j][0], dp[i][j + 1][0]) dp[i][j][1] = dp[i + 1][j][1] + dp[i][j + 1][1] if dp[i][j][1] > 0: dp[i][j][1] %= 1000000007 dp[i][j][0] += matrix[i][j] if dp[0][0][1] == 0: return [0, 0] return [dp[0][0][1] % 1000000007, dp[0][0][0]]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER NUMBER NUMBER NUMBER RETURN LIST NUMBER NUMBER RETURN LIST BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): dp = [[[0, 0] for _ in range(len(matrix))] for _ in range(len(matrix))] dp[0][0] = [1, 0] for i in range(len(matrix)): for j in range(len(matrix)): if i > 0 and matrix[i - 1][j] in (2, 3): dp[i][j][0] += dp[i - 1][j][0] dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][1]) if j > 0 and matrix[i][j - 1] in (1, 3): dp[i][j][0] += dp[i][j - 1][0] dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]) if dp[i][j][0] > 0: dp[i][j][0] %= 1000000007 dp[i][j][1] += matrix[i][j] return dp[-1][-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): n = len(matrix) dp = [[(0, matrix[i][j]) for j in range(n)] for i in range(n)] mod = 10**9 + 7 dp[0][0] = 1, matrix[0][0] for i in range(n): for j in range(n): if i - 1 >= 0 and (matrix[i - 1][j] == 3 or matrix[i - 1][j] == 2): if dp[i - 1][j][0] > 0: dp[i][j] = dp[i - 1][j][0] % mod, dp[i - 1][j][1] + dp[i][j][1] if j - 1 >= 0 and (matrix[i][j - 1] == 3 or matrix[i][j - 1] == 1): first = (dp[i][j][0] + dp[i][j - 1][0]) % mod sec = dp[i][j][1] if dp[i][j - 1][0] > 0: sec = max(dp[i][j - 1][1] + matrix[i][j], dp[i][j][1]) % mod dp[i][j] = first, sec ans = [] last = dp[n - 1][n - 1] ans.append(last[0] % mod) if last[0] > 0: ans.append(last[1]) else: ans.append(0) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): n, m = len(matrix), len(matrix[0]) dp = [[[0, 0] for _ in range(m)] for i in range(n)] dp[0][0] = [1, matrix[0][0]] MOD = 10**9 + 7 for i in range(n): for j in range(m): if j - 1 >= 0 and matrix[i][j - 1] in [1, 3] and dp[i][j - 1][0] != 0: dp[i][j][0] += dp[i][j - 1][0] dp[i][j][0] %= MOD dp[i][j][1] = max(dp[i][j][1], matrix[i][j] + dp[i][j - 1][1]) if i - 1 >= 0 and matrix[i - 1][j] in [2, 3] and dp[i - 1][j][0] != 0: dp[i][j][0] += dp[i - 1][j][0] dp[i][j][0] %= MOD dp[i][j][1] = max(dp[i][j][1], matrix[i][j] + dp[i - 1][j][1]) return dp[-1][-1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER LIST NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR LIST NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def cal_max_path_sum(self, matrix, dp): ans = 0 for i in range(n - 1, -1, -1): for j in range(n - 1, -1, -1): if i == n - 1 and j == n - 1: continue if matrix[i][j] == 1: a = dp[i][j + 1] if a[1] == 0: dp[i][j] = [dp[i][j + 1][0], 0] else: dp[i][j] = [a[0], matrix[i][j] + a[1]] elif matrix[i][j] == 2: a = dp[i + 1][j] if a[1] == 0: dp[i][j] = [dp[i][j][0], 0] else: dp[i][j] = [a[0], matrix[i][j] + a[1]] else: a = dp[i][j + 1] b = dp[i + 1][j] if a[1] > b[1]: dp[i][j] = [a[0] + b[0], matrix[i][j] + a[1]] elif a[1] == 0 and b[1] == 0: dp[i][j] = [a[0] + b[0], 0] else: dp[i][j] = [a[0] + b[0], matrix[i][j] + b[1]] return dp[0][0] def FindWays(self, matrix): mod = 10**9 + 7 dp = [] for i in range(n + 1): d = [[0, 0] for j in range(n + 1)] dp.append(d) dp[n - 1][n - 1] = [1, matrix[n - 1][n - 1]] ans = self.cal_max_path_sum(matrix, dp) if ans[0] == 0: return [0, 0] return [ans[0] % mod, ans[1] % mod]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR LIST VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER RETURN LIST NUMBER NUMBER RETURN LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Pair: def __init__(self, path, value): self.path = path self.value = value class Solution: def FindWays(self, matrix): n = len(matrix) mod = 10**9 + 7 dp = [[Pair(0, 0) for i in range(n)] for j in range(n)] for i in range(n): for j in range(n): if i == 0 and j == 0: dp[i][j].path = 1 dp[i][j].value = matrix[i][j] elif i == 0 and j > 0: if matrix[i][j - 1] != 2 and dp[i][j - 1].path != 0: dp[i][j].path = dp[i][j - 1].path dp[i][j].value = dp[i][j - 1].value + matrix[i][j] elif j == 0 and i > 0: if matrix[i - 1][j] != 1 and dp[i - 1][j].path != 0: dp[i][j].path = dp[i - 1][j].path dp[i][j].value = dp[i - 1][j].value + matrix[i][j] else: continue else: if matrix[i - 1][j] != 1 and dp[i - 1][j].path != 0: dp[i][j].path = dp[i - 1][j].path + dp[i][j].path dp[i][j].value = dp[i - 1][j].value + matrix[i][j] if matrix[i][j - 1] != 2 and dp[i][j - 1].path != 0: dp[i][j].path += dp[i][j - 1].path dp[i][j].value = max( dp[i][j].value, dp[i][j - 1].value + matrix[i][j] ) return [dp[n - 1][n - 1].path % mod, dp[n - 1][n - 1].value]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN LIST BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): rows = len(matrix) columns = len(matrix[0]) ways = [([0] * (columns + 1)) for _ in range(rows + 1)] lengths = [([0] * (columns + 1)) for _ in range(rows + 1)] matrix2 = [[0] * (columns + 1)] matrix2 += [([0] + matrix[r]) for r in range(rows)] matrix2[0][1] = 2 ways[0][1] = 1 for y in range(1, rows + 1): for x in range(1, columns + 1): if matrix2[y][x - 1] == 1 or matrix2[y][x - 1] == 3: ways[y][x] = ways[y][x - 1] if ways[y][x - 1]: lengths[y][x] = lengths[y][x - 1] + matrix2[y][x] if matrix2[y - 1][x] == 2 or matrix2[y - 1][x] == 3: ways[y][x] += ways[y - 1][x] if ways[y - 1][x]: lengths[y][x] = max( lengths[y][x], lengths[y - 1][x] + matrix2[y][x] ) return ways[-1][-1] % (10**9 + 7), lengths[-1][-1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR BIN_OP LIST NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
import sys sys.setrecursionlimit(10**9) class Solution: def ctPath(self, i, j, n, m, matrix, dp): if i >= n or j >= m: return 0 if i == n - 1 and j == m - 1: return 1 if dp[i][j] != -1: return dp[i][j] right, down, right_fin, down_fin = 0, 0, 0, 0 if matrix[i][j] == 1: right = self.ctPath(i, j + 1, n, m, matrix, dp) elif matrix[i][j] == 2: down = self.ctPath(i + 1, j, n, m, matrix, dp) elif matrix[i][j] == 3: right_fin = self.ctPath(i, j + 1, n, m, matrix, dp) down_fin = self.ctPath(i + 1, j, n, m, matrix, dp) dp[i][j] = right + down + right_fin + down_fin return dp[i][j] def maxPath(self, i, j, n, m, matrix, dp): if i >= n or j >= m: return -999999999 if i == n - 1 and j == m - 1: return matrix[n - 1][m - 1] if dp[i][j] != -1: return dp[i][j] right, down, right_fin, down_fin = ( -999999999, -999999999, -999999999, -999999999, ) if matrix[i][j] == 1: right = 1 + self.maxPath(i, j + 1, n, m, matrix, dp) elif matrix[i][j] == 2: down = 2 + self.maxPath(i + 1, j, n, m, matrix, dp) elif matrix[i][j] == 3: right_fin = 3 + self.maxPath(i, j + 1, n, m, matrix, dp) down_fin = 3 + self.maxPath(i + 1, j, n, m, matrix, dp) dp[i][j] = max(right, down, right_fin, down_fin) return dp[i][j] def FindWays(self, matrix): mod = 10**9 + 7 n = len(matrix) m = len(matrix[0]) dp = [[(-1) for i in range(m + 1)] for j in range(n + 1)] maxi = self.maxPath(0, 0, n, m, matrix, dp) dp = [[(-1) for i in range(m + 1)] for j in range(n + 1)] ct = self.ctPath(0, 0, n, m, matrix, dp) if maxi < 0: maxi = 0 return [ct % mod, maxi % mod]
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN LIST BIN_OP VAR VAR BIN_OP VAR VAR
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): m, n = len(matrix), len(matrix[0]) dp = [([0] * n) for _ in range(m)] M = 1000000007 for i in range(m): for j in range(n): num = 1 if (i, j) == (0, 0) else 0 value = 0 if i > 0 and matrix[i - 1][j] in [2, 3]: value = max(value, dp[i - 1][j][1]) num += dp[i - 1][j][0] if j > 0 and matrix[i][j - 1] in [1, 3]: value = max(value, dp[i][j - 1][1]) num += dp[i][j - 1][0] if value == 0 and (i, j) != (0, 0): dp[i][j] = num, 0 else: dp[i][j] = num % M, (value + matrix[i][j]) % M return dp[m - 1][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): n = len(matrix) m = len(matrix[0]) path = [[(0) for j in range(m)] for i in range(n)] adv = [[(0) for j in range(m)] for i in range(n)] path[0][0] = 1 adv[0][0] = matrix[0][0] for j in range(1, m): if path[0][j - 1] == 0: path[0][j] = 0 adv[0][j] = 0 elif path[0][j - 1] != 0 and matrix[0][j - 1] in [1, 3]: path[0][j] = 1 adv[0][j] = adv[0][j - 1] + matrix[0][j] for i in range(1, n): if path[i - 1][0] == 0: path[i][0] = 0 adv[i][0] = 0 elif path[i - 1][0] != 0 and matrix[i - 1][0] in [2, 3]: path[i][0] = 1 adv[i][0] = adv[i - 1][0] + matrix[i][0] for i in range(1, n): for j in range(1, m): top, left = 0, 0 if path[i - 1][j] != 0 and matrix[i - 1][j] in [2, 3]: path[i][j] += path[i - 1][j] top = adv[i - 1][j] if path[i][j - 1] != 0 and matrix[i][j - 1] in [1, 3]: path[i][j] += path[i][j - 1] left = adv[i][j - 1] adv[i][j] = max(top, left) if adv[i][j] != 0: adv[i][j] += matrix[i][j] return [path[n - 1][m - 1] % 1000000007, adv[n - 1][m - 1]]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR LIST NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN LIST BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): n = len(matrix) mod = 1000000007 memory = {} def solve(i, j): if i >= n or j >= n: return 0, 0 if i == n - 1 and j == n - 1: return 1, matrix[i][j] if (i, j) in memory: return memory[i, j] right = 0, 0 down = 0, 0 if matrix[i][j] == 1: right = solve(i, j + 1) elif matrix[i][j] == 2: down = solve(i + 1, j) else: down = solve(i + 1, j) right = solve(i, j + 1) x = (right[0] + down[0]) % mod if max(right[1], down[1]) == 0: y = 0 else: y = max(right[1], down[1]) + matrix[i][j] memory[i, j] = x, y return memory[i, j] return solve(0, 0)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, mat): m, n = len(mat), len(mat[0]) dp = [[(0) for i in range(n)] for j in range(m)] for i in range(m - 1, -1, -1): for j in range(n - 1, -1, -1): if i == m - 1 and j == n - 1: dp[i][j] = 1 elif i == m - 1: if mat[i][j] != 2: dp[i][j] = dp[i][j + 1] if dp[i][j]: mat[i][j] += mat[i][j + 1] else: mat[i][j] = 0 elif j == n - 1: if mat[i][j] != 1: dp[i][j] = dp[i + 1][j] if dp[i][j]: mat[i][j] += mat[i + 1][j] else: mat[i][j] = 0 elif mat[i][j] == 1: dp[i][j] = dp[i][j + 1] if dp[i][j]: mat[i][j] += mat[i][j + 1] else: mat[i][j] = 0 elif mat[i][j] == 2: dp[i][j] = dp[i + 1][j] if dp[i][j]: mat[i][j] += mat[i + 1][j] else: mat[i][j] = 0 else: dp[i][j] = dp[i][j + 1] + dp[i + 1][j] if dp[i][j]: mat[i][j] += max(mat[i + 1][j], mat[i][j + 1]) else: mat[i][j] = 0 dp[i][j] = dp[i][j] % (10**9 + 7) return [dp[0][0], mat[0][0]] if __name__ == "__main__": T = int(input()) for i in range(T): n = int(input()) matrix = [] for _ in range(n): cur = list(map(int, input().split())) matrix.append(cur) obj = Solution() ans = obj.FindWays(matrix) for _ in ans: print(_, end=" ") print()
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN LIST VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, l): n = len(l) m = len(l[0]) dp = [[(0) for _ in range(m)] for _ in range(n)] ln = [[(0) for _ in range(m)] for _ in range(n)] ln[0][0] = l[0][0] dp[0][0] = 1 for i in range(1, n): if l[i - 1][0] != 1: dp[i][0] = 1 ln[i][0] = (ln[i - 1][0] + l[i][0]) % 1000000007 else: break for j in range(1, m): if l[0][j - 1] != 2: dp[0][j] = 1 ln[0][j] = (ln[0][j - 1] + l[0][j]) % 1000000007 else: break for i in range(1, n): for j in range(1, m): if l[i - 1][j] != 1 and ln[i - 1][j] != 0: dp[i][j] = dp[i - 1][j] % 1000000007 ln[i][j] = (ln[i - 1][j] + l[i][j]) % 1000000007 if l[i][j - 1] != 2 and ln[i][j - 1] != 0: dp[i][j] = (dp[i][j - 1] + dp[i][j]) % 1000000007 ln[i][j] = max(ln[i][j - 1] + l[i][j], ln[i][j]) % 1000000007 return [dp[-1][-1], ln[-1][-1]]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER RETURN LIST VAR NUMBER NUMBER VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
from itertools import product MOD = int(1000000000.0 + 7) class Solution: def FindWays(self, matrix): m, n = len(matrix), len(matrix[0]) dp = [[(0, 0) for _ in range(n)] for __ in range(m)] dp[0][0] = 1, matrix[0][0] for i in range(1, m): if matrix[i - 1][0] == 1: break val = dp[i - 1][0][1] dp[i][0] = 1, val + matrix[i][0] for j in range(1, n): if matrix[0][j - 1] == 2: break val = dp[0][j - 1][1] dp[0][j] = 1, val + matrix[0][j] for i, j in product(range(1, m), range(1, n)): cnt_top, val_top = dp[i - 1][j] cnt_left, val_left = dp[i][j - 1] cnt, val = 0, 0 if matrix[i - 1][j] != 1 and cnt_top: cnt = cnt_top % MOD val = val_top + matrix[i][j] if matrix[i][j - 1] != 2 and cnt_left: cnt = (cnt + cnt_left) % MOD val = max(val, val_left + matrix[i][j]) dp[i][j] = cnt, val return list(dp[m - 1][n - 1])
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): dp = dict() def rec(ro, co, n, m): if ro == n - 1 and co == m - 1: return 1 if (ro, co) not in dp: ans = 0 if matrix[ro][co] in [1, 3]: if co + 1 < m: ans += rec(ro, co + 1, n, m) if matrix[ro][co] in [2, 3]: if ro + 1 < n: ans += rec(ro + 1, co, n, m) dp[ro, co] = ans % (10**9 + 7) return dp[ro, co] op = rec(0, 0, len(matrix), len(matrix[0])) if op == 0: return [0, 0] dp = dict() def rec2(ro, co, n, m): if ro == n - 1 and co == m - 1: return matrix[-1][-1] if (ro, co) not in dp: ans = -1000000000000.0 if matrix[ro][co] in [1, 3]: if co + 1 < m: ans = max(ans, matrix[ro][co] + rec2(ro, co + 1, n, m)) if matrix[ro][co] in [2, 3]: if ro + 1 < n: ans = max(ans, matrix[ro][co] + rec2(ro + 1, co, n, m)) dp[ro, co] = ans return dp[ro, co] return [op, rec2(0, 0, len(matrix), len(matrix[0])) % (10**9 + 7)]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR LIST NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR LIST NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR LIST NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR LIST NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN LIST VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): dp = [[(0) for i in range(len(matrix[0]))] for i in range(len(matrix))] dp[0][0] = 1 for i in range(1, len(matrix[0])): if (matrix[0][i - 1] == 1 or matrix[0][i - 1] == 3) and dp[0][i - 1] == 1: dp[0][i] = 1 for i in range(1, len(matrix)): if (matrix[i - 1][0] == 2 or matrix[i - 1][0] == 3) and dp[i - 1][0] == 1: dp[i][0] = 1 for i in range(1, len(matrix)): for j in range(1, len(matrix[0])): if matrix[i][j - 1] == 1 or matrix[i][j - 1] == 3: dp[i][j] = dp[i][j] + dp[i][j - 1] if matrix[i - 1][j] == 2 or matrix[i - 1][j] == 3: dp[i][j] = dp[i][j] + dp[i - 1][j] l = [] l.append(dp[len(matrix) - 1][len(matrix[0]) - 1] % (10**9 + 7)) dp = [[(0) for i in range(len(matrix[0]))] for i in range(len(matrix))] dp[0][0] = matrix[0][0] for i in range(1, len(matrix[0])): if (matrix[0][i - 1] == 1 or matrix[0][i - 1] == 3) and dp[0][i - 1] != 0: dp[0][i] = dp[0][i] + matrix[0][i] + dp[0][i - 1] for i in range(1, len(matrix)): if (matrix[i - 1][0] == 2 or matrix[i - 1][0] == 3) and dp[i - 1][0] != 0: dp[i][0] = dp[i][0] + matrix[i][0] + dp[i - 1][0] for i in range(1, len(matrix)): r = 0 g = 0 for j in range(1, len(matrix[0])): if matrix[i][j - 1] == 1 or matrix[i][j - 1] == 3: r = dp[i][j - 1] if matrix[i - 1][j] == 2 or matrix[i - 1][j] == 3: g = dp[i - 1][j] if r != 0 or g != 0: dp[i][j] = dp[i][j] + matrix[i][j] + max(r, g) r = 0 g = 0 l.append(dp[len(matrix) - 1][len(matrix[0]) - 1]) return l
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, arr): n = len(arr) dp = [[[0, 0] for _ in range(n)] for _ in range(n)] m = 1000000007 for i in range(n): for j in range(n): if i == 0 and j == 0: dp[i][j] = [1, arr[i][j]] elif i == 0: if dp[i][j - 1][0] and arr[i][j - 1] != 2: dp[i][j][0] = dp[i][j - 1][0] dp[i][j][1] = dp[i][j - 1][1] + arr[i][j] elif j == 0: if dp[i - 1][j][0] and arr[i - 1][j] != 1: dp[i][j][0] = dp[i - 1][j][0] dp[i][j][1] = dp[i - 1][j][1] + arr[i][j] else: if dp[i - 1][j][0] and arr[i - 1][j] != 1: dp[i][j][0] = dp[i - 1][j][0] dp[i][j][1] = dp[i - 1][j][1] + arr[i][j] if dp[i][j - 1][0] and arr[i][j - 1] != 2: dp[i][j][0] = (dp[i][j][0] + dp[i][j - 1][0]) % m dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1] + arr[i][j]) return dp[-1][-1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER 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 NUMBER VAR NUMBER ASSIGN VAR VAR VAR LIST NUMBER VAR VAR VAR IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR RETURN VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): dp = [([[0, 0]] * len(matrix)) for _ in range(len(matrix))] dp[-1][-1] = [1, matrix[-1][-1]] space = range(len(matrix) - 2, -1, -1) for j in space: if matrix[-1][j] & 1: dp[-1][j] = [dp[-1][j + 1][0], matrix[-1][j] + dp[-1][j + 1][1]] else: dp[-1][j] = [0, 0] for i in space: if matrix[i][-1] & 2: dp[i][-1] = [dp[i + 1][-1][0], matrix[i][-1] + dp[i + 1][-1][1]] else: dp[i][-1] = [0, 0] for i in space: for j in space: r_path, r_val, d_path, d_val = 0, 0, 0, 0 if matrix[i][j] & 1: r_path, r_val = dp[i][j + 1] if matrix[i][j] & 2: d_path, d_val = dp[i + 1][j] total_path = r_path + d_path if total_path: dp[i][j] = [ ( total_path if total_path < 1000000007 else total_path - 1000000007 ), matrix[i][j] + max(r_val, d_val), ] else: dp[i][j] = [0, 0] return dp[0][0]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER LIST NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR LIST NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER LIST VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER LIST NUMBER NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR ASSIGN VAR VAR VAR LIST VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER RETURN VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, m): MOD = int(1000000000.0 + 7) n = len(m) f, g = [([-1e18] * n) for _ in range(n)], [([0] * n) for _ in range(n)] f[0][0] = m[0][0] g[0][0] = 1 for i in range(n): for j in range(n): if m[i][j] & 1 and j + 1 < n: f[i][j + 1] = max(f[i][j + 1], f[i][j] + m[i][j + 1]) g[i][j + 1] = (g[i][j + 1] + g[i][j]) % MOD if m[i][j] & 2 and i + 1 < n: f[i + 1][j] = max(f[i + 1][j], f[i][j] + m[i + 1][j]) g[i + 1][j] = (g[i + 1][j] + g[i][j]) % MOD return g[-1][-1], max(0, f[-1][-1])
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR NUMBER NUMBER FUNC_CALL VAR NUMBER VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): m = len(matrix) n = len(matrix[0]) dp = [[(0) for i in range(n)] for j in range(m)] dp2 = [[(-1) for i in range(n)] for j in range(m)] dp2[0][0] = 1 for i in range(1, m): if matrix[i - 1][0] == 2 or matrix[i - 1][0] == 3: if dp2[i - 1][0] != -1: dp2[i][0] = dp2[i - 1][0] for j in range(1, n): if matrix[0][j - 1] == 1 or matrix[0][j - 1] == 3: if dp2[0][j - 1] != -1: dp2[0][j] = dp2[0][j - 1] for i in range(1, m): for j in range(1, n): l = 0 r = 0 if matrix[i][j - 1] == 1 or matrix[i][j - 1] == 3: if dp2[i][j - 1] != -1: l = dp2[i][j - 1] if matrix[i - 1][j] == 2 or matrix[i - 1][j] == 3: if dp2[i - 1][j] != -1: r = dp2[i - 1][j] if l != 0 or r != 0: dp2[i][j] = l + r dp[0][0] = matrix[0][0] for i in range(1, m): l = 0 if matrix[i - 1][0] == 2 or matrix[i - 1][0] == 3: l = dp[i - 1][0] if l != 0: dp[i][0] = l + matrix[i][0] for j in range(1, n): l = 0 if matrix[0][j - 1] == 1 or matrix[0][j - 1] == 3: l = dp[0][j - 1] if l != 0: dp[0][j] = l + matrix[0][j] for i in range(1, m): for j in range(1, n): l = 0 r = 0 if matrix[i][j - 1] == 1 or matrix[i][j - 1] == 3: l = dp[i][j - 1] if matrix[i - 1][j] == 2 or matrix[i - 1][j] == 3: r = dp[i - 1][j] if l != 0 or r != 0: dp[i][j] = max(l, r) + matrix[i][j] else: dp[i][j] = 0 if dp2[-1][-1] == -1: l = 0 else: l = dp2[-1][-1] % 1000000007 r = dp[-1][-1] return [l, r]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER RETURN LIST VAR VAR
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
def modulo(p): return p % 1000000007 right, down = 1, 2 class Solution: def FindWays(self, maze): n = len(maze) adv = [([0] * n) for _ in range(n)] adv[0][0] = maze[0][0] pts = [([0] * n) for _ in range(n)] pts[0][0] = 1 for j in range(1, n): if maze[0][j - 1] == down: break adv[0][j] = adv[0][j - 1] + maze[0][j] pts[0][j] = 1 for i in range(1, n): if maze[i - 1][0] == right: break adv[i][0] = adv[i - 1][0] + maze[i][0] pts[i][0] = 1 for i in range(1, n): for j in range(1, n): if maze[i - 1][j] != right and pts[i - 1][j]: adv[i][j] = adv[i - 1][j] + maze[i][j] pts[i][j] = pts[i - 1][j] if maze[i][j - 1] != down and pts[i][j - 1]: adv[i][j] = max(adv[i][j], adv[i][j - 1] + maze[i][j]) pts[i][j] += pts[i][j - 1] if not pts[-1][-1]: return 0, 0 return modulo(pts[-1][-1]), adv[-1][-1]
FUNC_DEF RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER RETURN NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): n = len(matrix) mod = 10**9 + 7 def f(i, j, dp): if i < 0 or i >= n or j < 0 or j >= n: return 0 if dp[i][j] != -1: return dp[i][j] if i == n - 1 and j == n - 1: return 1 if matrix[i][j] == 1: dp[i][j] = f(i, j + 1, dp) % mod return dp[i][j] elif matrix[i][j] == 2: dp[i][j] = f(i + 1, j, dp) % mod return dp[i][j] else: dp[i][j] = (f(i, j + 1, dp) + f(i + 1, j, dp)) % mod return dp[i][j] def ff(i, j, pdp): if i < 0 or i >= n or j < 0 or j >= n: return -1000000000 if pdp[i][j] != -1: return pdp[i][j] if i == n - 1 and j == n - 1: return matrix[i][j] if matrix[i][j] == 1: pdp[i][j] = ff(i, j + 1, pdp) + 1 return pdp[i][j] elif matrix[i][j] == 2: pdp[i][j] = ff(i + 1, j, pdp) + 2 return pdp[i][j] else: pdp[i][j] = max(ff(i, j + 1, pdp), ff(i + 1, j, pdp)) + 3 return pdp[i][j] ans = [0] point = [0] dp = [([-1] * n) for i in range(n)] pdp = [([-1] * n) for i in range(n)] fin = f(0, 0, dp) fin2 = ff(0, 0, pdp) if fin == 0: return [0, 0] return [fin, fin2]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR IF VAR NUMBER RETURN LIST NUMBER NUMBER RETURN LIST VAR VAR
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, a): n = len(a) mod = int(1000000000.0 + 7) dp = [[[0, 0] for _ in range(n)] for _ in range(n)] dp[0][0] = [1, 0] for i in range(n): for j in range(n): if i and a[i - 1][j] in (2, 3): dp[i][j][0] += dp[i - 1][j][0] dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][1]) if j and a[i][j - 1] in (1, 3): dp[i][j][0] += dp[i][j - 1][0] dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]) if dp[i][j][0] > 0: dp[i][j][0] %= mod dp[i][j][1] += matrix[i][j] return dp[n - 1][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): cache = [(1, matrix[0][0])] for i in range(1, len(matrix[0])): if matrix[0][i - 1] in (1, 3) and cache[-1] is not None: cache.append((cache[-1][0], matrix[0][i] + cache[-1][1])) else: cache.append(None) for i in range(1, len(matrix)): if matrix[i - 1][0] in (2, 3) and cache[0] is not None: new_cache = [(cache[0][0], matrix[i][0] + cache[0][1])] else: new_cache = [None] for j in range(1, len(matrix[0])): from_top = cache[j] if matrix[i - 1][j] in (2, 3) else None from_left = new_cache[-1] if matrix[i][j - 1] in (1, 3) else None if from_top == from_left == None: new_cache.append(None) elif from_top is None: new_cache.append((from_left[0], from_left[1] + matrix[i][j])) elif from_left is None: new_cache.append((from_top[0], from_top[1] + matrix[i][j])) elif from_top[1] == from_left[1]: new_cache.append( (from_left[0] + from_top[0], from_left[1] + matrix[i][j]) ) elif from_top[1] > from_left[1]: new_cache.append( (from_left[0] + from_top[0], from_top[1] + matrix[i][j]) ) else: new_cache.append( (from_left[0] + from_top[0], from_left[1] + matrix[i][j]) ) cache = new_cache if cache[-1] is None: return 0, 0 return cache[-1][0] % (10**9 + 7), cache[-1][1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NONE EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NONE FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER NONE ASSIGN VAR LIST VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST NONE FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NONE ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NONE IF VAR VAR NONE EXPR FUNC_CALL VAR NONE IF VAR NONE EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER NONE RETURN NUMBER NUMBER RETURN BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
mod = 1000000007 class Solution: def FindWays(self, matrix): le = len(matrix) dp = [[[0, 0] for i in range(le)] for j in range(le)] dp[0][0] = [1, 0] for i in range(le): for j in range(le): if i == 0 and j == 0: dp[i][j][1] = matrix[i][j] elif i == 0: if matrix[i][j - 1] in [1, 3] and dp[i][j - 1][0] != 0: dp[i][j][0] = dp[i][j - 1][0] % mod dp[i][j][1] = (matrix[i][j] + dp[i][j - 1][1]) % mod elif j == 0: if matrix[i - 1][j] in [2, 3] and dp[i - 1][j][0] != 0: dp[i][j][0] = dp[i - 1][j][0] % mod dp[i][j][1] = (matrix[i][j] + dp[i - 1][j][1]) % mod elif ( matrix[i][j - 1] in [1, 3] and matrix[i - 1][j] in [2, 3] and dp[i - 1][j][0] != 0 ): dp[i][j][0] = (dp[i - 1][j][0] + dp[i][j - 1][0]) % mod dp[i][j][1] = ( matrix[i][j] + max(dp[i - 1][j][1], dp[i][j - 1][1]) ) % mod elif matrix[i - 1][j] in [2, 3] and dp[i - 1][j][0] != 0: dp[i][j][0] = dp[i - 1][j][0] % mod dp[i][j][1] = (matrix[i][j] + dp[i - 1][j][1]) % mod elif matrix[i][j - 1] in [1, 3] and dp[i][j - 1][0] != 0: dp[i][j][0] = dp[i][j - 1][0] % mod dp[i][j][1] = (matrix[i][j] + dp[i][j - 1][1]) % mod return dp[-1][-1]
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR LIST NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR LIST NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR LIST NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): m = len(matrix[0]) n = len(matrix) dp = [[[-1, 0] for i in range(m)] for j in range(n)] dp[0][0][0] = 1 dp[0][0][1] = matrix[0][0] for i in range(1, m): if (matrix[0][i - 1] == 1 or matrix[0][i - 1] == 3) and dp[0][i - 1][ 0 ] != -1: dp[0][i][0] = dp[0][i - 1][0] dp[0][i][1] = dp[0][i - 1][1] + matrix[0][i] for j in range(1, n): if (matrix[j - 1][0] == 2 or matrix[j - 1][0] == 3) and dp[j - 1][0][ 0 ] != -1: dp[j][0][0] = dp[j - 1][0][0] dp[j][0][1] = dp[j - 1][0][1] + matrix[j][0] for i in range(1, n): for j in range(1, m): if (matrix[i - 1][j] == 2 or matrix[i - 1][j] == 3) and dp[i - 1][j][ 0 ] != -1: dp[i][j][0] = dp[i - 1][j][0] dp[i][j][1] = dp[i - 1][j][1] + matrix[i][j] if (matrix[i][j - 1] == 1 or matrix[i][j - 1] == 3) and dp[i][j - 1][ 0 ] != -1: dp[i][j][0] = max(dp[i][j][0], 0) dp[i][j][0] += dp[i][j - 1][0] dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1] + matrix[i][j]) dp[n - 1][m - 1][0] = max(dp[n - 1][m - 1][0], 0) dp[n - 1][m - 1][0] = dp[n - 1][m - 1][0] % int(1000000000.0 + 7) return dp[n - 1][m - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
MOD = int(1000000000.0 + 7) def f(A, x, y, N, dp, adv): if x == N - 1 and y == N - 1: adv[x][y] = A[x][y] return 1 if x < 0 or x >= N or y < 0 or y >= N: return 0 if dp[x][y] != -1: return dp[x][y] ans = 0 if A[x][y] == 1: out = f(A, x, y + 1, N, dp, adv) if y + 1 < N and adv[x][y + 1] > 0: adv[x][y] = max(adv[x][y], A[x][y] + adv[x][y + 1]) elif A[x][y] == 2: out = f(A, x + 1, y, N, dp, adv) if x + 1 < N and adv[x + 1][y] > 0: adv[x][y] = max(adv[x][y], A[x][y] + adv[x + 1][y]) else: r = f(A, x, y + 1, N, dp, adv) d = f(A, x + 1, y, N, dp, adv) if y + 1 < N and adv[x][y + 1] > 0: adv[x][y] = max(adv[x][y], A[x][y] + adv[x][y + 1]) if x + 1 < N and adv[x + 1][y] > 0: adv[x][y] = max(adv[x][y], A[x][y] + adv[x + 1][y]) out = r + d ans = out ans %= MOD dp[x][y] = ans return ans class Solution: def FindWays(self, A): N = len(A) dp = [[(-1) for i in range(N)] for i in range(N)] adv = [[(0) for i in range(N)] for i in range(N)] ans = f(A, 0, 0, N, dp, adv) return [ans, adv[0][0]]
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR RETURN LIST VAR VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): n = len(matrix) mod = 10**9 + 7 matrix[0][0] = matrix[0][0], 1, matrix[0][0] for i in range(n - 1): if matrix[0][i][0] == 1 or matrix[0][i][0] == 3: matrix[0][i + 1] = ( matrix[0][i + 1], matrix[0][i][1], matrix[0][i + 1] + matrix[0][i][2], ) else: matrix[0][i + 1] = 0, 0, 0 if matrix[i][0][0] == 2 or matrix[i][0][0] == 3: matrix[i + 1][0] = ( matrix[i + 1][0], matrix[i][0][1], matrix[i + 1][0] + matrix[i][0][2], ) else: matrix[i + 1][0] = 0, 0, 0 for i in range(1, n): for j in range(1, n): value = matrix[i][j] count = 0 summa = 0 if matrix[i - 1][j][0] == 2 or matrix[i - 1][j][0] == 3: count += matrix[i - 1][j][1] summa = matrix[i - 1][j][2] if matrix[i][j - 1][0] == 1 or matrix[i][j - 1][0] == 3: count += matrix[i][j - 1][1] summa = max(summa, matrix[i][j - 1][2]) if count: matrix[i][j] = value, count % mod, (summa + value) % mod else: matrix[i][j] = 0, 0, 0 return ( (matrix[n - 1][n - 1][1], matrix[n - 1][n - 1][2]) if matrix[n - 1][n - 1][1] else (0, 0) )
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
import sys sys.setrecursionlimit(3500) class Solution: def FindWays(self, matrix): MOD = 10**9 + 7 self.mat = matrix self.n = len(matrix) self.m = len(matrix[0]) self.dp = [[[-1, -1] for _ in range(self.m)] for _ in range(self.n)] def dfs(x, y): if self.dp[x][y][0] != -1: return self.dp[x][y] if x == self.n - 1 and y == self.m - 1: self.dp[x][y] = [1, self.mat[x][y]] return self.dp[x][y] elif self.mat[x][y] == 1 and y + 1 < self.m: hl = dfs(x, y + 1) if hl[0] == 0: self.dp[x][y] = [0, 0] return self.dp[x][y] self.dp[x][y] = [hl[0], hl[1] + self.mat[x][y]] return self.dp[x][y] elif self.mat[x][y] == 2 and x + 1 < self.n: hl = dfs(x + 1, y) if hl[0] == 0: self.dp[x][y] = [0, 0] return self.dp[x][y] self.dp[x][y] = [hl[0], self.mat[x][y] + hl[1]] return self.dp[x][y] elif self.mat[x][y] == 3: m1 = m2 = 0 p1 = p2 = 0 if y + 1 < self.m: hl = dfs(x, y + 1) p1 = hl[0] m1 = self.mat[x][y] + hl[1] if x + 1 < self.n: hl = dfs(x + 1, y) p2 = hl[0] m2 = self.mat[x][y] + hl[1] if p1 + p2 == 0: self.dp[x][y] = [0, 0] return self.dp[x][y] self.dp[x][y] = [p1 + p2, max(m1, m2)] return self.dp[x][y] else: self.dp[x][y] = [0, 0] return self.dp[x][y] ans = dfs(0, 0) return [ans[0] % MOD, ans[1] % MOD]
IMPORT EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST NUMBER VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST NUMBER NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST NUMBER NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR LIST NUMBER NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR LIST BIN_OP VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER RETURN LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): dp = [] n = len(matrix) for i in range(n): dp.append([0] * n) dp[0][0] = [1, matrix[0][0]] for i in range(n): for j in range(n): if i == 0 and j == 0: continue val = 0 maxi = 0 if i > 0 and (matrix[i - 1][j] == 2 or matrix[i - 1][j] == 3): val += dp[i - 1][j][0] maxi = max(maxi, dp[i - 1][j][1] + matrix[i][j]) if j > 0 and (matrix[i][j - 1] == 1 or matrix[i][j - 1] == 3): val += dp[i][j - 1][0] maxi = max(maxi, dp[i][j - 1][1] + matrix[i][j]) dp[i][j] = [val, maxi] if val == 0: dp[i][j] = [0, 0] return [dp[n - 1][n - 1][0] % 1000000007, dp[n - 1][n - 1][1]]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER LIST NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR LIST NUMBER NUMBER RETURN LIST BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): n, m = len(matrix), len(matrix[0]) dp = [[[0, cell] for cell in col] for col in matrix] mod = 1000000007 dp[0][0][0] = 1 for i in range(n): for j in range(m): up, down, upsum, downsum = 0, 0, 0, 0 if ( i > 0 and (matrix[i - 1][j] == 2 or matrix[i - 1][j] == 3) and dp[i - 1][j][0] ): up = dp[i - 1][j][0] upsum = dp[i - 1][j][1] if ( j > 0 and (matrix[i][j - 1] == 1 or matrix[i][j - 1] == 3) and dp[i][j - 1][0] ): down = dp[i][j - 1][0] downsum = dp[i][j - 1][1] dp[i][j] = [ max(dp[i][j][0], (up + down) % mod), dp[i][j][1] + max(upsum, downsum) % mod, ] res = [0] * 2 res[0] = dp[n - 1][m - 1][0] % mod res[1] = dp[n - 1][m - 1][1] % mod if dp[n - 1][m - 1][0] != 0 else 0 return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): n = len(matrix) arr = matrix dp = [[(-1) for i in range(n)] for j in range(n)] dp[n - 1][n - 1] = [1, arr[n - 1][n - 1]] for i in range(n - 2, -1, -1): v = arr[n - 1][i] if v == 1: dp[n - 1][i] = dp[n - 1][i + 1][:] if dp[n - 1][i][0] != 0 and dp[n - 1][i][1] != 0: dp[n - 1][i][1] += arr[n - 1][i] elif v == 2: dp[n - 1][i] = [0, 0] else: dp[n - 1][i] = dp[n - 1][i + 1][:] if dp[n - 1][i][0] != 0 and dp[n - 1][i][1] != 0: dp[n - 1][i][1] += arr[n - 1][i] for i in range(n - 2, -1, -1): v = arr[i][n - 1] if v == 1: dp[i][n - 1] = [0, 0] elif v == 2: dp[i][n - 1] = dp[i + 1][n - 1][:] if dp[i][n - 1][0] != 0 and dp[i][n - 1][1] != 0: dp[i][n - 1][1] += arr[i][n - 1] else: dp[i][n - 1] = dp[i + 1][n - 1][:] if dp[i][n - 1][0] != 0 and dp[i][n - 1][1] != 0: dp[i][n - 1][1] += arr[i][n - 1] for i in range(n - 2, -1, -1): for j in range(n - 2, -1, -1): v = arr[i][j] if v == 1: dp[i][j] = dp[i][j + 1][:] if dp[i][j][0] != 0 and dp[i][j][1] != 0: dp[i][j][1] += arr[i][j] elif v == 2: dp[i][j] = dp[i + 1][j][:] if dp[i][j][0] != 0 and dp[i][j][1] != 0: dp[i][j][1] += arr[i][j] else: dp[i][j] = [0, 0] dp[i][j][0] = dp[i + 1][j][0] + dp[i][j + 1][0] dp[i][j][1] = max(dp[i + 1][j][1], dp[i][j + 1][1]) if dp[i][j][0] != 0 and dp[i][j][1] != 0: dp[i][j][1] += arr[i][j] dp[i][j][0] = dp[i][j][0] % (10**9 + 7) return dp[0][0]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR NUMBER NUMBER
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): n = len(matrix) self.paths = 0 dp = [([[0, -1]] * n) for _ in range(n)] dp[-1][-1] = [1, matrix[-1][-1]] path, adventure = self.travelMaze(matrix, dp, 0, 0, n) return [path % int(1000000000.0 + 7), max(0, adventure) % int(1000000000.0 + 7)] def travelMaze(self, maze, dp, i, j, n): if not (0 <= i < n and 0 <= j < n): return [0, float("-inf")] if dp[i][j][1] != -1: return dp[i][j].copy() if maze[i][j] == 1: dp[i][j] = self.travelMaze(maze, dp, i, j + 1, n) elif maze[i][j] == 2: dp[i][j] = self.travelMaze(maze, dp, i + 1, j, n) else: right = self.travelMaze(maze, dp, i, j + 1, n) down = self.travelMaze(maze, dp, i + 1, j, n) dp[i][j] = [right[0] + down[0], max(right[1], down[1])] dp[i][j][1] += maze[i][j] return dp[i][j].copy()
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER LIST NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR RETURN LIST BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER BIN_OP FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF NUMBER VAR VAR NUMBER VAR VAR RETURN LIST NUMBER FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
You have got a maze, which is a n*n Grid. Every cell of the maze contains these numbers 1, 2 or 3. If it contains 1 : means we can go Right from that cell only. If it contains 2 : means we can go Down from that cell only. If it contains 3 : means we can go Right and Down to both paths from that cell. We cant go out of the maze at any time. Initially, You are on the Top Left Corner of The maze(Entry). And, You need to go to the Bottom Right Corner of the Maze(Exit). You need to find the total number of paths from Entry to Exit Point. There may be many paths but you need to select that path which contains the maximum number of Adventure. The Adventure on a path is calculated by taking the sum of all the cell values thatlies in the path. Example 1: Input: matrix = {{1,1,3,2,1},{3,2,2,1,2}, {1,3,3,1,3},{1,2,3,1,2},{1,1,1,3,1}} Output: {4,18} Explanation: There are total 4 Paths Available out of which The Max Adventure is 18. Total possible Adventures are 18,17,17,16. Of these 18 is the maximum. Your Task: You don't need to read or print anything. Your task is to complete the function FindWays() which takes matrix as input parameter and returns a list containg total number of ways to reach at (n, n) modulo 10^{9} + 7 and maximum number of Adventure. Expected Time Complexity: O(n^{2}) Expected Space Complexity: O(n^{2}) Constraints: 1 <= n <= 100
class Solution: def FindWays(self, matrix): mod = 1000000007 n = len(matrix) m = len(matrix[0]) count = [[(0) for i in range(m)] for i in range(n)] flag = 1 if matrix[0][0] == 1: flag = 0 count[0][0] = [1, matrix[0][0]] for i in range(1, n): if flag == 1: count[i][0] = [1, matrix[i][0] + count[i - 1][0][1]] else: count[i][0] = [0, 0] if matrix[i][0] == 1: flag = 0 flag = 1 if matrix[0][0] == 2: flag = 0 for i in range(1, m): if flag == 1: count[0][i] = [1, matrix[0][i] + count[0][i - 1][1]] else: count[0][i] = [0, 0] if matrix[0][i] == 2: flag = 0 for i in range(1, n): for j in range(1, m): count[i][j] = [0, 0] if count[i - 1][j][0] != 0 and ( matrix[i - 1][j] == 3 or matrix[i - 1][j] == 2 ): count[i][j] = [ count[i][j][0] + count[i - 1][j][0], max(count[i][j][1], count[i - 1][j][1] + matrix[i][j]), ] if count[i][j - 1][0] != 0 and ( matrix[i][j - 1] == 1 or matrix[i][j - 1] == 3 ): count[i][j] = [ count[i][j][0] + count[i][j - 1][0], max(count[i][j][1], count[i][j - 1][1] + matrix[i][j]), ] count[i][j][0] = count[i][j][0] % mod count[i][j][1] = count[i][j][1] % mod if count[n - 1][m - 1][0] == 0: return 0, 0 return count[n - 1][m - 1][0] % mod, count[n - 1][m - 1][1] % mod
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER LIST NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER LIST NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER LIST NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR LIST NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR LIST NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxi(self, row): res = [] top, area, maxArea = 0, 0, 0 i = 0 while i < len(row): if len(res) == 0 or row[res[-1]] <= row[i]: res.append(i) i += 1 else: top = row[res.pop()] area = top * i if len(res): area = top * (i - res[-1] - 1) maxArea = max(area, maxArea) while len(res): top = row[res.pop()] area = top * i if len(res): area = top * (i - res[-1] - 1) maxArea = max(area, maxArea) return maxArea def maxArea(self, M, n, m): res = self.maxi(M[0]) for i in range(1, n): for j in range(m): if M[i][j]: M[i][j] += M[i - 1][j] res = max(res, self.maxi(M[i])) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def getMaxArea(self, histogram): n = len(histogram) height = [] maxArea = 0 for i, h in enumerate(histogram): start = i while height and height[-1][1] > h: index, hei = height.pop() maxArea = max(maxArea, hei * (i - index)) start = index height.append((start, h)) for i, h in height: maxArea = max(maxArea, h * (n - i)) return maxArea def maxArea(self, M, n, m): maxi = 0 stack = [] for i in range(n): l = [] for j in range(m): if i == 0: l.append(M[i][j]) elif M[i][j] == 0: l.append(0) else: l.append(stack[i - 1][j] + M[i][j]) maxi = max(maxi, self.getMaxArea(l)) stack.append(l) return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxArea(self, M, n, m): hist = [0] * m max_area = 0 for i in range(n): for j in range(m): if M[i][j] == 0: hist[j] = 0 else: hist[j] += 1 stack = [] j = 0 while j < m: if not stack or hist[stack[-1]] <= hist[j]: stack.append(j) j += 1 else: top = stack.pop() area = hist[top] * (j - stack[-1] - 1 if stack else j) max_area = max(max_area, area) while stack: top = stack.pop() area = hist[top] * (j - stack[-1] - 1 if stack else j) max_area = max(max_area, area) return max_area
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxArea(self, matrix, rows, cols): shiftRow = [0] * cols sol = 0 for row in matrix: for i, ele in enumerate(row): if ele == 1: shiftRow[i] += 1 else: shiftRow[i] = 0 st = [] for i in range(cols + 1): while len(st) > 0 and (i == cols or shiftRow[st[-1]] >= shiftRow[i]): height = shiftRow[st[-1]] st.pop() width = i if len(st) > 0: width = i - st[-1] - 1 sol = max(height * width, sol) st.append(i) return sol
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
def getarea(height): n = len(height) nxt, prev = [n] * n, [-1] * n stack = [] for i in range(n): while stack and height[stack[-1]] >= height[i]: nxt[stack[-1]] = i stack.pop() prev[i] = stack[-1] if stack else -1 stack.append(i) area = -1 for i in range(n): length = height[i] width = nxt[i] - prev[i] - 1 newarea = length * width area = max(area, newarea) return area class Solution: def maxArea(self, M, n, m): height = [0] * m area = -1 for i in range(n): for j in range(m): if M[i][j] == 0: height[j] = 0 else: height[j] += M[i][j] newarea = getarea(height) area = max(area, newarea) return area
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST VAR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxArea(self, M, R, C): def maxHis(arr): n = len(arr) stack = [] nsri = [n for i in range(n)] for i in range(n): while stack and arr[stack[-1]] > arr[i]: nsri[stack.pop()] = i stack.append(i) stack = [] nsli = [(-1) for i in range(n)] for i in range(n - 1, -1, -1): while stack and arr[stack[-1]] > arr[i]: nsli[stack.pop()] = i stack.append(i) width = [] for i in range(n): width.append((nsri[i] - nsli[i] - 1) * arr[i]) return max(width) ans = float("-inf") l = [0] * C for i in range(R): for k in range(C): if M[i][k] == 0: l[k] = 0 else: l[k] += 1 ans = max(ans, maxHis(l.copy())) return ans
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxArea(self, M, n, m): if not n or not m: return 0 dp = [([0] * m) for _ in range(n)] for i in range(n): acc = 0 for j in range(m): if M[i][j] == 1: acc += 1 else: acc = 0 dp[i][j] = acc res = 0 for i in reversed(range(n)): for j in reversed(range(m)): bSide, rSide = dp[i][j], 0 k = i while k > -1 and dp[k][j]: bSide = min(bSide, dp[k][j]) rSide += 1 res = max(res, bSide * rSide) k -= 1 return res
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER 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 VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def calMaxArea(self, heights): n = len(heights) maxi = 0 stack = [] for i, h in enumerate(heights): start = i while stack and stack[-1][1] > h: ind, height = stack.pop() maxi = max(maxi, height * (i - ind)) start = ind stack.append((start, h)) for i, h in stack: maxi = max(maxi, h * (n - i)) return maxi def maxArea(self, M, n, m): hist = M[0] res = self.calMaxArea(hist) for i in range(1, n): for j in range(m): if M[i][j] == 0: hist[j] = 0 else: hist[j] += 1 res = max(res, self.calMaxArea(hist)) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxArea(self, M, n, m): heights = [0] * m max_area = 0 for i in range(n): for j in range(m): if M[i][j] == 0: heights[j] = 0 else: heights[j] += 1 area = self.largestRectangleArea(heights) max_area = max(max_area, area) return max_area def largestRectangleArea(self, heights): stack = [] max_area = 0 for i in range(len(heights)): while stack and heights[i] < heights[stack[-1]]: height = heights[stack.pop()] width = i if not stack else i - stack[-1] - 1 area = height * width max_area = max(max_area, area) stack.append(i) while stack: height = heights[stack.pop()] width = len(heights) if not stack else len(heights) - stack[-1] - 1 area = height * width max_area = max(max_area, area) return max_area
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxrect(self, arr, n): ps = [1] for i in range(1, n): k = i - 1 count = 1 while k >= 0 and arr[i] <= arr[k] and arr[i] != 0: count += ps[k] k -= ps[k] ps.append(count) ns = [1] * n for i in range(n - 2, -1, -1): k = i + 1 count = 1 while k < n and arr[i] <= arr[k] and arr[i] != 0: count += ns[k] k += ns[k] ns[i] = count maxi = 0 for i in range(n): maxi = max(maxi, (abs(ns[i] + ps[i]) - 1) * arr[i]) return maxi def maxArea(self, M, n, m): arr = [0] * m maxi = 0 for i in range(n): for j in range(m): if M[i][j] == 1: arr[j] += 1 else: arr[j] = 0 maxi = max(maxi, self.maxrect(arr, m)) return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def largestRect(self, a): n = len(a) stack = [] maxArea = 0 i = 0 while i < n: if not stack or a[stack[-1]] < a[i]: stack.append(i) i += 1 else: t = a[stack.pop()] area = t * (i - stack[-1] - 1) if stack else t * i maxArea = max(area, maxArea) while stack: t = a[stack.pop()] area = t * (i - stack[-1] - 1) if stack else t * i maxArea = max(area, maxArea) return maxArea def maxArea(self, a, n, m): maxArea = self.largestRect(a[0]) for i in range(1, n): for j in range(m): if a[i][j] == 1: a[i][j] += a[i - 1][j] area = self.largestRect(a[i]) maxArea = max(area, maxArea) return maxArea
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxArea(self, M, n, m): def maxhistrogram(height): st = [] res = 0 for i in range(len(height)): while st and height[st[-1]] >= height[i]: tp = st[-1] st.pop() curr_width = i - st[-1] - 1 if st else i res = max(res, curr_width * height[tp]) st.append(i) while st: tp = st[-1] st.pop() curr_width = len(height) - st[-1] - 1 if st else len(height) res = max(res, curr_width * height[tp]) return res res = maxhistrogram(M[0]) for i in range(1, len(M)): for j in range(len(M[i])): if M[i][j]: M[i][j] += M[i - 1][j] res = max(res, maxhistrogram(M[i])) return res
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxArea(self, M, n, m): ans = 0 for j in range(n): if j == 0: histogram = M[0] else: for k in range(m): if M[j][k] == 0: histogram[k] = 0 else: histogram[k] += 1 s, res, n, curr = [], 0, m, 0 for i in range(n): while len(s) != 0 and histogram[s[-1]] >= histogram[i]: temp = s.pop() if len(s) == 0: curr = histogram[temp] * i else: curr = histogram[temp] * (i - s[-1] - 1) res = max(res, curr) s.append(i) while len(s) != 0: temp = s.pop() if len(s) == 0: curr = histogram[temp] * n else: curr = histogram[temp] * (n - s[-1] - 1) res = max(res, curr) ans = max(ans, res) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def mah(self, a, n, x=0): freq = [0] * n stack = [] for i in range(n - 1, -1, -1): while stack != [] and a[stack[-1]] >= a[i]: stack.pop() if stack == []: freq[i] = n stack.append(i) continue freq[i] = stack[-1] stack.append(i) stack = [] for i in range(n): while stack != [] and a[stack[-1]] >= a[i]: stack.pop() if stack == []: freq[i] = freq[i] * a[i] stack.append(i) continue freq[i] = (freq[i] - stack[-1] - 1) * a[i] stack.append(i) return max(freq) def maxArea(self, M, n, m): for i in range(n): if i == 0: res = self.mah(M[i], m) ans = M[i] continue for j in range(m): if M[i][j] == 0: ans[j] = 0 continue ans[j] += M[i][j] res = max(res, self.mah(ans, m)) return res
CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR LIST VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR LIST ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR LIST VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR LIST ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxArea(self, M, n, m): def largestRectangleArea(heights): stack = [] max_area = 0 i = 0 while i < len(heights): if not stack or heights[i] >= heights[stack[-1]]: stack.append(i) i += 1 else: top = stack.pop() area = heights[top] * (i - stack[-1] - 1 if stack else i) max_area = max(max_area, area) while stack: top = stack.pop() area = heights[top] * (i - stack[-1] - 1 if stack else i) max_area = max(max_area, area) return max_area def maxRectangleArea(M): n = len(M) m = len(M[0]) histogram = [0] * m max_area = 0 for i in range(n): for j in range(m): if M[i][j] == 0: histogram[j] = 0 else: histogram[j] += 1 area = largestRectangleArea(histogram) max_area = max(max_area, area) return max_area return maxRectangleArea(M)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxArea(self, matrix, n, m): for row in range(len(matrix)): matrix[row].append("0") height = [0] * len(matrix[0]) max_rectangle = 0 for row in matrix: stack = [-1] for col, val in enumerate(row): height[col] = (height[col] + 1) * int(val) while height[stack[-1]] > height[col]: prev_col = stack.pop() left = prev_col - stack[-1] - 1 right = col - prev_col width = left + right area = width * height[prev_col] max_rectangle = max(max_rectangle, area) stack.append(col) return max_rectangle
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxArea(self, M, n, m): arr = M[0] res = -1 res = max(res, getmaxarea(arr)) for i in range(1, n): for j in range(m): if M[i][j] == 0: arr[j] = 0 else: arr[j] = arr[j] + M[i][j] res = max(res, getmaxarea(arr)) return res def getmaxarea(h): h.append(0) stack = [] area = 0 i = 0 while i < len(h): if not stack or h[stack[-1]] < h[i]: stack.append(i) i += 1 else: top = stack.pop() area = max(area, h[top] * (i - stack[-1] - 1 if stack else i)) return area
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR RETURN VAR
Given a binary matrix M of size n X m. Find the maximum area of a rectangle formed only of 1s in the given matrix. Example 1: Input: n = 4, m = 4 M[][] = {{0 1 1 0}, {1 1 1 1}, {1 1 1 1}, {1 1 0 0}} Output: 8 Explanation: For the above test case the matrix will look like 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 the max size rectangle is 1 1 1 1 1 1 1 1 and area is 4 *2 = 8. Your Task: Your task is to complete the function maxArea which returns the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Expected Time Complexity : O(n*m) Expected Auixiliary Space : O(m) Constraints: 1<=n,m<=1000 0<=M[][]<=1 Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
class Solution: def maxArea(self, matrix, n, m): m = len(matrix) n = len(matrix[0]) dp = [(0) for _ in range(n)] def max_histogram(heights): stk = [] max_area = 0 for i, el in enumerate(heights + [0]): while stk and heights[stk[-1]] > el: H = heights[stk.pop()] if not stk: W = i else: W = i - stk[-1] - 1 max_area = max(max_area, int(H * W)) stk.append(i) return max_area res = 0 for i in range(m): for j in range(n): if matrix[i][j] == 0: dp[j] = 0 else: dp[j] = dp[j] + matrix[i][j] res = max(res, max_histogram(dp)) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR BIN_OP VAR LIST NUMBER WHILE VAR VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given a weighted, directed and connected graph of V vertices and E edges, Find the shortest distance of all the vertex's from the source vertex S. Note: If the Graph contains a negative cycle then return an array consisting of only -1. Example 1: Input: E = [[0,1,9]] S = 0 Output: 0 9 Explanation: Shortest distance of all nodes from source is printed. Example 2: Input: E = [[0,1,5],[1,0,3],[1,2,-1],[2,0,1]] S = 2 Output: 1 6 0 Explanation: For nodes 2 to 0, we can follow the path- 2-0. This has a distance of 1. For nodes 2 to 1, we cam follow the path- 2-0-1, which has a distance of 1+5 = 6, Your Task: You don't need to read input or print anything. Your task is to complete the function bellman_ford( ) which takes a number of vertices V and an E-sized list of lists of three integers where the three integers are u,v, and w; denoting there's an edge from u to v, which has a weight of w and source node S as input parameters and returns a list of integers where the ith integer denotes the distance of an ith node from the source node. If some node isn't possible to visit, then its distance should be 100000000(1e8). Also, If the Graph contains a negative cycle then return an array consisting of only -1. Expected Time Complexity: O(V*E). Expected Auxiliary Space: O(V). Constraints: 1 ≤ V ≤ 500 1 ≤ E ≤ V*(V-1) -1000 ≤ adj[i][j] ≤ 1000 0 ≤ S < V
class Solution: def bellman_ford(self, V, edges, S): G = convert(edges, V) return my_bellman_ford(G, S, V) def convert(G, size): graph = [[] for _ in range(size + 1)] for u, v, w in G: graph[u].append((v, w)) return graph def my_bellman_ford(G, source, size): distance = [100000000] * size parent = [None] * size distance[source] = 0 for i in range(size - 1): test = True for j in range(size): for k, w in G[j]: if distance[k] > distance[j] + w: test = False distance[k] = distance[j] + w parent[k] = j if test: break for j in range(size): for k, w in G[j]: if distance[k] > distance[j] + w: return [-1] return distance
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR RETURN LIST NUMBER RETURN VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: def countOneRow(a): res = 0 length = 0 for el in a: if el == 0: length = 0 else: length += 1 res += length return res M = len(mat) N = len(mat[0]) res = 0 for up in range(M): h = [1] * N for down in range(up, M): for k in range(N): h[k] *= mat[down][k] print(h) res += countOneRow(h) return res
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: M = len(mat) N = len(mat[0]) res = 0 for i in range(M): r = [1] * N for j in range(i, M): r = [(r[k] & mat[j][k]) for k in range(N)] res += self.countOneRow(r) return res def countOneRow(self, r): res = length = 0 for i, x in enumerate(r): length = 0 if x == 0 else length + 1 res += length return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: n, m = len(mat), len(mat[0]) def num_submat_at(a, b): c = 0 bound = m i = a while i < n: j = b while j < bound: if mat[i][j]: c += 1 else: bound = j j += 1 i += 1 return c total_c = 0 for i in range(n): for j in range(m): total_c += num_submat_at(i, j) return total_c
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: if not mat: return 0 m, n = len(mat), len(mat[0]) ret = 0 height = [0] * n for i in range(m): for j in range(n): height[j] = 0 if not mat[i][j] else height[j] + 1 if mat[i][j]: h = height[j] for k in range(j, -1, -1): if mat[i][k]: h = min(h, height[k]) ret += h else: break return ret
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: result = 0 n = len(mat) m = len(mat[0]) consecutive_ones = [([0] * m) for _ in range(n)] overall_ones = [] for i in range(n): c = 0 for j in range(m - 1, -1, -1): if mat[i][j] == 1: c += 1 overall_ones.append((i, j)) else: c = 0 consecutive_ones[i][j] = c while len(overall_ones) != 0: leftmost_point = overall_ones.pop() i = leftmost_point[0] j = leftmost_point[1] prev_row_width = float("inf") for row in range(i, n): curr_width = consecutive_ones[row][j] if curr_width == 0: break result += min(prev_row_width, curr_width) prev_row_width = min(prev_row_width, curr_width) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: nums = [([0] * len(mat[0])) for _ in range(len(mat))] for row in range(len(mat)): for col in range(len(mat[0])): if col == 0: nums[row][col] = mat[row][col] elif mat[row][col] == 0: nums[row][col] = 0 else: nums[row][col] = nums[row][col - 1] + 1 res = 0 for row in range(len(mat)): for col in range(len(mat[0])): Min = nums[row][col] k = row while k >= 0: if nums[k][col] == 0: break Min = min(nums[k][col], Min) res += Min k -= 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) n = len(mat[0]) dp = [[mat[i][j] for j in range(n)] for i in range(m)] for i in range(m): for j in range(n - 2, -1, -1): if mat[i][j] == 1: dp[i][j] = dp[i][j + 1] + 1 res = 0 for i in range(m): for j in range(n): k = i mn = dp[i][j] while k < m and dp[k][j] != 0: mn = min(mn, dp[k][j]) res += mn k += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat) -> int: m, n = len(mat), len(mat[0]) consective = [0] * m res = 0 for line in range(n): for row in range(m): if mat[row][line] == 1: consective[row] += 1 else: consective[row] = 0 stack = [] for i in range(m): val = consective[i] while stack and val <= stack[-1][0]: stack.pop() stack.append((val, i)) for k in range(len(stack)): if k == 0: a, b = stack[k] res += a * (b + 1) else: a, b = stack[k - 1] c, d = stack[k] res += c * (d - b) return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: dph = [[(0) for j in range(len(mat[0]) + 1)] for i in range(len(mat) + 1)] dpv = [[(0) for j in range(len(mat[0]) + 1)] for i in range(len(mat) + 1)] dps = [[[] for j in range(len(mat[0]) + 1)] for i in range(len(mat) + 1)] counts = 0 for i in range(1, len(mat) + 1): for j in range(1, len(mat[0]) + 1): if mat[i - 1][j - 1] == 1: dpv[i][j] = dpv[i - 1][j] + 1 dph[i][j] = dph[i][j - 1] + 1 if dpv[i][j] - 1 > dpv[i - 1][j - 1]: tmp = collections.deque([dph[i][j]]) for k in range(dpv[i][j] - 1): if k < dpv[i - 1][j - 1]: tmp.append(min(dps[i - 1][j - 1][k] + 1, dph[i][j])) else: tmp.append(1) else: tmp = collections.deque([dph[i][j]]) for k in range(dpv[i][j] - 1): tmp.append(min(dps[i - 1][j - 1][k] + 1, dph[i][j])) dps[i][j] = tmp counts += sum(tmp) print(dps) return counts
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER 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 NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN 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 NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def helper(self, arr): tracker = [0] * len(arr) stack = [] for i in range(len(arr)): while stack and arr[stack[-1]] >= arr[i]: stack.pop() if stack: tracker[i] = tracker[stack[-1]] tracker[i] += arr[i] * (i - stack[-1]) else: tracker[i] = arr[i] * (i + 1) stack.append(i) return sum(tracker) def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) n = len(mat[0]) column_track = [0] * n res = 0 for i in range(m): for j in range(n): column_track[j] = 0 if mat[i][j] == 0 else column_track[j] + 1 res += self.helper(column_track) return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, matrix): m, n = len(matrix), len(matrix[0]) res = 0 heights = [0] * (n + 1) for x in range(m): stack = [] dp = [0] * (n + 1) for y in range(n): if matrix[x][y] == 1: heights[y] = heights[y] + 1 else: heights[y] = 0 for right in range(n + 1): while stack and heights[right] <= heights[stack[-1]]: cur = stack.pop() if stack: left = stack[-1] dp[right] = dp[left] + heights[right] * (right - left) else: left = -1 dp[right] = heights[right] * (right - left) stack.append(right) res += sum(dp) return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: for i in range(1, len(mat)): for j in range(len(mat[0])): if mat[i][j]: mat[i][j] += mat[i - 1][j] ans = 0 for i in range(len(mat)): stack = [] cnt = 0 for j in range(len(mat[0])): while stack and mat[i][stack[-1]] > mat[i][j]: last = stack.pop() prev = stack[-1] if stack else -1 cnt -= (mat[i][last] - mat[i][j]) * (last - prev) cnt += mat[i][j] ans += cnt stack.append(j) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: ans = 0 N, M = len(mat), len(mat[0]) for i in range(N): mask = mat[i] for j in range(i, N): if j > i: mask = [int(i & j) for i, j in zip(mask, mat[j])] cnt = 0 for k in mask: if k == 1: cnt += 1 else: ans += cnt * (cnt + 1) // 2 cnt = 0 ans += cnt * (cnt + 1) // 2 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: res = 0 m, n = len(mat), len(mat[0]) dp = [[(0) for j in range(n)] for i in range(m)] for i in range(m): for j in range(n): if mat[i][j]: if j == 0: dp[i][j] = 1 else: dp[i][j] = dp[i][j - 1] + 1 for i in range(m): for j in range(n): if mat[i][j] == 0: continue row, minwidth = i, float("inf") while row >= 0 and mat[row][j]: minwidth = min(minwidth, dp[row][j]) res += minwidth row -= 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: rows = len(mat) columns = len(mat[0]) def countSubmat(s_r, s_c): e_r = rows e_c = columns c_r = s_r c_c = s_c count = 0 while c_r < e_r: if mat[c_r][s_c] == 0: break else: count += 1 c_c = s_c + 1 while c_c < e_c: if mat[c_r][c_c] == 1: count += 1 else: e_c = c_c c_c += 1 c_r += 1 c_c = s_c return count res = 0 for i in range(rows): for j in range(columns): res += countSubmat(i, j) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) count = 0 for left in range(n): row_sums = [(0) for _ in range(m)] for right in range(left, n): consec_count = 0 width = right - left + 1 for bottom in range(m): row_sums[bottom] += mat[bottom][right] if row_sums[bottom] != width: consec_count = 0 else: consec_count += 1 count += consec_count return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: r = len(mat) c = len(mat[0]) for i in range(r): for j in range(c - 2, -1, -1): if mat[i][j] == 1: if j < c - 1: mat[i][j] += mat[i][j + 1] res = 0 print(mat) for i in range(r): for j in range(c): m = float("inf") for k in range(i, r): m = min(m, mat[k][j]) res += m return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) row_left_ones = [([0] * n) for _ in range(m)] for i in range(m): for j in range(n): if mat[i][j] == 0: row_left_ones[i][j] = 0 else: row_left_ones[i][j] = 1 if j > 0: row_left_ones[i][j] += row_left_ones[i][j - 1] res = 0 for i in range(m): for j in range(n): curr_row_left_ones = row_left_ones[i][j] for k in range(i, -1, -1): curr_row_left_ones = min(curr_row_left_ones, row_left_ones[k][j]) res += curr_row_left_ones return res res = 0 for j in range(n): total = 0 stack = [] for i in range(m): total += left_ones[i][j] width = 1 while stack and stack[-1][0] > left_ones[i][j]: last_ones, last_width = stack.pop() total -= last_width * (last_ones - left_ones[i][j]) width += last_width res += total stack.append((left_ones[i][j], width)) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: lim1 = len(mat) lim2 = len(mat[0]) hz = [[(0) for j in range(0, lim2)] for i in range(0, lim1)] t = 0 for i in range(lim1 - 1, -1, -1): for j in range(lim2 - 1, -1, -1): if mat[i][j] != 0: if j < lim2 - 1: hz[i][j] += hz[i][j + 1] + 1 else: hz[i][j] = 1 vt = [[(0) for j in range(0, lim2)] for i in range(0, lim1)] for i in range(0, lim1): for j in range(0, lim2): if hz[i][j] != 0: mn = 99999999999 for k in range(i, lim1): if hz[k][j] == 0: break elif hz[k][j] < mn: mn = hz[k][j] t += mn return t
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) n = len(mat[0]) ans = 0 for i1 in range(m): dp = [0] * n for i2 in range(i1, m): for j in range(n): dp[j] += mat[i2][j] l = 0 j = 0 while j < n: if dp[j] == i2 - i1 + 1: l += 1 else: ans += l * (l + 1) // 2 l = 0 j += 1 if l > 0: ans += l * (l + 1) // 2 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: def pom(tab): stack = [] sums = [0] * len(tab) for i in range(len(tab)): while len(stack) > 0 and tab[stack[-1]] > tab[i]: stack.pop() if len(stack) == 0: sums[i] = (i + 1) * tab[i] else: sums[i] = sums[stack[-1]] + tab[i] * (i - stack[-1]) stack.append(i) return sum(sums) n_rows = len(mat) n_cols = len(mat[0]) h = [0] * n_cols res = 0 for row in mat: for i in range(len(row)): if row[i] == 1: h[i] += 1 else: h[i] = 0 res += pom(h) return res
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: n_row = len(mat) if n_row == 0: return 0 n_col = len(mat[0]) if n_col == 0: return 0 result = 0 hist = [0] * (n_col + 1) for row in range(n_row): min_hist_indices = [-1] dp = [0] * (n_col + 1) for col in range(n_col): hist[col] = 0 if mat[row][col] == 0 else hist[col] + 1 while hist[col] < hist[min_hist_indices[-1]]: min_hist_indices.pop() dp[col] = dp[min_hist_indices[-1]] + hist[col] * ( col - min_hist_indices[-1] ) min_hist_indices.append(col) result += sum(dp) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def histogramCount(self, histograms): stack = [] count = 0 out = 0 for i in range(len(histograms)): while len(stack) != 0 and histograms[i] < histograms[stack[-1]]: j = stack.pop() if len(stack) == 0: k = -1 else: k = stack[-1] count -= (histograms[j] - histograms[i]) * (j - k) count += histograms[i] out += count stack.append(i) return out def numSubmatHard(self, mat: List[List[int]]) -> int: prev = [0] * len(mat[0]) out = 0 for i in range(len(mat)): for j in range(len(mat[i])): if mat[i][j] == 0: prev[j] = 0 else: prev[j] += mat[i][j] out += self.histogramCount(prev) print(prev, out) return out def memoization(self, grid, i, j, memo): if i == len(grid) or j == len(grid[0]): return 0, 0, 0 if memo[i][j] is not None: return memo[i][j] if grid[i][j] == 0: memo[i][j] = 0, 0, 0 elif i == len(grid) - 1: _, col, _ = self.memoization(grid, i, j + 1, memo) memo[i][j] = 1, col + 1, col + 1 elif j == len(grid[0]) - 1: row, _, _ = self.memoization(grid, i + 1, j, memo) memo[i][j] = row + 1, 1, row + 1 else: rows, _, _ = self.memoization(grid, i + 1, j, memo) prev = rows + 1 out = prev k = j + 1 while k < len(grid[0]) and grid[i][k] != 0: row, _, _ = self.memoization(grid, i, k, memo) prev = min(prev, row) out += prev k += 1 memo[i][j] = rows + 1, k - j, out return memo[i][j] def numSubmat(self, mat: List[List[int]]) -> int: memo = [[None for j in range(len(mat[0]))] for i in range(len(mat))] out = 0 for i in range(len(mat)): for j in range(len(mat[0])): _, _, val = self.memoization(mat, i, j, memo) out += val return out
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER NUMBER NUMBER IF VAR VAR VAR NONE RETURN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: rows = len(mat) + 1 cols = len(mat[0]) + 1 dp = [[(0) for x in range(cols)] for y in range(rows)] for i in range(1, rows): dp[i][0] = 0 for j in range(1, cols): dp[0][j] = 0 res = 0 for i in range(1, rows): for j in range(1, cols): if mat[i - 1][j - 1] == 1: dp[i][j] = dp[i][j - 1] + 1 res += dp[i][j] min_ones = dp[i][j] for k in range(i - 1, -1, -1): min_ones = min(dp[k][j], min_ones) res += min_ones return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) n = len(mat[0]) for i in range(m): for j in reversed(range(n - 1)): if mat[i][j] == 1: mat[i][j] = mat[i][j + 1] + 1 def countSubmat(i, j): top = i left = j bound = n counter = 0 while i < m and mat[i][j] > 0: counter += min(bound, mat[i][j]) bound = min(bound, mat[i][j]) i += 1 return counter total = 0 for i in range(m): for j in range(n): if mat[i][j] > 0: total += countSubmat(i, j) return total
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def get_matrix_cnt(self, cur_row_height): res = 0 for i, polm in enumerate(cur_row_height): if polm > 0: for cur_height in range(1, polm + 1): res += 1 cur_index = i + 1 while ( cur_index < len(cur_row_height) and cur_row_height[cur_index] >= cur_height ): res += 1 cur_index += 1 return res def numSubmat(self, mat: List[List[int]]) -> int: m = len(mat) n = len(mat[0]) cur_row_height = [0] * n total = 0 for row in range(m): for col in range(n): if mat[row][col] == 0: cur_row_height[col] = 0 else: cur_row_height[col] += 1 cur_cnt = self.get_matrix_cnt(cur_row_height) total += cur_cnt return total
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: rows = len(mat) cols = len(mat[0]) row_matrices = [([0] * cols) for _ in range(rows)] for i in range(rows): row_matrices[i][0] = mat[i][0] for j in range(1, cols): if mat[i][j] == 1: row_matrices[i][j] = row_matrices[i][j - 1] + 1 else: row_matrices[i][j] = 0 result = 0 for i in range(rows): for j in range(cols): cur_min = float("inf") for k in range(i, -1, -1): if mat[k][j] == 0: break cur_min = min(cur_min, row_matrices[k][j]) result += cur_min return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, A: List[List[int]]) -> int: m, n = len(A), len(A[0]) h = [0] * n res = 0 for i in range(m): stk = [] psum = [0] * n for j in range(n): h[j] = h[j] + 1 if A[i][j] else 0 while stk and h[stk[-1]] >= h[j]: stk.pop() k = stk[-1] if stk else -1 ksum = psum[k] if stk else 0 psum[j] = ksum + h[j] * (j - k) stk.append(j) res += sum(psum) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: if not mat: return 0 answer = 0 row, col = len(mat), len(mat[0]) one_counts = [[(0) for _ in range(col)] for _ in range(row)] for r in range(row): counter = 0 for c in range(col - 1, -1, -1): if mat[r][c] == 1: counter += 1 one_counts[r][c] = counter else: counter = 0 for r in range(row): for c in range(col): if mat[r][c] == 1: min_width = float("inf") for k in range(r, row): if mat[k][c] == 1: min_width = min(min_width, one_counts[k][c]) answer += min_width continue break return answer
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: m, n = len(mat), len(mat[0]) def count_sub(i, j): max_c = n count = 0 for r in range(i, m): for c in range(j, max_c): if mat[r][c] == 1: count += 1 else: max_c = c break return count ans = 0 for i in range(m): for j in range(n): if mat[i][j]: ans += count_sub(i, j) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: def helper(h): res = 0 st = [] sum_ = [0] * len(h) for i in range(len(h)): while len(st) > 0 and h[st[-1]] >= h[i]: st.pop() if len(st) > 0: left_bound = st[-1] sum_[i] += sum_[left_bound] sum_[i] += h[i] * (i - left_bound) else: sum_[i] = h[i] * (i + 1) st.append(i) return sum(sum_) res = 0 h = [0] * len(mat[0]) for i in range(len(mat)): for j in range(len(mat[0])): if mat[i][j] == 1: h[j] += 1 else: h[j] = 0 res += helper(h) return res
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: mat = [([0] + row) for row in mat] m, n = len(mat), len(mat[0]) result = 0 for i, row in enumerate(mat): stack = [] count = [0] * n for j in range(n): if row[j] and i: row[j] += mat[i - 1][j] while stack and row[stack[-1]] > row[j]: stack.pop() if j and row[j]: count[j] = count[stack[-1]] + row[j] * (j - stack[-1]) result += count[j] stack.append(j) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a rows * columns matrix mat of ones and zeros, return how many submatrices have all ones.   Example 1: Input: mat = [[1,0,1],   [1,1,0],   [1,1,0]] Output: 13 Explanation: There are 6 rectangles of side 1x1. There are 2 rectangles of side 1x2. There are 3 rectangles of side 2x1. There is 1 rectangle of side 2x2. There is 1 rectangle of side 3x1. Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13. Example 2: Input: mat = [[0,1,1,0],   [0,1,1,1],   [1,1,1,0]] Output: 24 Explanation: There are 8 rectangles of side 1x1. There are 5 rectangles of side 1x2. There are 2 rectangles of side 1x3. There are 4 rectangles of side 2x1. There are 2 rectangles of side 2x2. There are 2 rectangles of side 3x1. There is 1 rectangle of side 3x2. Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24. Example 3: Input: mat = [[1,1,1,1,1,1]] Output: 21 Example 4: Input: mat = [[1,0,1],[0,1,0],[1,0,1]] Output: 5   Constraints: 1 <= rows <= 150 1 <= columns <= 150 0 <= mat[i][j] <= 1
class Solution: def numSubmat(self, mat: List[List[int]]) -> int: if not mat or not mat[0]: return 0 m = len(mat) n = len(mat[0]) dp = [([0] * (n + 1)) for i in range(m + 1)] res = 0 for i in range(m): for j in range(n): if mat[i][j] == 1: dp[i + 1][j + 1] = dp[i + 1][j] + 1 res += dp[i + 1][j + 1] minOnes = dp[i + 1][j + 1] k = i - 1 while k >= 0 and mat[k][j] == 1: minOnes = min(minOnes, dp[k + 1][j + 1]) res += minOnes k -= 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER 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 VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR