description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | MOD = 1000000007
A = [([0, 1, 2, 4, 8] + [0] * 996) for _ in range(1001)]
A[0] = [0] * 1001
for m in range(5, 1001):
A[1][m] = (A[1][m - 1] + A[1][m - 2] + A[1][m - 3] + A[1][m - 4]) % MOD
for n in range(2, 1001):
for m in range(1001):
A[n][m] = A[n - 1][m] * A[1][m] % MOD
def solve(N, M):
SN = [0] * 1001
SN[1] = 1
for m in range(2, M + 1):
SN[m] = A[N][m]
for i in range(1, m):
SN[m] = (SN[m] - SN[i] * A[N][m - i]) % MOD
return SN[M]
def main():
t = int(input())
for _ in range(t):
n, m = [int(i) for i in input().split()]
print(solve(n, m))
main() | ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER NUMBER NUMBER NUMBER BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | A = 1000000007
inp = []
for _ in range(int(input())):
[n, m] = [int(x) for x in input().split()]
inp.append((n, m))
m_max = max([a[1] for a in inp])
r = [1, 2, 4, 8]
for i in range(4, m_max):
r.append(sum(r[i - 4 : i]) % A)
for a, b in inp:
t = [1]
s = [pow(x, a, A) for x in r[:b]]
for j in range(1, b):
t.append((s[j] - sum([(t[k] * s[j - k - 1] % A) for k in range(j)])) % A)
print(t[-1]) | ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | mod = 1000000007
lim = 1000
A = [0] * (lim + 1)
for i in range(1, 5):
A[i] = sum(A[:i]) + 1
for i in range(5, lim + 1):
A[i] = sum(A[i - 4 : i]) % mod
for _ in range(int(input())):
n, m = map(int, input().strip().split())
H = [0] * (m + 1)
for i in range(1, m + 1):
H[i] = pow(A[i], n, mod)
S = [0] * (m + 1)
for i in range(1, m + 1):
S[i] = (H[i] - sum([(S[j] * H[i - j]) for j in range(1, i)]) % mod + mod) % mod
print(S[m]) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | mod = 1000000007
T = int(input())
for case in range(T):
N, M = map(int, input().rstrip().split())
def solve():
if N == 1:
return 1 if M < 5 else 0
row_comb = [0] * (M + 1)
row_comb[0] = 1
for i in range(len(row_comb)):
for coin in [1, 2, 3, 4]:
if i - coin >= 0:
row_comb[i] += row_comb[i - coin]
total_comb = [pow(i, N, mod) for i in row_comb]
unstable_comb = [0] * (M + 1)
def stable_comb(k):
return total_comb[k] - unstable_comb[k]
for i in range(2, M + 1):
for j in range(1, i):
unstable_comb[i] += total_comb[j] * stable_comb(i - j)
unstable_comb[i] %= mod
return stable_comb(M) % mod
print(solve()) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR LIST NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | tc = int(input())
mod = 1000000007
while tc:
tc -= 1
N, M = input().split()
N, M = int(N), int(M)
d = {(-3): 0, (-2): 0, (-1): 0, (0): 1, (1): 1}
for i in range(2, M + 1):
d[i] = (d[i - 1] + d[i - 2] + d[i - 3] + d[i - 4]) % mod
D = {(1): 1}
for i in range(2, M + 1):
d[i] = pow(d[i], N, mod)
ans = d[i]
for j in range(1, i):
ans -= d[i - j] * D[j]
ans = ans % mod
if ans < 0:
ans = mod - ans
D[i] = ans
print(D[M]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | modc = 1000000007
maxn = 1000 + 5
a = {}
a[1, 1] = 1
a[1, 2] = 2
a[1, 3] = 4
a[1, 4] = 8
for i in range(5, maxn):
a[1, i] = (a[1, i - 1] + a[1, i - 2] + a[1, i - 3] + a[1, i - 4]) % modc
for h in range(2, maxn):
for w in range(1, maxn):
p, d = a[h // 2, w], a[1, w] if h % 2 == 1 else 1
a[h, w] = p * p * d % modc
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
s = [0] * maxn
s[1] = 1
for i in range(2, m + 1):
w = sum(s[j] * a[n, i - j] % modc for j in range(1, i)) % modc
s[i] = (a[n, i] - w + modc) % modc
print(s[m]) | ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | def lego_blocks(N, M):
S = [(0) for _ in range(M)]
P = [(0) for _ in range(M)]
Q = [(0) for _ in range(M)]
for i in range(M):
Q[i] += pow(2, i) if i < 4 else Q[i - 1] + Q[i - 2] + Q[i - 3] + Q[i - 4]
Q[i] %= 1000000007
for i in range(M):
P[i] = pow(Q[i], N, 1000000007)
for i in range(M):
S[i] = P[i]
for j in range(1, i + 1):
S[i] -= P[j - 1] * S[i - j]
S[i] %= 1000000007
return S[M - 1] % 1000000007
def main():
T = int(input())
for _ in range(T):
N, M = [int(i) for i in input().split()]
print(lego_blocks(N, M))
main() | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | def get_walls(n, m):
first = [1, 1, 2, 4]
for i in range(4, m + 1):
first.append(sum(first[-4:]) % 1000000007)
table = [[1] * (m + 1)]
for i in range(n):
table.append([(x * y % 1000000007) for x, y in zip(table[-1], first)])
return table
walls = get_walls(1000, 1000)
def get_unbroken(n, m, walls=walls):
unbroken = []
wall = []
for w in walls[n][1 : m + 1]:
total = sum(x * y for x, y in zip(unbroken, reversed(wall)))
unbroken.append((w - total) % 1000000007)
wall.append(w)
return unbroken[-1]
t = int(input())
for test_case in range(t):
n, m = input().strip().split()
n, m = int(n), int(m)
print(get_unbroken(n, m)) | FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | t = input()
mod = 1000000007
def fastPow(base, power, MOD):
result = 1
while power > 0:
if power % 2 == 1:
result = result * base % MOD
power = power // 2
base = base * base % MOD
return result
for _ in range(int(t)):
n, m = map(int, input().split())
floorCombinations = [0] * 1001
floorCombinations[0] = 0
floorCombinations[1] = 1
floorCombinations[2] = 2
floorCombinations[3] = 4
floorCombinations[4] = 8
for i in range(5, 1001):
floorCombinations[i] = (
floorCombinations[i - 1]
+ floorCombinations[i - 2]
+ floorCombinations[i - 3]
+ floorCombinations[i - 4]
) % mod
dp = [0] * (m + 1)
dp[0] = 0
dp[1] = 1
totalCases = [0] * (m + 1)
for i in range(1, len(totalCases)):
totalCases[i] = fastPow(floorCombinations[i], n, mod)
for i in range(2, m + 1):
dp[i] = totalCases[i]
for ptr in range(1, i):
dp[i] = (dp[i] - dp[ptr] * totalCases[i - ptr]) % mod
print(dp[m] % mod) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | t = int(input())
inps = []
max_n, max_m = 0, 0
M = int(1000000000.0 + 7)
rows = []
cols = []
for test in range(t):
n, m = [int(x) for x in input().split()]
max_n, max_m = max(max_n, n), max(max_m, m)
inps.append((n, m))
if not n in rows:
rows.append(n)
cols.append(m)
z = rows.index(n)
cols[z] = max(m, cols[z])
a = [([0] * (max_m + 1)) for i in range(max_n + 1)]
dp = [([0] * (max_m + 1)) for i in range(max_n + 1)]
for col in range(max_m + 1):
a[0][col] = 1
for row in range(max_n + 1):
a[row][0] = 1
for col in range(1, max_m + 1):
a[1][col] = a[1][col - 1]
if col - 2 > -1:
a[1][col] += a[1][col - 2] % M
if col - 3 > -1:
a[1][col] += a[1][col - 3] % M
if col - 4 > -1:
a[1][col] += a[1][col - 4] % M
a[1][col] %= M
for row in range(2, max_n + 1):
for col in range(1, max_m + 1):
a[row][col] = a[row - 1][col] * a[1][col] % M
for col in range(max_m + 1):
dp[0][col] = 1
for row in range(max_n + 1):
dp[row][0] = 1
for i in range(len(rows)):
row = rows[i]
for col in range(cols[i] + 1):
dp[row][col] = a[row][col]
for cut in range(1, col):
dp[row][col] -= dp[row][cut] * a[row][col - cut]
dp[row][col] %= M
for test in range(t):
n_, m_ = inps[test]
print(dp[n_][m_] % M) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | __author__ = "camilo ariza"
__email__ = "camilo.ariza@protomail.com"
__created__ = "Sat 04 Apr 2020 11:36:43 -0300"
__modified__ = "Wed 08 Apr 2020 14:31:52 -0300"
def get_solid_walls(n, m, rows=[1, 1, 2, 4, 8]):
mod = pow(10, 9) + 7
n, m = n % mod, m % mod
all_walls = [0] * (m + 1)
solid_walls = [0] * (m + 1)
len_rows = len(rows)
rows += [0] * (m + 1 - len_rows)
for i in range(1, m + 1):
if i >= len_rows:
rows[i] = (rows[i - 1] + rows[i - 2] + rows[i - 3] + rows[i - 4]) % mod
solid_walls[i] = all_walls[i] = pow(rows[i], n) % mod
for j in range(1, i):
solid_walls[i] = (solid_walls[i] - solid_walls[j] * all_walls[i - j]) % mod
return solid_walls[m]
t = int(input())
for _ in range(t):
print(get_solid_walls(*map(int, input().split()))) | ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF LIST NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | MOD = 10**9 + 7
def main(MOD=MOD, rowtbl=[1]):
ncases = int(input())
for case in range(ncases):
rows, cols = map(int, input().split())
while len(rowtbl) <= cols:
rowtbl.append(sum(rowtbl[-4:]) % MOD)
walltbl = [pow(possrows, rows, MOD) for possrows in rowtbl]
solidtbl = []
for i in range(cols + 1):
solidtbl.append(
(walltbl[i] - sum(solidtbl[j] * walltbl[i - j] for j in range(1, i)))
% MOD
)
print(solidtbl[-1])
main() | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | modulo = 1000000007
g = [(0) for x in range(1000)]
g[0:4] = [1, 2, 4, 8]
for i in range(4, 1000):
g[i] = (g[i - 1] + g[i - 2] + g[i - 3] + g[i - 4]) % modulo
T = int(input())
for i in range(T):
[N, M] = [int(x) for x in input().split()]
unconst = [(0) for x in range(M)]
const = [(0) for x in range(M)]
for j in range(M):
unconst[j] = pow(g[j], N, modulo)
for j in range(M):
const[j] = unconst[j] % modulo
for k in range(j):
const[j] = (const[j] - unconst[k] * const[j - k - 1]) % modulo
print(const[M - 1]) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | T = int(input())
for _ in range(T):
n, m = map(int, input().split())
ntes = [(0) for i in range(max(m + 1, 4))]
ntes[0], ntes[1], ntes[2], ntes[3] = 1, 1, 2, 4
for i in range(4, m + 1):
ntes[i] = (ntes[i - 1] + ntes[i - 2] + ntes[i - 3] + ntes[i - 4]) % 1000000007
total = [pow(ntes[i], n, 1000000007) for i in range(m + 1)]
validos = [(0) for i in range(m + 1)]
for i in range(1, m + 1):
validos[i] = total[i]
aux = 0
for j in range(1, i):
aux = (aux + validos[j] * total[i - j]) % 1000000007
validos[i] = (validos[i] - aux) % 1000000007
print(validos[m]) | 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 NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | m = 1000000007
raw = [0] * 1001
raw[1] = 1
raw[2] = 2
raw[3] = 4
raw[4] = 8
for i in range(5, 1001):
raw[i] = (raw[i - 1] + raw[i - 2] + raw[i - 3] + raw[i - 4]) % m
def modexp(b, e):
rv = 1
bb = b
ee = e
while ee > 0:
if ee & 1 == 1:
rv = rv * bb % m
bb = bb * bb % m
ee >>= 1
return rv
num_probs = int(input())
for prob in range(num_probs):
h, w = (int(x) for x in input().split())
base = [modexp(raw[i], h) for i in range(w + 1)]
count = [0] * (w + 1)
for i in range(1, w + 1):
count[i] = base[i]
for j in range(1, i):
count[i] = (count[i] - count[j] * base[i - j]) % m
print(count[w]) | ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR 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 VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | M = 1000000007
def powMod(num, power):
c, e = 1, 0
while e < power:
e += 1
c = num * c % M
return c
def solve(h, w):
memo = [0, 1, 2, 4, 8]
A = [0, 1, powMod(2, h), powMod(4, h), powMod(8, h)]
for i in range(5, w + 1):
memo.append((memo[i - 1] + memo[i - 2] + memo[i - 3] + memo[i - 4]) % M)
A.append(powMod(memo[i], h))
S = [0, 1]
for i in range(2, w + 1):
total = A[i]
for j in range(1, i):
total -= S[j] * A[i - j] % M
S.append(total % M)
return S[w]
T = int(input())
for i in range(T):
H, W = input().split(" ")
H, W = int(H), int(W)
print(solve(H, W)) | ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | def go(n, m):
M = 1000000007
f = [1, 1, 2, 4]
for i in range(4, m + 1):
f.append(sum(f[-4:]))
F = [0, 1]
for i in range(2, m + 1):
v = f[i] = pow(f[i], n, M)
for j in range(1, i):
v -= F[j] * f[i - j]
F.append(v % M)
return F[-1]
for t in range(int(input())):
print(go(*map(int, input().split()))) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | m = 1000000007
dp1 = [1, 1, 2, 4]
for i in range(4, 1001):
dp1.append((dp1[-1] + dp1[-2] + dp1[-3] + dp1[-4]) % m)
def f(h, w):
dp2 = []
dp3 = [pow(dp1[i], h, m) for i in range(w + 1)]
for i in range(w + 1):
tmp = dp3[i]
for j in range(1, i):
tmp = (tmp - dp2[j] * dp3[i - j]) % m
dp2.append(tmp)
return dp2[-1]
for e in range(int(input())):
print(f(*list(map(int, input().split())))) | ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | mod = 1000000007
max_w = 1000
def pow(a, b):
a %= mod
result = 1
while b > 0:
if b & 1:
result = result * a % mod
a = a * a % mod
b = b >> 1
return result
n_rows = [0] * (max_w + 1)
n_rows[0] = 1
n_rows[1] = 1
n_rows[2] = 2
n_rows[3] = 4
n_rows[4] = 8
for i in range(5, max_w + 1):
n_rows[i] = (n_rows[i - 1] + n_rows[i - 2] + n_rows[i - 3] + n_rows[i - 4]) % mod
T = int(input())
for _ in range(T):
h, w = [int(i) for i in input().split()]
n_grids = [pow(n, h) for n in n_rows]
n_no_splits = [1] * (w + 1)
for i in range(2, w + 1):
s = 0
for j in range(1, i):
s = (s + n_no_splits[j] * n_grids[i - j]) % mod
n_no_splits[i] = (n_grids[i] - s + mod) % mod
print(n_no_splits[-1]) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | MOD = int(1000000000.0 + 7)
for _ in range(int(input())):
N, M = map(int, input().split())
ways = [0] * (M + 4)
ways[0] = 1
for i in range(M):
for j in range(1, 5):
ways[i + j] = (ways[i + j] + ways[i]) % MOD
res = [0] * (M + 1)
resbad = [0] * (M + 1)
resgood = [0] * (M + 1)
resgood[1] = 1
resbad[1] = 0
res[1] = 1
for i in range(2, M + 1):
res[i] = pow(ways[i], N, MOD)
for j in range(1, i):
resbad[i] += resgood[j] * res[i - j] % MOD
resgood[i] = (res[i] - resbad[i] % MOD + MOD) % MOD
print(resgood[M]) | ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | def modPower(value, toPower, toMod):
powered = value
for i in range(1, toPower):
powered *= value
if powered > 1000000007:
powered %= toMod
return powered % toMod
def LegoSum(H, W):
A = [0] * (W + 1)
L = [0] * (W + 1)
A[0] = 1
for i in range(1, W + 1):
for j in range(1, 5):
if i >= j:
A[i] += A[i - j]
if A[i] > 1000000007:
A[i] = A[i] % 1000000007
for i in range(1, W + 1):
A[i] = modPower(A[i], H, 1000000007)
L[1] = 1
for w in range(2, W + 1):
toDel = 0
for i in range(1, w):
toDel += L[i] * A[w - i]
L[w] = (A[w] - toDel) % 1000000007
return L[W] % 1000000007
T = int(input())
for _ in range(T):
H, W = tuple(map(int, input().split()))
print(LegoSum(H, W)) | FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You have an infinite number of 4 types of lego blocks of sizes given as (depth x height x width):
d h w
1 1 1
1 1 2
1 1 3
1 1 4
Using these blocks, you want to make a wall of height $n$ and width $m$. Features of the wall are:
- The wall should not have any holes in it.
- The wall you build should be one solid structure, so there should not be a straight vertical break across all rows of bricks.
- The bricks must be laid horizontally.
How many ways can the wall be built?
Example
$n=2$
$m=3$
The height is $2$ and the width is $3$. Here are some configurations:
These are not all of the valid permutations. There are $\mbox{9}$ valid permutations in all.
Function Description
Complete the legoBlocks function in the editor below.
legoBlocks has the following parameter(s):
int n: the height of the wall
int m: the width of the wall
Returns
- int: the number of valid wall formations modulo $(10^9+7)$
Input Format
The first line contains the number of test cases $\boldsymbol{\boldsymbol{t}}$.
Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains two space-separated integers $n$ and $m$.
Constraints
$1\leq t\leq100$
$1\leq n,m\leq1000$
Sample Input
STDIN Function
----- --------
4 t = 4
2 2 n = 2, m = 2
3 2 n = 3, m = 2
2 3 n = 2, m = 3
4 4 n = 4, m = 4
Sample Output
3
7
9
3375
Explanation
For the first case, we can have:
$3\:\:\:\text{mod}\:(10^9+7)=3$
For the second case, each row of the wall can contain either two blocks of width 1, or one block of width 2. However, the wall where all rows contain two blocks of width 1 is not a solid one as it can be divided vertically. Thus, the number of ways is $2\times2\times2-1=7$ and $7\:\:\:\text{mod}\:(10^9+7)=7$. | ll = [0] * 1001
ll[0] = 1
ll[1] = 1
ll[2] = 2
ll[3] = 4
for g in range(4, 1001):
ll[g] = (ll[g - 1] + ll[g - 2] + ll[g - 3] + ll[g - 4]) % 1000000007
a = int(input().strip())
l = []
for t in range(a):
l.append(tuple(map(int, input().strip().split(" "))))
for i in l:
S = []
for tt in range(0, i[1] + 1):
S.append(pow(ll[tt], i[0], 1000000007))
U = [0, 0]
for j in range(2, i[1] + 1):
U.append(sum((S[k] - U[k]) * S[j - k] for k in range(1, j)) % 1000000007)
print((S[i[1]] - U[i[1]]) % 1000000007) | ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
a = []
for i in range(1, N + 1):
a.append(i)
n = N
k = K
dp = []
for i in range(N + 1):
c = []
for j in range(N + 1):
d = []
for k in range(k + 1):
d.append(-1)
c.append(d)
dp.append(c)
def util(i, su, b):
if su == n:
if b == k:
return 1
else:
return 0
if su > n or i >= n or b > k:
return 0
if dp[i][su][b] != -1:
return dp[i][su][b]
x = util(i, su + a[i], b + 1)
y = util(i + 1, su, b)
dp[i][su][b] = x + y
return dp[i][su][b]
return util(0, 0, 0) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
dp = [[(0) for _ in range(N + 1)] for _ in range(K + 1)]
for i in range(1, K + 1):
for j in range(i, N + 1):
if i == 1:
dp[i][j] = 1
elif j == i:
dp[i][j] = 1
else:
dp[i][j] = dp[i - 1][j - 1] + dp[i][j - i]
return dp[K][N] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
if N == K or K == 1:
return 1
elif N < K:
return 0
else:
return self.countWaystoDivide(N - 1, K - 1) + self.countWaystoDivide(
N - K, K
) | CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
memo = [[[(-1) for _ in range(N + 1)] for _ in range(N + 1)] for _ in range(K)]
def dfs(index: int = 0, last_used: int = 1, left: int = N):
if index == K - 1:
return 1 if left >= last_used else 0
if left <= 0:
return 0
if memo[index][left][last_used] > -1:
return memo[index][left][last_used]
res = []
for i in range(last_used, left):
if left >= max(0, i * (K - (index + 1))):
res.append(dfs(index + 1, i, left - i))
memo[index][left][last_used] = sum(res)
return memo[index][left][last_used]
return dfs(0, 1, N) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF VAR VAR VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER RETURN VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
if K > N:
return 0
def func(dp, ind, tar, K, N):
if ind > N:
return 0
if ind == N and tar == 0 and K == 0:
return 1
if tar < 0 or K < 0:
return 0
if dp[ind][tar][K] != -1:
return dp[ind][tar][K]
flag = 0
if tar >= ind:
flag = func(dp, ind, tar - ind, K - 1, N)
notflag = func(dp, ind + 1, tar, K, N)
dp[ind][tar][K] = flag + notflag
return dp[ind][tar][K]
dp = [
[[(-1) for i in range(N + 1)] for j in range(N + 1)] for K in range(N + 1)
]
return func(dp, 1, N, K, N) | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR VAR VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, n, k):
memo = {}
def helper(i, n, k):
if (i, n, k) in memo:
return memo[i, n, k]
if n == 0 and k != 0 or n != 0 and k == 0:
return 0
if n == 0 and k == 0:
return 1
if n < 0 or i > n:
return 0
inc = 0
exc = 0
inc = helper(i, n - i, k - 1)
exc = helper(i + 1, n, k)
memo[i, n, k] = inc + exc
return inc + exc
return helper(1, n, k) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
if N < K:
return 0
if K == 1:
return 1
return self.countWaystoDivide(N - K, K) + self.countWaystoDivide(N - 1, K - 1) | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | import sys
sys.setrecursionlimit(10**6)
class Solution:
def countWaystoDivide(self, n, s):
def csum(ind, t, co):
if ind > n:
return 0
if ind == n and t == 0 and co == 0:
return 1
if t < 0 or co < 0:
return 0
if dp[ind][t][co] != -1:
return dp[ind][t][co]
p = 0
if ind <= t:
p = csum(ind, t - ind, co - 1)
np = csum(ind + 1, t, co)
dp[ind][t][co] = p + np
return p + np
dp = [[([-1] * (s + 1)) for i in range(n + 1)] for j in range(n + 1)]
return csum(1, n, s) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
if N < K:
return 0
def solve(P, N, K):
if N < K * P:
return 0
if K == 1 and N > 0 or P * K == N:
return 1
i = P
s = 0
while i * K <= N:
s += solve(i, N - i, K - 1)
i += 1
return s
return solve(1, N, K) | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER FUNC_DEF IF VAR BIN_OP VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
if N < K:
return 0
T = [([0] * (K + 1)) for i in range(N + 1)]
for i in range(1, N + 1):
T[i][1] = 1
T[0][0] = 1
for i in range(1, N + 1):
for j in range(2, K + 1):
if i >= j:
T[i][j] = T[i - 1][j - 1] + T[i - j][j]
else:
T[i][j] = T[i - 1][j - 1]
return T[N][K] | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR 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 ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
memo = {}
def dp(i, remain, total, memo):
if total > N:
return 0
if remain == 0:
if total == N:
return 1
return 0
if i > N:
return 0
if (i, remain, total) in memo:
return memo[i, remain, total]
memo[i, remain, total] = dp(i, remain - 1, total + i, memo) + dp(
i + 1, remain, total, memo
)
return memo[i, remain, total]
return dp(1, K, 0, memo) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR NUMBER VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
def solve(sum_left, count, prev):
if count == 0:
if sum_left == 0:
return 1
else:
return 0
if sum_left == 0 and count != 0:
return 0
if dp[sum_left][count][prev] != -1:
return dp[sum_left][count][prev]
ways = 0
for i in range(prev, sum_left + 1):
ways += solve(sum_left - i, count - 1, i)
dp[sum_left][count][prev] = ways
return dp[sum_left][count][prev]
dp = [
[[(-1) for i in range(0, N + 1)] for j in range(K + 1)]
for k in range(N + 1)
]
return solve(N, K, 1) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
max_val = N - (K - 1)
mat = [
[[(0) for _ in range(max_val + 1)] for _ in range(N + 1)]
for _ in range(K + 1)
]
return self.ways_to_divide(N, K, max_val, mat)
def ways_to_divide(self, N, K, max_val, mat):
if mat[K][N][max_val]:
return mat[K][N][max_val]
if K == 1:
return 1
count = 0
max_val = min(N - (K - 1), max_val)
min_val = N // K + (N % K > 0)
for val in range(min_val, max_val + 1):
count += self.ways_to_divide(N - val, K - 1, val, mat)
mat[K][N][max_val] = count
return count | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, n, k):
dp = [[[(0) for _ in range(K + 1)] for _ in range(N + 1)] for _ in range(N + 1)]
for i in range(1, N + 1):
dp[i][0][0] = 1
for max_value in range(1, N + 1):
for remaining_sum in range(1, N + 1):
for remaining_groups in range(1, K + 1):
dp[max_value][remaining_sum][remaining_groups] = dp[max_value - 1][
remaining_sum
][remaining_groups]
if remaining_sum >= max_value:
dp[max_value][remaining_sum][remaining_groups] += dp[max_value][
remaining_sum - max_value
][remaining_groups - 1]
ans = 0
for i in range(1, N + 1):
ans += dp[i][N - i][K - 1]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, n, k):
dp = []
for i in range(n + 1):
l1 = []
for j in range(n + 1):
l2 = []
for z in range(k + 1):
l2.append(-1)
l1.append(l2)
dp.append(l1)
return solve(n, k, 1, dp)
def solve(s, c, p, dp):
if c == 0:
if s == 0:
return 1
else:
return 0
if s == 0:
return 0
ans = 0
if dp[s][p][c] != -1:
return dp[s][p][c]
for i in range(p, s + 1):
ans += solve(s - i, c - 1, i, dp)
dp[s][p][c] = ans
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
a = [i for i in range(1, N + 1)]
dp = [[([-1] * (K + 1)) for i in range(N + 1)] for j in range(N + 1)]
def coin(i, su, a1):
if su == N:
if a1 == K:
return 1
else:
return 0
if su > N or i >= N or a1 > K:
return 0
if dp[i][su][a1] != -1:
return dp[i][su][a1]
a3 = coin(i, su + a[i], a1 + 1)
a2 = coin(i + 1, su, a1)
dp[i][su][a1] = a3 + a2
return dp[i][su][a1]
return coin(0, 0, 0) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | import sys
sys.setrecursionlimit(1500)
class Solution:
def countWaystoDivide(self, N, K):
cache = [[([None] * (N + 1)) for i in range(N + 1)] for k in range(K + 1)]
def helper(k, _sum, i):
if _sum > N:
return 0
if k == 0:
if _sum == N:
cache[k][_sum][i] = 1
return 1
else:
cache[k][_sum][i] = 0
return 0
if i > N:
return 0
if cache[k][_sum][i] is None:
cache[k][_sum][i] = helper(k - 1, _sum + i, i) + helper(k, _sum, i + 1)
return cache[k][_sum][i]
return helper(K, 0, 1) | IMPORT EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR NONE ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def __init__(self):
self.cache = {}
def dp(self, nxt, summ, n):
if summ == 0 and n == 0:
return 1
if summ <= 0 or n == 0:
return 0
if (nxt, summ, n) in self.cache:
return self.cache[nxt, summ, n]
ans = 0
for i in range(1, nxt + 1):
ans += self.dp(i, summ - i, n - 1)
self.cache[nxt, summ, n] = ans
return ans
def countWaystoDivide(self, N, K):
ans = 0
for i in range(1, N + 1):
ans += self.dp(i, N - i, K - 1)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
dp = [
[[(-1) for i in range(K + 1)] for j in range(N + 1)] for j in range(N + 1)
]
def recursive(N, K, Sum=N):
nonlocal dp
if Sum == 0:
if K == 0:
return 1
else:
return 0
if K == 0 or N == 0:
return 0
if dp[N][Sum][K] != -1:
return dp[N][Sum][K]
dp[N][Sum][K] = recursive(N - 1, K, Sum)
if N <= Sum:
dp[N][Sum][K] += recursive(N, K - 1, Sum - N)
return dp[N][Sum][K]
return recursive(N, K) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF VAR IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def countWaystoDivide(self, N, K):
dp = [[(0) for j in range(K + 1)] for i in range(N + 1)]
dp[0][0] = 1
for i in range(1, N + 1):
dp[i][0] = 0
dp[i][1] = 1
for j in range(2, K + 1):
for i in range(j, N + 1):
dp[i][j] = dp[i - 1][j - 1] + dp[i - j][j]
return dp[N][K] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN VAR VAR VAR |
Given two integers N and K, the task is to count the number of ways to divide N into groups of positive integers. Such that each group shall have K elements and their sum is N. Also the number of elements in the groups follows a non-decreasing order (i.e. group[i] <= group[i+1]). Find the number of such groups
Example 1:
Input:
N = 8
K = 4
Output:
5
Explanation:
There are 5 groups such that their sum is 8
and the number of positive integers in each
group is 4. The 5 groups are [1, 1, 1, 5],
[1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3] and
[2, 2, 2, 2].
Example 2:
Input:
N = 4
K = 3
Output:
1
Explanation:
The only possible grouping is {1, 1, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function countWaystoDivide() which takes the integers N and K as the input parameters, and returns the number of ways to divide the sum N into K groups.
Expected Time Complexity: O(N^{2}*K)
Expected Auxiliary Space: O(N^{2}*K)
Constraints:
1 ≤ K ≤ N ≤ 100 | class Solution:
def s(self, n, k, p, dp):
if k == 0:
if n == 0:
return 1
return 0
if n <= 0:
return 0
if dp[n][k][p] != -1:
return dp[n][k][p]
w = 0
for i in range(p, n + 1):
w = w + self.s(n - i, k - 1, i, dp)
dp[n][k][p] = w
return w
def countWaystoDivide(self, N, K):
dp = []
for i in range(N + 2):
t2 = []
for j in range(K + 2):
t1 = []
for k in range(N + 2):
t1.append(-1)
t2.append(t1)
dp.append(t2)
return self.s(N, K, 1, dp) | CLASS_DEF FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER VAR |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | n = int(input())
(*a,) = map(int, input().split())
a.append(-1)
b = [a[i] for i in range(n) if a[i] != a[i - 1]]
dp = [[(0) for j in range(n + 2)] for i in range(n + 2)]
for l in range(len(b)):
for i in range(len(b) - l):
j = i + l
dp[i][j] = max(dp[i][j - 1], dp[i + 1][j])
if b[i] == b[j]:
dp[i][j] = dp[i + 1][j - 1] + 2
print(len(b) - (dp[0][len(b) - 1] + 1) // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | def lps(str):
n = len(str)
L = [[(0) for x in range(n)] for x in range(n)]
for i in range(n):
L[i][i] = 1
for cl in range(2, n + 1):
for i in range(n - cl + 1):
j = i + cl - 1
if str[i] == str[j] and cl == 2:
L[i][j] = 2
elif str[i] == str[j]:
L[i][j] = L[i + 1][j - 1] + 2
else:
L[i][j] = max(L[i][j - 1], L[i + 1][j])
return L[0][n - 1]
n = input()
n = int(n)
l = list(map(str, input().strip().split()))
seq = [l[0]]
ch = l[0]
for i in range(1, n):
if l[i] != ch:
ch = l[i]
seq.append(l[i])
n = len(seq)
print(n - lps(seq) // 2 - 1) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | def inpl():
return list(map(int, input().split()))
N = int(input())
A = inpl()
B = [0]
for a in A:
if B[-1] != a:
B.append(a)
B = B[1:]
k = len(B)
dp = [0] * ((k * k + k) // 2)
for d in range(1, k):
for i in range(k - d):
if B[i] == B[i + d]:
dp[d * (2 * k - d + 1) // 2 + i] = (
dp[(d - 2) * (2 * k - d + 3) // 2 + i + 1] + 1
)
else:
dp[d * (2 * k - d + 1) // 2 + i] = 1 + min(
dp[(d - 1) * (2 * k - d + 2) // 2 + i],
dp[(d - 1) * (2 * k - d + 2) // 2 + i + 1],
)
print(dp[-1]) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | n = int(input())
lsr = list(map(int, input().split()))
ls = [lsr[0]]
for le in lsr[1:]:
if le != ls[-1]:
ls.append(le)
n = len(ls)
dpa = []
dpb = [(0) for i in range(n + 1)]
for sz in range(2, n + 1):
dp = []
for start in range(n - sz + 1):
if ls[start] == ls[start + sz - 1]:
dp.append(min(dpb[start], dpb[start + 1], dpa[start + 1]) + 1)
else:
dp.append(min(dpb[start], dpb[start + 1]) + 1)
dpa, dpb = dpb, dp
print(dpb[0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | def main():
for i in range(n):
for j in range(n):
dp[0][i][j] = dp[1][i][j] = 0 if i == j else n
for r in range(n):
for l in range(r, -1, -1):
for k in [0, 1]:
color = c[l] if k == 0 else c[r]
if l != 0:
dp[0][l - 1][r] = min(
dp[0][l - 1][r], dp[k][l][r] + (1 if color != c[l - 1] else 0)
)
if r + 1 != n:
dp[1][l][r + 1] = min(
dp[1][l][r + 1], dp[k][l][r] + (1 if color != c[r + 1] else 0)
)
print(min(dp[0][0][n - 1], dp[1][0][n - 1]))
n = int(input())
c = list(map(int, input().split()))
dp = [[([0] * n) for _ in range(n)] for _ in range(2)]
main() | FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | n = int(input())
c = list(map(int, input().split()))
c.append(-5)
arr = [0]
for i in range(n):
if c[i] != c[i + 1]:
arr.append(c[i])
arr_r = [0] + arr[::-1]
l = len(arr)
store = [0] * l
up = [0] * l
for i in range(1, l):
for j in range(1, l):
if arr[i] == arr_r[j]:
store[j] = up[j - 1] + 1
else:
store[j] = max(store[j - 1], up[j])
up = store
store = [0] * l
print(l - 1 - 1 - up[-1] // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | def lcs(a, b):
c = [[(0) for i in range(len(a) + 1)] for i in range(len(b) + 1)]
for i in range(len(b) + 1):
for j in range(len(b) + 1):
if i == 0 or j == 0:
c[i][j] = 0
elif a[i - 1] == b[j - 1]:
c[i][j] = c[i - 1][j - 1] + 1
else:
c[i][j] = max(c[i - 1][j], c[i][j - 1])
return c
n = int(input())
a = list(map(int, input().split()))
b = [a[0]]
for i in range(1, n):
if a[i] != a[i - 1]:
b.append(a[i])
a = b[::-1]
c = lcs(a, b)
ans = 0
n = len(a)
for i in range(0, n + 1):
ans = max(ans, c[i][n - i])
print(n - ans - 1) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | def fill():
nonlocal dp
dp = [0] * (n + 2)
for i in range(n + 2):
dp[i] = [0] * (n + 2)
def compress(v):
nonlocal n
nonlocal c
c = [v[0]]
for i in range(1, n):
if v[i] != v[i - 1]:
c.append(v[i])
n = len(c)
n = int(input())
v = [int(i) for i in input().split()]
c = []
compress(v)
dp = []
fill()
for i in range(n):
dp[i][1] = 0
dp[i][2] = 1
for sz in range(3, n + 1):
for i in range(n):
j = i + sz - 1
if j >= n:
break
if c[i] == c[j]:
dp[i][sz] = dp[i + 1][sz - 2] + 1
else:
dp[i][sz] = min(dp[i + 1][sz - 1], dp[i][sz - 1]) + 1
print(dp[0][n]) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | n = int(input())
C = [0] + list(map(int, input().split()))
A = []
for i in range(1, n + 1):
if C[i] != C[i - 1]:
A.append(C[i])
L = len(A)
DP = [[([0] * L) for i in range(L)] for j in range(2)]
def color(r, i, j):
if r == 1:
if A[i] == A[j]:
DP[r][i][j] = min(DP[0][i][j - 1], DP[1][i][j - 1] + 1)
else:
DP[r][i][j] = min(DP[0][i][j - 1] + 1, DP[1][i][j - 1] + 1)
elif A[i] == A[j]:
DP[r][i][j] = min(DP[1][i + 1][j], DP[0][i + 1][j] + 1)
else:
DP[r][i][j] = min(DP[1][i + 1][j] + 1, DP[0][i + 1][j] + 1)
for i in range(1, L):
for j in range(L - i):
color(0, j, i + j)
color(1, j, i + j)
print(min(DP[0][0][L - 1], DP[1][0][L - 1])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | import sys
input = sys.stdin.readline
n = int(input())
c = list(map(int, input().split())) + [-1]
b = [c[i] for i in range(n) if c[i] != c[i + 1]]
m = len(b)
INF = 10**9
dp = [[([INF] * (m + 1)) for i in range(m + 1)] for j in range(2)]
for i in range(m):
dp[0][i][i] = dp[1][i][i] = 0
for l in range(1, m):
for i in range(m - l):
j = i + l
dp[0][i][j] = min(
dp[0][i][j],
dp[0][i + 1][j] + (b[i] != b[i + 1]),
dp[1][i + 1][j] + (b[i] != b[j]),
)
dp[1][i][j] = min(
dp[1][i][j],
dp[0][i][j - 1] + (b[i] != b[j]),
dp[1][i][j - 1] + (b[j - 1] != b[j]),
)
print(min(dp[1][0][m - 1], dp[0][0][m - 1])) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | def run_length_compress(string):
string = string + ["."]
n = len(string)
begin = 0
end = 1
cnt = 1
ans = []
while True:
if end >= n:
break
if string[begin] == string[end]:
end += 1
cnt += 1
else:
ans.append(string[begin])
begin = end
end = begin + 1
cnt = 1
return ans
def lps(string):
n = len(string)
dp = [([0] * n) for _ in range(n)]
for i in range(n):
dp[i][i] = 1
for cl in range(2, n + 1):
for i in range(n - cl + 1):
j = i + cl - 1
if string[i] == string[j] and cl == 2:
dp[i][j] = 2
elif string[i] == string[j]:
dp[i][j] = dp[i + 1][j - 1] + 2
else:
dp[i][j] = max(dp[i][j - 1], dp[i + 1][j])
return dp[0][n - 1]
n = int(input())
a = list(map(int, input().split()))
a = run_length_compress(a)
len_pal = lps(a)
print(len(a) - 1 - len_pal // 2) | FUNC_DEF ASSIGN VAR BIN_OP VAR LIST STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | n = int(input())
C = [int(i) for i in input().split()]
INF = 5000
DP = [[([0] * 2) for i in range(n)] for j in range(2)]
for i in range(n - 1):
for j in range(n - i - 1):
DP[(i + 1) % 2][j][0] = INF
DP[(i + 1) % 2][j][1] = INF
for k in range(2):
c = C[j] if k == 0 else C[j + i]
if c == C[j + i + 1]:
DP[(i + 1) % 2][j][1] = min(DP[(i + 1) % 2][j][1], DP[i % 2][j][k])
else:
DP[(i + 1) % 2][j][1] = min(DP[(i + 1) % 2][j][1], DP[i % 2][j][k] + 1)
c = C[j + 1] if k == 0 else C[j + i + 1]
if c == C[j]:
DP[(i + 1) % 2][j][0] = min(DP[(i + 1) % 2][j][0], DP[i % 2][j + 1][k])
else:
DP[(i + 1) % 2][j][0] = min(
DP[(i + 1) % 2][j][0], DP[i % 2][j + 1][k] + 1
)
print(min(DP[(n - 1) % 2][0])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | n = int(input())
a = list(map(int, input().split()))
b = []
p = -1
for e in a:
if e == p:
continue
else:
b.append(e)
p = e
def lcs(a, b):
lengths = [[(0) for j in range(len(b) + 1)] for i in range(len(a) + 1)]
for i, x in enumerate(a):
for j, y in enumerate(b):
if x == y:
lengths[i + 1][j + 1] = lengths[i][j] + 1
else:
lengths[i + 1][j + 1] = max(lengths[i + 1][j], lengths[i][j + 1])
x, y = len(a), len(b)
return lengths[x][y]
ans = 0
ans = lcs(list(reversed(b)), b) // 2
n = len(b)
print(n - ans - 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | def solve(a):
n = len(a)
best = [([0] * n) for _ in range(n)]
for i in range(n - 1, -1, -1):
for j in range(i + 1, n):
if a[i] == a[j]:
best[i][j] = best[i + 1][j - 1] + 1
else:
best[i][j] = min(best[i + 1][j], best[i][j - 1]) + 1
return best[0][n - 1]
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
if i == 0 or a[i - 1] != a[i]:
b.append(a[i])
print(solve(b)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER 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 VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.
Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i < k < j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.
For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.
The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.
The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.
-----Output-----
Print a single integer — the minimum number of the turns needed.
-----Examples-----
Input
4
5 2 2 1
Output
2
Input
8
4 5 2 2 1 3 5 5
Output
4
Input
1
4
Output
0
-----Note-----
In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows: $[5, 2, 2, 1]$ $[5, 5, 5, 1]$ $[1, 1, 1, 1]$
In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.
In the third example, the line already consists of one color only. | n = int(input())
C = [int(i) for i in input().split()]
D = []
for c in C:
if not D or D[-1] != c:
D.append(c)
n = len(D)
C = D[::-1]
DP = [([0] * (n + 1)) for i in range(2)]
for i in range(n):
for j in range(n):
if C[i] == D[j]:
DP[i + 1 & 1][j + 1] = DP[i & 1][j] + 1
else:
DP[i + 1 & 1][j + 1] = max(DP[i + 1 & 1][j], DP[i & 1][j + 1])
print(n - 1 - DP[n & 1][n] // 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER |
Raju and Rani and playing games in the school. Raju has recently learned how to draw angles.
He knows how to draw some angles. He also can add/subtract any of the two angles he draws. Rani now wants to test Raju.
Input:
First line contains N and K, denoting number of angles Raju knows how to draw and K the number of angles Rani will tell Raju to draw.
Next line contains, N space separated integers denoting the angles which Raju knows how to draw. Next line contains K space separated numbers denoting the angles Rani wants Raju to draw.
Output:
For each of the K angles Rani wants Raju to draw, output "YES" or "NO" (quotes for clarity) if he can draw those angles or not respectively.
Constraints:
1 ≤ N,K ≤ 10
0 ≤ All\; angles ≤ 360
SAMPLE INPUT
1 2
100
60 70
SAMPLE OUTPUT
YES
NO
Explanation
Adding 100 fifteen times gives 1500 degrees, which is same as 60.
70 cannot be drawn any way. | n, k = list(map(int, input().split()))
knows = list(map(int, input().split()))
draw = list(map(int, input().split()))
for i in draw:
if i in knows:
print("YES")
else:
for j in knows:
f = False
angle = j * 2
while angle % 360 != j:
if angle % 360 == i:
print("YES")
f = True
break
angle += j
if f:
break
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR STRING |
Raju and Rani and playing games in the school. Raju has recently learned how to draw angles.
He knows how to draw some angles. He also can add/subtract any of the two angles he draws. Rani now wants to test Raju.
Input:
First line contains N and K, denoting number of angles Raju knows how to draw and K the number of angles Rani will tell Raju to draw.
Next line contains, N space separated integers denoting the angles which Raju knows how to draw. Next line contains K space separated numbers denoting the angles Rani wants Raju to draw.
Output:
For each of the K angles Rani wants Raju to draw, output "YES" or "NO" (quotes for clarity) if he can draw those angles or not respectively.
Constraints:
1 ≤ N,K ≤ 10
0 ≤ All\; angles ≤ 360
SAMPLE INPUT
1 2
100
60 70
SAMPLE OUTPUT
YES
NO
Explanation
Adding 100 fifteen times gives 1500 degrees, which is same as 60.
70 cannot be drawn any way. | N, K = [int(i) for i in input().split()]
KnownAngle = [int(x) for x in input().split()]
Todraw = [int(x) for x in input().split()]
def Tell(toknow):
for j in KnownAngle:
for i in range(1, 360):
if j * i % 360 == toknow:
return True
def Telll(toknow):
for i in KnownAngle:
for k in KnownAngle:
if i + k == toknow or i - k == toknow or k - i == toknow:
return True
for j in Todraw:
if Tell(j) == True or Telll(j) == True:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER FUNC_DEF FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Raju and Rani and playing games in the school. Raju has recently learned how to draw angles.
He knows how to draw some angles. He also can add/subtract any of the two angles he draws. Rani now wants to test Raju.
Input:
First line contains N and K, denoting number of angles Raju knows how to draw and K the number of angles Rani will tell Raju to draw.
Next line contains, N space separated integers denoting the angles which Raju knows how to draw. Next line contains K space separated numbers denoting the angles Rani wants Raju to draw.
Output:
For each of the K angles Rani wants Raju to draw, output "YES" or "NO" (quotes for clarity) if he can draw those angles or not respectively.
Constraints:
1 ≤ N,K ≤ 10
0 ≤ All\; angles ≤ 360
SAMPLE INPUT
1 2
100
60 70
SAMPLE OUTPUT
YES
NO
Explanation
Adding 100 fifteen times gives 1500 degrees, which is same as 60.
70 cannot be drawn any way. | N, K = input().strip().split()
N = int(N)
K = int(K)
if N < 1 or N > 10 or K < 1 or K > 10:
exit()
raju_angles = input().strip().split()
raju_angles = list(map(int, raju_angles))
rani_angles = input().strip().split()
rani_angles = list(map(int, rani_angles))
for i in raju_angles:
if i < 0 or i > 360:
exit()
for i in rani_angles:
if i < 0 or i > 360:
exit()
raju = {}
for i in raju_angles:
for j in range(1, 50):
raju[i * j % 360] = i * j % 360
for i in rani_angles:
if i in raju:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Raju and Rani and playing games in the school. Raju has recently learned how to draw angles.
He knows how to draw some angles. He also can add/subtract any of the two angles he draws. Rani now wants to test Raju.
Input:
First line contains N and K, denoting number of angles Raju knows how to draw and K the number of angles Rani will tell Raju to draw.
Next line contains, N space separated integers denoting the angles which Raju knows how to draw. Next line contains K space separated numbers denoting the angles Rani wants Raju to draw.
Output:
For each of the K angles Rani wants Raju to draw, output "YES" or "NO" (quotes for clarity) if he can draw those angles or not respectively.
Constraints:
1 ≤ N,K ≤ 10
0 ≤ All\; angles ≤ 360
SAMPLE INPUT
1 2
100
60 70
SAMPLE OUTPUT
YES
NO
Explanation
Adding 100 fifteen times gives 1500 degrees, which is same as 60.
70 cannot be drawn any way. | def formatter(a):
if a == "360":
return 0
return int(a)
super_array = []
for i in range(360):
super_array.append([])
num = i
j = 1
while num not in super_array[i]:
super_array[i].append(num)
j += 1
num = i * j
while num >= 360:
num -= 360
n, k = list(map(int, input().split()))
gangles = list(map(formatter, input().split()))
cangles = list(map(formatter, input().split()))
all_angles_array = []
for i in gangles:
all_angles_array.extend(super_array[i])
all_angles_array = list(set(all_angles_array))
all_angles_array.sort()
for c in cangles:
counter = False
if c in all_angles_array:
print("yes".upper())
counter = True
if counter:
continue
for i in all_angles_array:
if i < c:
if c - i in all_angles_array:
print("yes".upper())
counter = True
break
else:
break
if counter:
continue
for i in all_angles_array:
if i > c:
if c + 360 - i in all_angles_array:
print("yes".upper())
counter = True
break
for i in all_angles_array:
shit = i + c
if shit > 360:
shit -= 360
if shit in all_angles_array:
print("yes".upper())
counter = True
break
if counter:
continue
for i in all_angles_array:
val = i - c
if val < 0:
val += 360
if val in all_angles_array:
print("yes".upper())
counter = True
break
if not counter:
print("no".upper()) | FUNC_DEF IF VAR STRING RETURN NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING ASSIGN VAR NUMBER IF VAR FOR VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING ASSIGN VAR NUMBER IF VAR FOR VAR VAR IF VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING ASSIGN VAR NUMBER IF VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING |
Raju and Rani and playing games in the school. Raju has recently learned how to draw angles.
He knows how to draw some angles. He also can add/subtract any of the two angles he draws. Rani now wants to test Raju.
Input:
First line contains N and K, denoting number of angles Raju knows how to draw and K the number of angles Rani will tell Raju to draw.
Next line contains, N space separated integers denoting the angles which Raju knows how to draw. Next line contains K space separated numbers denoting the angles Rani wants Raju to draw.
Output:
For each of the K angles Rani wants Raju to draw, output "YES" or "NO" (quotes for clarity) if he can draw those angles or not respectively.
Constraints:
1 ≤ N,K ≤ 10
0 ≤ All\; angles ≤ 360
SAMPLE INPUT
1 2
100
60 70
SAMPLE OUTPUT
YES
NO
Explanation
Adding 100 fifteen times gives 1500 degrees, which is same as 60.
70 cannot be drawn any way. | def fin(n, k):
for i in range(1, 51):
if (360 * i + n) % k == 0:
return 1
if (360 * i - n) % k == 0:
return 1
return 0
def main():
t = input().split()
N = int(t[0])
K = int(t[1])
raj = [int(i) for i in input().split()]
ran = [int(i) for i in input().split()]
for i in ran:
if i in raj:
print("YES")
else:
flg = 0
for j in raj:
if fin(i, j):
print("YES")
flg = 1
break
if flg == 0:
print("NO")
if __name__ == "__main__":
main() | FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR STRING EXPR FUNC_CALL VAR |
Raju and Rani and playing games in the school. Raju has recently learned how to draw angles.
He knows how to draw some angles. He also can add/subtract any of the two angles he draws. Rani now wants to test Raju.
Input:
First line contains N and K, denoting number of angles Raju knows how to draw and K the number of angles Rani will tell Raju to draw.
Next line contains, N space separated integers denoting the angles which Raju knows how to draw. Next line contains K space separated numbers denoting the angles Rani wants Raju to draw.
Output:
For each of the K angles Rani wants Raju to draw, output "YES" or "NO" (quotes for clarity) if he can draw those angles or not respectively.
Constraints:
1 ≤ N,K ≤ 10
0 ≤ All\; angles ≤ 360
SAMPLE INPUT
1 2
100
60 70
SAMPLE OUTPUT
YES
NO
Explanation
Adding 100 fifteen times gives 1500 degrees, which is same as 60.
70 cannot be drawn any way. | N, K = list(map(int, input().split(" ")))
angles_raju = list(map(int, input().split(" ")))
angles_rani = list(map(int, input().split(" ")))
all_angles = {}
for angle in angles_raju:
temp = angle
all_angles[temp] = 1
while temp % 360 != 0:
temp = temp + angle
all_angles[temp % 360] = 1
for angle in angles_rani:
if angle in all_angles:
print("YES")
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Raju and Rani and playing games in the school. Raju has recently learned how to draw angles.
He knows how to draw some angles. He also can add/subtract any of the two angles he draws. Rani now wants to test Raju.
Input:
First line contains N and K, denoting number of angles Raju knows how to draw and K the number of angles Rani will tell Raju to draw.
Next line contains, N space separated integers denoting the angles which Raju knows how to draw. Next line contains K space separated numbers denoting the angles Rani wants Raju to draw.
Output:
For each of the K angles Rani wants Raju to draw, output "YES" or "NO" (quotes for clarity) if he can draw those angles or not respectively.
Constraints:
1 ≤ N,K ≤ 10
0 ≤ All\; angles ≤ 360
SAMPLE INPUT
1 2
100
60 70
SAMPLE OUTPUT
YES
NO
Explanation
Adding 100 fifteen times gives 1500 degrees, which is same as 60.
70 cannot be drawn any way. | import sys
def main():
N, K = list(map(int, sys.stdin.readline().split()))
raju_knows = list(map(int, sys.stdin.readline().split()))
rani_wants = list(map(int, sys.stdin.readline().split()))
raju_can_make = []
for i in raju_knows:
raju_can_make.extend(angel(i))
for e in rani_wants:
if e in raju_can_make:
print("YES")
else:
print("NO")
def angel(n):
possible = []
a = get_next(n)
next(a)
for i in a:
angel = i % 360
if angel == n:
return possible
possible.append(angel)
def get_next(n):
constant = n
while True:
prev = n
n += constant
yield prev
main() | IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR VAR VAR VAR EXPR VAR EXPR FUNC_CALL VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
There's an array A consisting of N non-zero integers A_{1..N}. A subarray of A is called alternating if any two adjacent elements in it have different signs (i.e. one of them should be negative and the other should be positive).
For each x from 1 to N, compute the length of the longest alternating subarray that starts at x - that is, a subarray A_{x..y} for the maximum possible y ≥ x. The length of such a subarray is y-x+1.
------ Input ------
The first line of the input contains an integer T - the number of test cases.
The first line of each test case contains N.
The following line contains N space-separated integers A_{1..N}.
------ Output ------
For each test case, output one line with N space-separated integers - the lengths of the longest alternating subarray starting at x, for each x from 1 to N.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$-10^{9} ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
4
1 2 3 4
4
1 -5 1 -5
6
-5 -1 -1 2 -2 -3
----- Sample Output 1 ------
1 1 1 1
4 3 2 1
1 1 3 2 1 1
----- explanation 1 ------
Example case 1. No two elements have different signs, so any alternating subarray may only consist of a single number.
Example case 2. Every subarray is alternating.
Example case 3. The only alternating subarray of length 3 is A3..5. | for i in range(int(input())):
a = int(input())
l = list(map(int, input().split(" ")))
l1 = [(-1) for i in range(a)]
l1[-1] = 1
for i in range(a - 2, -1, -1):
if l[i] >= 0 and l[i + 1] <= 0 or l[i] <= 0 and l[i + 1] >= 0:
l1[i] = l1[i + 1] + 1
else:
l1[i] = 1
for i in l1:
print(i, end=" ")
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
There's an array A consisting of N non-zero integers A_{1..N}. A subarray of A is called alternating if any two adjacent elements in it have different signs (i.e. one of them should be negative and the other should be positive).
For each x from 1 to N, compute the length of the longest alternating subarray that starts at x - that is, a subarray A_{x..y} for the maximum possible y ≥ x. The length of such a subarray is y-x+1.
------ Input ------
The first line of the input contains an integer T - the number of test cases.
The first line of each test case contains N.
The following line contains N space-separated integers A_{1..N}.
------ Output ------
For each test case, output one line with N space-separated integers - the lengths of the longest alternating subarray starting at x, for each x from 1 to N.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$-10^{9} ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
4
1 2 3 4
4
1 -5 1 -5
6
-5 -1 -1 2 -2 -3
----- Sample Output 1 ------
1 1 1 1
4 3 2 1
1 1 3 2 1 1
----- explanation 1 ------
Example case 1. No two elements have different signs, so any alternating subarray may only consist of a single number.
Example case 2. Every subarray is alternating.
Example case 3. The only alternating subarray of length 3 is A3..5. | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
nl = []
j, c = 0, 1
for i in range(n):
if j < i:
j = i
while j < n - 1 and (l[j] < 0 and l[j + 1] >= 0 or l[j] >= 0 and l[j + 1] < 0):
j += 1
print(j - i + 1, end=" ")
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
There's an array A consisting of N non-zero integers A_{1..N}. A subarray of A is called alternating if any two adjacent elements in it have different signs (i.e. one of them should be negative and the other should be positive).
For each x from 1 to N, compute the length of the longest alternating subarray that starts at x - that is, a subarray A_{x..y} for the maximum possible y ≥ x. The length of such a subarray is y-x+1.
------ Input ------
The first line of the input contains an integer T - the number of test cases.
The first line of each test case contains N.
The following line contains N space-separated integers A_{1..N}.
------ Output ------
For each test case, output one line with N space-separated integers - the lengths of the longest alternating subarray starting at x, for each x from 1 to N.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$-10^{9} ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
4
1 2 3 4
4
1 -5 1 -5
6
-5 -1 -1 2 -2 -3
----- Sample Output 1 ------
1 1 1 1
4 3 2 1
1 1 3 2 1 1
----- explanation 1 ------
Example case 1. No two elements have different signs, so any alternating subarray may only consist of a single number.
Example case 2. Every subarray is alternating.
Example case 3. The only alternating subarray of length 3 is A3..5. | t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
arr.append(0)
j = 0
while j < n:
l = 1
for x in range(j, n):
if arr[x + 1] / arr[x] < 0:
l += 1
else:
break
for k in range(l):
print(l - k, end=" ")
j = x + 1
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
There's an array A consisting of N non-zero integers A_{1..N}. A subarray of A is called alternating if any two adjacent elements in it have different signs (i.e. one of them should be negative and the other should be positive).
For each x from 1 to N, compute the length of the longest alternating subarray that starts at x - that is, a subarray A_{x..y} for the maximum possible y ≥ x. The length of such a subarray is y-x+1.
------ Input ------
The first line of the input contains an integer T - the number of test cases.
The first line of each test case contains N.
The following line contains N space-separated integers A_{1..N}.
------ Output ------
For each test case, output one line with N space-separated integers - the lengths of the longest alternating subarray starting at x, for each x from 1 to N.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$-10^{9} ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
4
1 2 3 4
4
1 -5 1 -5
6
-5 -1 -1 2 -2 -3
----- Sample Output 1 ------
1 1 1 1
4 3 2 1
1 1 3 2 1 1
----- explanation 1 ------
Example case 1. No two elements have different signs, so any alternating subarray may only consist of a single number.
Example case 2. Every subarray is alternating.
Example case 3. The only alternating subarray of length 3 is A3..5. | for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
dp = [1] * n
for i in range(n - 1, 0, -1):
if arr[i] < 0 and arr[i - 1] < 0 or arr[i] > 0 and arr[i - 1] > 0:
continue
else:
dp[i - 1] = 1 + dp[i]
print(*dp) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
There's an array A consisting of N non-zero integers A_{1..N}. A subarray of A is called alternating if any two adjacent elements in it have different signs (i.e. one of them should be negative and the other should be positive).
For each x from 1 to N, compute the length of the longest alternating subarray that starts at x - that is, a subarray A_{x..y} for the maximum possible y ≥ x. The length of such a subarray is y-x+1.
------ Input ------
The first line of the input contains an integer T - the number of test cases.
The first line of each test case contains N.
The following line contains N space-separated integers A_{1..N}.
------ Output ------
For each test case, output one line with N space-separated integers - the lengths of the longest alternating subarray starting at x, for each x from 1 to N.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$-10^{9} ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
4
1 2 3 4
4
1 -5 1 -5
6
-5 -1 -1 2 -2 -3
----- Sample Output 1 ------
1 1 1 1
4 3 2 1
1 1 3 2 1 1
----- explanation 1 ------
Example case 1. No two elements have different signs, so any alternating subarray may only consist of a single number.
Example case 2. Every subarray is alternating.
Example case 3. The only alternating subarray of length 3 is A3..5. | t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))[::-1]
s = [1]
for i in range(1, n):
if arr[i] * arr[i - 1] < 0:
s.append(s[-1] + 1)
else:
s.append(1)
print(*s[::-1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
There's an array A consisting of N non-zero integers A_{1..N}. A subarray of A is called alternating if any two adjacent elements in it have different signs (i.e. one of them should be negative and the other should be positive).
For each x from 1 to N, compute the length of the longest alternating subarray that starts at x - that is, a subarray A_{x..y} for the maximum possible y ≥ x. The length of such a subarray is y-x+1.
------ Input ------
The first line of the input contains an integer T - the number of test cases.
The first line of each test case contains N.
The following line contains N space-separated integers A_{1..N}.
------ Output ------
For each test case, output one line with N space-separated integers - the lengths of the longest alternating subarray starting at x, for each x from 1 to N.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$-10^{9} ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
4
1 2 3 4
4
1 -5 1 -5
6
-5 -1 -1 2 -2 -3
----- Sample Output 1 ------
1 1 1 1
4 3 2 1
1 1 3 2 1 1
----- explanation 1 ------
Example case 1. No two elements have different signs, so any alternating subarray may only consist of a single number.
Example case 2. Every subarray is alternating.
Example case 3. The only alternating subarray of length 3 is A3..5. | for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()] + [0]
cur = 1
for i in range(1, n + 1):
if a[i] * a[i - 1] >= 0:
while cur:
print(cur, end=" ")
cur -= 1
cur = 1
else:
cur += 1
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER WHILE VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
There's an array A consisting of N non-zero integers A_{1..N}. A subarray of A is called alternating if any two adjacent elements in it have different signs (i.e. one of them should be negative and the other should be positive).
For each x from 1 to N, compute the length of the longest alternating subarray that starts at x - that is, a subarray A_{x..y} for the maximum possible y ≥ x. The length of such a subarray is y-x+1.
------ Input ------
The first line of the input contains an integer T - the number of test cases.
The first line of each test case contains N.
The following line contains N space-separated integers A_{1..N}.
------ Output ------
For each test case, output one line with N space-separated integers - the lengths of the longest alternating subarray starting at x, for each x from 1 to N.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$-10^{9} ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
4
1 2 3 4
4
1 -5 1 -5
6
-5 -1 -1 2 -2 -3
----- Sample Output 1 ------
1 1 1 1
4 3 2 1
1 1 3 2 1 1
----- explanation 1 ------
Example case 1. No two elements have different signs, so any alternating subarray may only consist of a single number.
Example case 2. Every subarray is alternating.
Example case 3. The only alternating subarray of length 3 is A3..5. | for _ in range(int(input())):
n = int(input())
a = [(0 if int(x) < 0 else 1) for x in input().split()]
i, j, k = 0, 0, 0
while i <= n - 1:
s = 0
j = i
while j <= n - 1:
if j < n - 1:
k = j + 1
if j == n - 1:
k = j
if a[j] != a[k]:
s += 1
j += 1
continue
if a[j] == a[k]:
break
s += 1
for q in range(s, 0, -1):
print(q, end=" ")
i = k
if j == n - 1:
break
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
There's an array A consisting of N non-zero integers A_{1..N}. A subarray of A is called alternating if any two adjacent elements in it have different signs (i.e. one of them should be negative and the other should be positive).
For each x from 1 to N, compute the length of the longest alternating subarray that starts at x - that is, a subarray A_{x..y} for the maximum possible y ≥ x. The length of such a subarray is y-x+1.
------ Input ------
The first line of the input contains an integer T - the number of test cases.
The first line of each test case contains N.
The following line contains N space-separated integers A_{1..N}.
------ Output ------
For each test case, output one line with N space-separated integers - the lengths of the longest alternating subarray starting at x, for each x from 1 to N.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$-10^{9} ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
4
1 2 3 4
4
1 -5 1 -5
6
-5 -1 -1 2 -2 -3
----- Sample Output 1 ------
1 1 1 1
4 3 2 1
1 1 3 2 1 1
----- explanation 1 ------
Example case 1. No two elements have different signs, so any alternating subarray may only consist of a single number.
Example case 2. Every subarray is alternating.
Example case 3. The only alternating subarray of length 3 is A3..5. | for i in range(int(input())):
n = int(input())
y = list(map(int, input().split()))
a = [0] * n
a[-1] = 1
j = 0
while j < n - 1:
c = 1
for i in range(j, n - 1):
if y[i] < 0:
if y[i + 1] > 0:
c += 1
else:
break
elif y[i + 1] < 0:
c += 1
else:
break
s = c
while s > 0:
a[j] = s
j += 1
s -= 1
print(*a) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
There's an array A consisting of N non-zero integers A_{1..N}. A subarray of A is called alternating if any two adjacent elements in it have different signs (i.e. one of them should be negative and the other should be positive).
For each x from 1 to N, compute the length of the longest alternating subarray that starts at x - that is, a subarray A_{x..y} for the maximum possible y ≥ x. The length of such a subarray is y-x+1.
------ Input ------
The first line of the input contains an integer T - the number of test cases.
The first line of each test case contains N.
The following line contains N space-separated integers A_{1..N}.
------ Output ------
For each test case, output one line with N space-separated integers - the lengths of the longest alternating subarray starting at x, for each x from 1 to N.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$-10^{9} ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
3
4
1 2 3 4
4
1 -5 1 -5
6
-5 -1 -1 2 -2 -3
----- Sample Output 1 ------
1 1 1 1
4 3 2 1
1 1 3 2 1 1
----- explanation 1 ------
Example case 1. No two elements have different signs, so any alternating subarray may only consist of a single number.
Example case 2. Every subarray is alternating.
Example case 3. The only alternating subarray of length 3 is A3..5. | def f(arr, n):
ans = [-1] * n
ans[n - 1] = 1
for i in range(n - 2, -1, -1):
if arr[i] < 0:
if arr[i + 1] > 0:
ans[i] = ans[i + 1] + 1
else:
ans[i] = 1
elif arr[i + 1] < 0:
ans[i] = ans[i + 1] + 1
else:
ans[i] = 1
return ans
T = int(input())
while T > 0:
T -= 1
n = int(input())
arr = list(map(int, input().split()))
print(*f(arr, n)) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
ms = -1000000
cs = 0
ans = ""
t = ""
d = {}
for i in range(n):
d[x[i]] = b[i]
for j in range(len(w)):
ans += w[j]
if w[j] not in d:
cs += ord(w[j])
else:
cs += d[w[j]]
if cs > ms:
ms = cs
t = ans
if cs < 0:
cs = 0
ans = ""
return t | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
chr_arr = []
hashmap = dict(zip(x, b))
for i in w:
if i in hashmap:
chr_arr.append(hashmap[i])
continue
chr_arr.append(ord(i))
localmax = globalmax = chr_arr[0]
t_begin = t_end = g_begin = g_end = 0
for i in range(1, len(chr_arr)):
if chr_arr[i] < chr_arr[i] + localmax or localmax == 0:
localmax = chr_arr[i] + localmax
t_end = i
else:
localmax = chr_arr[i]
t_begin = i
t_end = i
if localmax > globalmax:
globalmax = localmax
g_begin = t_begin
g_end = t_end
return w[g_begin : g_end + 1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | from sys import maxsize
class Solution:
def maxSum(self, w, x, b, n):
weight = []
for i in range(len(w)):
value = ord(w[i])
if w[i] in x:
index = x.index(w[i])
value = b[index]
weight.append(value)
max_so_far = -maxsize - 1
max_ending_here = 0
start = 0
end = 0
s = 0
for i in range(0, len(weight)):
max_ending_here += weight[i]
if max_so_far < max_ending_here:
max_so_far = max_ending_here
start = s
end = i
if max_ending_here < 0:
max_ending_here = 0
s = i + 1
return w[start : end + 1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
d = {}
m = ""
min = -1001
for i in range(0, n):
d[x[i]] = b[i]
if b[i] > min:
min = b[i]
m = "" + x[i]
else:
continue
for i in range(0, len(w)):
if d.get(w[i], 1001) == 1001:
d[w[i]] = ord(w[i])
else:
continue
p = min
t = 0
c = m
s = ""
for i in range(0, len(w)):
if t + d[w[i]] > 0 and t + d[w[i]] > p:
s = s + w[i]
c = "" + s
t = t + d[w[i]]
p = 0 + t
elif t + d[w[i]] > 0:
s = s + w[i]
t = t + d[w[i]]
else:
t = 0
s = ""
return c | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
def solve(a, size):
max_so_far = float("-inf") - 1
max_ending_here = 0
start = 0
end = 0
s = 0
for i in range(0, size):
max_ending_here += a[i]
if max_so_far < max_ending_here:
max_so_far = max_ending_here
start = s
end = i
if max_ending_here < 0:
max_ending_here = 0
s = i + 1
return start, end
arr = []
for i in w:
if i not in x:
arr.append(ord(i))
else:
arr.append(b[x.index(i)])
s, e = solve(arr, len(arr))
return w[s : e + 1] | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
d = {}
str = ""
sum = 0
max = -999999
for i, j in zip(x, b):
d[i] = j
for i in w:
str = str + i
if i in d:
sum = sum + d[i]
else:
sum = sum + ord(i)
if sum > max:
max = sum
res = str
if sum < 0:
sum = 0
str = ""
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
csum = 0
msf = 0
dic = {}
for ch in w:
dic[ch] = ord(ch)
for i in range(0, len(x)):
dic[x[i]] = b[i]
i = 0
start = 0
end = 0
s = 0
while i < len(w):
if csum >= 0:
csum += dic[w[i]]
else:
csum = dic[w[i]]
s = i
if msf < csum:
msf = csum
start = s
end = i
i += 1
return w[start : end + 1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
char_dict = {}
for ch, val in zip(x, b):
char_dict[ch] = val
total, max_total = 0, float("-inf")
l = 0
res = ""
for i in range(len(w)):
if w[i] in char_dict:
total += char_dict[w[i]]
else:
total += ord(w[i])
if max_total < total:
max_total = total
res = w[l : i + 1]
if total < 0:
total = 0
l = i + 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
c = {i: j for i, j in zip(x, b)}
t = 0
m = -(10**9)
s = ""
ts = ""
for i in w:
ts += i
if i in c:
t += c[i]
else:
t += ord(i)
if m < t:
s = ts
m = t
if t < 0:
t = 0
ts = ""
return s | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
max_s = 0
maxsum = -10000
d = {}
s, start, stop = 0, 0, 0
for i in range(n):
d[x[i]] = b[i]
for i in range(len(w)):
if w[i] in d:
max_s += d[w[i]]
else:
max_s += ord(w[i])
if max_s > maxsum:
maxsum = max_s
start = s
stop = i
if max_s < 0:
max_s = 0
s = i + 1
return w[start : stop + 1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | from sys import maxsize
class Solution:
def maxSum(self, w, x, b, n):
asci = [(b[x.index(i)] if i in x else ord(i)) for i in w]
maxf = -maxsize - 1
maxe = start = end = s = 0
for i in range(len(asci)):
maxe += asci[i]
if maxf < maxe:
maxf = maxe
start = s
end = i
if maxe < 0:
maxe = 0
s = i + 1
return w[start : end + 1] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
d = dict()
for i in range(len(x)):
d[x[i]] = b[i]
s = 0
l = 0
m = -99999
for i in range(len(w)):
if w[i] not in d:
s += ord(w[i])
else:
s += d[w[i]]
if s > m:
ans = w[l : i + 1]
m = s
if s < 0:
s = 0
l = i + 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | import sys
class Solution:
def maxSum(self, w, x, b, n):
max_so_far = -sys.maxsize
max_current = 0
i = 0
j = 0
s = 0
if n == 0:
return w
for cur in range(len(w)):
new_asc = ord(w[cur])
if w[cur] in x:
new_asc = b[x.index(w[cur])]
max_current += new_asc
if max_so_far < max_current:
max_so_far = max_current
i = s
j = cur
if max_current < 0:
max_current = 0
s = cur + 1
return w[i : j + 1] | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
cur_max = 0
overall_max = -100000000
cur_string = ""
overall_string = ""
for c in w:
n = ord(c) if c not in x else b[x.index(c)]
cur_max += n
cur_string += c
if cur_max > overall_max:
overall_max = cur_max
overall_string = cur_string
if cur_max < 0:
cur_max = 0
cur_string = ""
return overall_string | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | import sys
class Solution:
def maxSum(self, w, x, b, n):
def getASCII(c):
if w[i] in x:
return b[x.index(w[i])]
else:
return ord(w[i])
sum = 0
max_sum = ord(w[0])
start = 0
cur = 0
end = 0
for i in range(len(w)):
sum += getASCII(w[i])
if sum > max_sum:
start = cur
end = i
max_sum = sum
if sum < 0:
sum = 0
cur = i + 1
ans = []
for i in range(start, end + 1):
ans.append(w[i])
return "".join(ans) | IMPORT CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
re = {}
for i in range(n):
re[x[i]] = b[i]
arr = []
for i in range(len(w)):
if w[i] in re:
arr.append(re[w[i]])
else:
arr.append(ord(w[i]))
pos = 0
maxi = -(10**9)
s = 0
prev = 0
st = ""
ans = ""
for i in range(len(arr)):
s += arr[i]
st += w[i]
if arr[i] > s:
st = w[i]
s = arr[i]
if s > maxi:
maxi = s
ans = st
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
d = {}
s = float("-inf")
ans = float("-inf")
for i in range(n):
d[x[i]] = b[i]
x = 0
res = [-1, -1]
for i in range(len(w)):
if w[i] in d:
val = d[w[i]]
else:
val = ord(w[i])
if s + val < val:
j = i
s = val
else:
s += val
if ans < s:
res[0] = j
res[1] = i
ans = s
return w[res[0] : res[1] + 1] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR RETURN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
map = {}
char = "a"
while char <= "z":
map[char] = ord(char)
char = chr(ord(char) + 1)
char = "A"
while char <= "Z":
map[char] = ord(char)
char = chr(ord(char) + 1)
for i in range(n):
map[x[i]] = b[i]
res, sum, str, start = map[w[0]], 0, w[0], 0
for i in range(len(w)):
x = w[i]
sum += map[x]
if res < sum:
res = sum
str = w[start : i + 1]
if sum < 0:
sum, start = 0, i + 1
return str | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR STRING WHILE VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING WHILE VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | from sys import maxsize
class Solution:
def maxSum(self, w, x, b, n):
arr = []
for i in w:
if i in x:
ind = x.index(i)
arr.append(b[ind])
else:
arr.append(ord(i))
current_sum = max_sum = arr[0]
if len(arr) == 0:
return 0
max_so_far = -maxsize - 1
max_ending_here = 0
start = end = s = 0
for i in range(0, len(arr)):
max_ending_here += arr[i]
if max_so_far < max_ending_here:
max_so_far = max_ending_here
start = s
end = i
if max_ending_here < 0:
max_ending_here = 0
s = i + 1
return w[start : end + 1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
char_weight = {ch: wt for ch, wt in zip(x, b)}
res, temp = "", ""
res_sum, curr_sum = float("-inf"), 0
for ch in w:
val = ord(ch) if ch not in char_weight else char_weight[ch]
if val <= curr_sum + val:
curr_sum += val
temp += ch
else:
curr_sum = val
temp = ch
if curr_sum > res_sum:
res_sum = curr_sum
res = temp
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING STRING ASSIGN VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
eval_char = lambda a: b[x.index(a)] if a in x else ord(a)
len_w = len(w)
i = 0
j = 0
max_str = w[j]
max_sum = summ = eval_char(w[j])
while j < len_w - 1:
if i < j and i < 0:
summ -= eval_char(w[i])
i += 1
elif summ < 0:
j += 1
i = j
summ = eval_char(w[i])
else:
j += 1
summ += eval_char(w[j])
if summ > max_sum:
max_sum = summ
max_str = w[i : j + 1]
return max_str | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
d = {}
for i in range(n):
d[x[i]] = b[i]
arr = []
for i in w:
if i in d:
arr.append(d[i])
else:
arr.append(ord(i))
if len(arr) == 1:
return w
m = 0
res = 0
s = ""
ans = ""
for i in range(len(arr)):
m = max(arr[i], m + arr[i])
if m < 0:
s = ""
continue
s += w[i]
if m >= res:
res = m
ans = s
if len(ans) == 0:
m = arr[0]
res = arr[0]
ans = w[0]
for i in range(1, len(arr)):
m = max(arr[i], m)
if m > res:
res = m
ans = w[i]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR STRING VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
max = -9999
sum = 0
asc = 0
z = ""
ans = ""
v = {}
for i, j in zip(x, b):
v[i] = j
for i in range(len(w)):
if w[i] in v:
asc = v[w[i]]
else:
asc = ord(w[i])
sum = sum + asc
z = z + w[i]
if sum > max:
max = sum
ans = z
if sum < 0:
sum = 0
z = ""
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
def helper(char):
if char in x:
return b[x.index(char)]
return ord(char)
total = 0
ml, mr, l, r = 0, 0, 0, 0
Max = float("-inf")
for i in range(len(w)):
curval = helper(w[i])
if curval > curval + total:
total = curval
l = i
r = i
else:
total += curval
r = i
if total > Max:
Max = total
ml = l
mr = r
return w[ml : mr + 1] | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
mp = {}
for i in range(n):
mp[x[i]] = b[i]
asciiList = []
for i in w:
if i in mp.keys():
asciiList.append(mp[i])
else:
asciiList.append(ord(i))
ans = ""
max_sum = int(-1000000000.0)
local_sum = 0
local_ans = ""
for i in range(len(w)):
if asciiList[i] > local_sum + asciiList[i]:
local_ans = w[i]
local_sum = asciiList[i]
else:
local_ans += w[i]
local_sum += asciiList[i]
if local_sum > max_sum:
ans = local_ans
max_sum = local_sum
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined.
Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i].
Example 1:
Input:
W = abcde
N = 1
X[] = { 'c' }
B[] = { -1000 }
Output:
de
Explanation:
Substring "de" has the
maximum sum of ascii value,
including c decreases the sum value
Example 2:
Input:
W = dbfbsdbf
N = 2
X[] = { 'b','s' }
B[] = { -100, 45 }
Output:
dbfbsdbf
Explanation:
Substring "dbfbsdbf" has the maximum
sum of ascii value.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value.
Expected Time Complexity: O(|W|)
Expected Auxiliary Space: O(1)
Constraints:
1<=|W|<=100000
1<=|X|<=52
-1000<=B_{i}<=1000
Each character of W and A will be from a-z, A-Z. | class Solution:
def maxSum(self, w, x, b, n):
s = 0
cs = -float("inf")
ss = ""
css = ""
for c in w:
if c in x:
i = x.index(c)
s += b[i]
ss = ss + x[i]
else:
s += ord(c)
ss = ss + c
if cs < s:
cs = s
css = ss
if s < 0:
s = 0
ss = ""
return css | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.