description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
-----Input-----
The first line contains three integers n, m and k (1 β€ n, m β€ 500, 0 β€ k β€ 500) β the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
-----Output-----
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
-----Examples-----
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
-----Note-----
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day. | def min_sub_array(day, k):
if not day:
return [0] * (k + 1)
n = len(day)
best = [float("inf")] * (n + 1)
best[0] = 0
best[1] = 1
for size in range(2, n + 1):
for i in range(n + 1 - size):
best[size] = min(best[size], day[i + size - 1] - day[i] + 1)
output = [0] * (k + 1)
for i in range(k + 1):
if n - i > 0:
output[i] = best[n - i]
return output
N, M, K = list(map(int, input().split()))
day = [i for i, val in enumerate(input()) if val == "1"]
best = min_sub_array(day, K)
for _ in range(N - 1):
day = [i for i, val in enumerate(input()) if val == "1"]
new_day_best = min_sub_array(day, K)
new_best = [float("inf")] * (K + 1)
for i in range(K + 1):
for j in range(i + 1):
new_best[i] = min(new_best[i], new_day_best[j] + best[i - j])
best = new_best
print(best[K]) | FUNC_DEF IF VAR RETURN BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
-----Input-----
The first line contains three integers n, m and k (1 β€ n, m β€ 500, 0 β€ k β€ 500) β the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
-----Output-----
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
-----Examples-----
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
-----Note-----
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day. | import sys
n, m, k = map(int, input().split())
table = [input() for _ in range(n)]
dp = [0] * (k + 1)
for a in table:
one = []
for i in range(m):
if a[i] == "1":
one.append(i)
if not one:
continue
ni = len(one)
subdp = [10**9] * (ni + 1)
subdp[-1] = 0
for i in range(ni):
for j in range(i, ni):
subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1)
next_dp = [10**9] * (k + 1)
for i in range(k, -1, -1):
for j in range(ni + 1):
if i + j > k:
break
next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j])
dp = next_dp
print(min(dp)) | IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Ivan is a student at Berland State University (BSU). There are n days in Berland week, and each of these days Ivan might have some classes at the university.
There are m working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during i-th hour, and last lesson is during j-th hour, then he spends j - i + 1 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university.
Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than k lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all.
Given n, m, k and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than k lessons?
-----Input-----
The first line contains three integers n, m and k (1 β€ n, m β€ 500, 0 β€ k β€ 500) β the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively.
Then n lines follow, i-th line containing a binary string of m characters. If j-th character in i-th line is 1, then Ivan has a lesson on i-th day during j-th hour (if it is 0, there is no such lesson).
-----Output-----
Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than k lessons.
-----Examples-----
Input
2 5 1
01001
10110
Output
5
Input
2 5 0
01001
10110
Output
8
-----Note-----
In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day.
In the second example Ivan can't skip any lessons, so he spends 4 hours every day. | n, m, k = map(int, input().split())
DATA = [input() for i in range(n)]
INF = 1 << 60
dp = [([INF] * (k + 10)) for i in range(n + 10)]
dp[0][0] = 0
COST = [([INF] * (k + 10)) for i in range(n + 10)]
for i, string in enumerate(DATA):
stack = []
for j in range(m):
if string[j] == "1":
stack.append(j)
L = len(stack)
for j in range(k + 10):
if j >= L:
COST[i + 1][j] = 0
continue
else:
for pos in range(j + 1):
l = pos
r = pos + L - 1 - j
COST[i + 1][j] = min(COST[i + 1][j], stack[r] - stack[l] + 1)
for day in range(1, n + 1):
for used_cost in range(k + 1):
dp[day][used_cost] = min(
dp[day - 1][prev_cost] + COST[day][used_cost - prev_cost]
for prev_cost in range(used_cost + 1)
)
ans = min(dp[n][used_cost] for used_cost in range(k + 1))
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, M, N):
N, M = len(points), len(points[0])
dp = [[(0) for _ in range(M)] for _ in range(N)]
for i in range(N - 1, -1, -1):
for j in range(M - 1, -1, -1):
if i == N - 1 and j == M - 1:
temp = points[i][j]
dp[i][j] = 0 if temp >= 0 else temp
elif i == N - 1 and j != M - 1:
temp = points[i][j] + dp[i][j + 1]
dp[i][j] = 0 if temp >= 0 else temp
elif i != N - 1 and j == M - 1:
temp = points[i][j] + dp[i + 1][j]
dp[i][j] = 0 if temp >= 0 else temp
elif i != N - 1 and j != M - 1:
temp = max(points[i][j] + dp[i + 1][j], points[i][j] + dp[i][j + 1])
dp[i][j] = 0 if temp >= 0 else temp
ans = abs(dp[0][0]) + 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN VAR |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, M, N):
k = max(M, N)
dp = [([-1] * (k + 1)) for i in range(k + 1)]
return self.helperFunction(points, M, N, 0, 0, dp)
def helperFunction(self, points, M, N, x, y, dp):
if x == M - 1 and y == N - 1:
if points[x][y] <= 0:
return 1 + abs(points[x][y])
else:
return 1
if x == M or y == N:
return float("inf")
if dp[x][y] != -1:
return dp[x][y]
down = self.helperFunction(points, M, N, x + 1, y, dp)
left = self.helperFunction(points, M, N, x, y + 1, dp)
res = min(down, left) - points[x][y]
if res <= 0:
dp[x][y] = 1
else:
dp[x][y] = res
return dp[x][y] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, n, m):
inf = float("inf")
dp = [[(0) for i in range(m)] for j in range(n)]
for i in range(n - 1, -1, -1):
for j in range(m - 1, -1, -1):
if i == n - 1 and j == m - 1:
if points[n - 1][m - 1] > 0:
dp[n - 1][m - 1] = 1
else:
dp[n - 1][m - 1] = abs(points[n - 1][m - 1]) + 1
elif i == n - 1:
dp[i][j] = max(1, dp[i][j + 1] - points[i][j])
elif j == m - 1:
dp[i][j] = max(1, dp[i + 1][j] - points[i][j])
else:
dp[i][j] = max(1, min(dp[i][j + 1], dp[i + 1][j]) - points[i][j])
return dp[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, grid, n, m):
t = [[(0) for i in range(m)] for j in range(n)]
for i in range(n - 1, -1, -1):
for j in range(m - 1, -1, -1):
if i == n - 1 and j == m - 1:
if grid[i][j] < 0:
t[i][j] = abs(grid[i][j]) + 1
else:
t[i][j] = 1
elif i == n - 1:
t[i][j] = max(t[i][j + 1] - grid[i][j], 1)
elif j == m - 1:
t[i][j] = max(t[i + 1][j] - grid[i][j], 1)
else:
t[i][j] = max(min(t[i + 1][j], t[i][j + 1]) - grid[i][j], 1)
return t[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, n, m):
dp = [[float("inf") for j in range(m + 1)] for i in range(n + 1)]
dp[n][m - 1] = 1
dp[n - 1][m] = 1
for i in range(n - 1, -1, -1):
for j in range(m - 1, -1, -1):
minPoints = min(dp[i + 1][j], dp[i][j + 1]) - points[i][j]
dp[i][j] = 1 if minPoints <= 0 else minPoints
return dp[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR RETURN VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | def f(A, N, M, x, y, dp):
if x >= N or y >= M:
return 1000000000.0
if x == N - 1 and y == M - 1:
if A[x][y] <= 0:
return 1 + abs(A[x][y])
else:
return 1
if dp[x][y] != -1:
return dp[x][y]
down = f(A, N, M, x + 1, y, dp)
right = f(A, N, M, x, y + 1, dp)
ans = min(down, right) - A[x][y]
if ans <= 0:
ans = 1
dp[x][y] = ans
return ans
class Solution:
def minPoints(self, points, M, N):
dp = [[(-1) for i in range(N + 1)] for i in range(M + 1)]
ans = 0
try:
ans = f(points, M, N, 0, 0, dp)
except Exception as e:
print(e)
return ans | FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | import sys
class Solution:
def minPoints(self, points, M, N):
return dp_mp2reach_destination(points)
def _dp_mp2reach_destination(grid, rind, cind, r_length, c_length, dp_matrix):
if rind >= r_length or cind >= c_length:
return sys.maxsize
if dp_matrix[rind][cind] != -1:
return dp_matrix[rind][cind]
if rind == r_length - 1 and cind == c_length - 1:
return 1 if grid[rind][cind] >= 0 else abs(grid[rind][cind]) + 1
min_value = min(
_dp_mp2reach_destination(grid, rind, cind + 1, r_length, c_length, dp_matrix),
_dp_mp2reach_destination(grid, rind + 1, cind, r_length, c_length, dp_matrix),
)
dp_matrix[rind][cind] = max(min_value - grid[rind][cind], 1)
return dp_matrix[rind][cind]
def dp_mp2reach_destination(grid):
row_length = len(grid)
col_length = len(grid[0])
dp_matrix = [[(-1) for i in range(col_length + 1)] for j in range(row_length + 1)]
min_points = _dp_mp2reach_destination(grid, 0, 0, row_length, col_length, dp_matrix)
return min_points | IMPORT CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR RETURN VAR |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, M, N):
result = [[(0) for i in range(N)] for j in range(M)]
if points[M - 1][N - 1] > 0:
result[M - 1][N - 1] = 1
else:
result[M - 1][N - 1] = abs(points[M - 1][N - 1]) + 1
for i in range(M - 2, -1, -1):
result[i][N - 1] = max(result[i + 1][N - 1] - points[i][N - 1], 1)
for i in range(N - 2, -1, -1):
result[M - 1][i] = max(result[M - 1][i + 1] - points[M - 1][i], 1)
for i in range(M - 2, -1, -1):
for j in range(N - 2, -1, -1):
min_points = min(result[i + 1][j], result[i][j + 1])
result[i][j] = max(min_points - points[i][j], 1)
return result[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, l, n, m):
n = len(l)
m = len(l[0])
dp = [[(0) for _ in range(m)] for _ in range(n)]
dp[-1][-1] = min(0, l[-1][-1])
for i in range(n - 2, -1, -1):
dp[i][-1] = min(0, dp[i + 1][-1] + l[i][-1])
for i in range(m - 2, -1, -1):
dp[-1][i] = min(0, dp[-1][i + 1] + l[-1][i])
for i in range(n - 2, -1, -1):
for j in range(m - 2, -1, -1):
dp[i][j] = min(0, l[i][j] + max(dp[i + 1][j], dp[i][j + 1]))
return 1 - dp[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, M, N):
r, c, grid = M, N, points
dp = [[(0) for i in range(c)] for j in range(r)]
if grid[r - 1][c - 1] < 0:
dp[r - 1][c - 1] = 1 - grid[r - 1][c - 1]
else:
dp[r - 1][c - 1] = 1
for i in range(c - 2, -1, -1):
dp[r - 1][i] = max(1, dp[r - 1][i + 1] - grid[r - 1][i])
for i in range(r - 2, -1, -1):
dp[i][c - 1] = max(1, dp[i + 1][c - 1] - grid[i][c - 1])
for i in range(r - 2, -1, -1):
for j in range(c - 2, -1, -1):
dp[i][j] = max(1, min(dp[i][j + 1], dp[i + 1][j]) - grid[i][j])
return dp[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, M, N):
for i in range(M - 1, -1, -1):
for j in range(N - 1, -1, -1):
if i == M - 1 and j == N - 1:
if points[i][j] > 0:
points[i][j] = 0
elif i == M - 1:
x = points[i][j] + points[i][j + 1]
points[i][j] = min(0, x)
elif j == N - 1:
x = points[i][j] + points[i + 1][j]
points[i][j] = min(0, x)
else:
x = points[i][j] + max(points[i][j + 1], points[i + 1][j])
points[i][j] = min(0, x)
return abs(points[0][0]) + 1
if __name__ == "__main__":
T = int(input())
for i in range(T):
m, n = input().split()
m, n = int(m), int(n)
points = []
for _ in range(m):
temp = [int(x) for x in input().split()]
points.append(temp)
ob = Solution()
ans = ob.minPoints(points, m, n)
print(ans) | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, m, n):
points[m - 1][n - 1] = min(points[m - 1][n - 1], 0)
for i in range(m - 2, -1, -1):
points[i][n - 1] = min(points[i][n - 1] + points[i + 1][n - 1], 0)
for i in range(n - 2, -1, -1):
points[m - 1][i] = min(points[m - 1][i] + points[m - 1][i + 1], 0)
for i in range(m - 2, -1, -1):
for j in range(n - 2, -1, -1):
points[i][j] = min(
0, points[i][j] + max(points[i + 1][j], points[i][j + 1])
)
return abs(points[0][0]) + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | import sys
sys.setrecursionlimit(10**6)
class Solution:
def minPoints(self, points, N, M):
dp = [[(-1) for i in range(M + 1)] for j in range(N + 1)]
def recursive(points, dp, N, M, i=0, j=0):
if i >= N or j >= M:
return float("inf")
if i == N - 1 and j == M - 1:
if points[i][j] <= 0:
return abs(points[i][j] - 1)
else:
return 1
if dp[i][j] != -1:
return dp[i][j]
op1 = recursive(points, dp, N, M, i + 1, j)
op2 = recursive(points, dp, N, M, i, j + 1)
dp[i][j] = min(op1, op2) - points[i][j]
if dp[i][j] <= 0:
dp[i][j] = 1
return dp[i][j]
return recursive(points, dp, N, M) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF NUMBER NUMBER IF VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | import sys
class Solution:
def minPoints(self, points, M, N):
t = [[(-1) for i in range(N + 1)] for j in range(M + 1)]
y = self.fn(0, 0, M, N, points, t)
return y
def fn(self, i, j, m, n, grid, t):
if i >= m or j >= n:
return sys.maxsize
if i == m - 1 and j == n - 1:
if grid[i][j] <= 0:
return abs(grid[i][j]) + 1
else:
return 1
if t[i][j] != -1:
return t[i][j]
dir1 = self.fn(i + 1, j, m, n, grid, t)
dir2 = self.fn(i, j + 1, m, n, grid, t)
mincost = min(dir1, dir2) - grid[i][j]
if mincost <= 0:
t[i][j] = 1
else:
t[i][j] = mincost
return t[i][j] | IMPORT 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 FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, M, N):
dp = [([10**9] * (N + 1)) for i in range(M + 1)]
dp[M - 1][N] = 1
dp[M][N - 1] = 1
for i in range(M - 1, -1, -1):
for j in range(N - 1, -1, -1):
dp[i][j] = max(min(dp[i + 1][j], dp[i][j + 1]) - points[i][j], 1)
return dp[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, M, N):
m, n = M, N
dp = [[(-1) for i in range(n)] for j in range(m)]
dp[m - 1][n - 1] = 1 if points[m - 1][n - 1] >= 0 else 1 - points[m - 1][n - 1]
for j in range(n - 2, -1, -1):
if points[m - 1][j] >= 0:
dp[m - 1][j] = max(1, dp[m - 1][j + 1] - points[m - 1][j])
else:
dp[m - 1][j] = dp[m - 1][j + 1] - points[m - 1][j]
for i in range(m - 2, -1, -1):
if points[i][n - 1] >= 0:
dp[i][n - 1] = max(1, dp[i + 1][n - 1] - points[i][n - 1])
else:
dp[i][n - 1] = dp[i + 1][n - 1] - points[i][n - 1]
for i in range(m - 2, -1, -1):
for j in range(n - 2, -1, -1):
if points[i][j] >= 0:
dp[i][j] = max(min(dp[i + 1][j], dp[i][j + 1]) - points[i][j], 1)
else:
dp[i][j] = min(dp[i + 1][j], dp[i][j + 1]) - points[i][j]
return dp[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, m, n):
dp = []
for i in range(m):
temp = []
for j in range(n):
temp.append(0)
dp.append(temp)
if points[m - 1][n - 1] < 1:
dp[m - 1][n - 1] = 1 - points[m - 1][n - 1]
else:
dp[m - 1][n - 1] = 1
for i in range(m - 2, -1, -1):
current = points[i][n - 1]
if current < 0:
current = dp[i + 1][n - 1] - current
else:
current = max(1, dp[i + 1][n - 1] - points[i][n - 1])
dp[i][n - 1] = current
for i in range(n - 2, -1, -1):
current = points[m - 1][i]
if current < 0:
current = dp[m - 1][i + 1] - current
else:
current = max(1, dp[m - 1][i + 1] - points[m - 1][i])
dp[m - 1][i] = current
for i in range(m - 2, -1, -1):
for j in range(n - 2, -1, -1):
dp[i][j] = max(min(dp[i + 1][j], dp[i][j + 1]) - points[i][j], 1)
return dp[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, M, N):
mat = [[(0) for i in range(N)] for j in range(M)]
if points[-1][-1] >= 0:
mat[-1][-1] = 1
else:
mat[-1][-1] = abs(points[-1][-1]) + 1
for i in range(M - 2, -1, -1):
x = mat[i + 1][N - 1] - points[i][N - 1]
if x <= 0:
mat[i][N - 1] = 1
else:
mat[i][N - 1] = x
for i in range(N - 2, -1, -1):
x = mat[M - 1][i + 1] - points[M - 1][i]
if x <= 0:
mat[M - 1][i] = 1
else:
mat[M - 1][i] = x
for i in range(M - 2, -1, -1):
for j in range(N - 2, -1, -1):
x = min(mat[i + 1][j], mat[i][j + 1]) - points[i][j]
if x <= 0:
mat[i][j] = 1
else:
mat[i][j] = x
return mat[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | import sys
class Solution:
def minPoints(self, points, M, N):
dp = [([-1] * N) for _ in range(M)]
def solve(i, j):
if i == M - 1 and j == N - 1:
return -1 * points[i][j] + 1 if points[i][j] <= 0 else 1
if i > M - 1 or j > N - 1:
return sys.maxsize
if dp[i][j] != -1:
return dp[i][j]
right = solve(i, j + 1)
down = solve(i + 1, j)
minHealth = min(right, down) - points[i][j]
if minHealth <= 0:
dp[i][j] = 1
else:
dp[i][j] = minHealth
return dp[i][j]
return solve(0, 0) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, arr, M, N):
dp = [[(78263476) for j in range(len(arr[0]))] for i in range(len(arr))]
for i in range(len(arr) - 1, -1, -1):
for j in range(len(arr[0]) - 1, -1, -1):
if i == len(arr) - 1 and j == len(arr[0]) - 1:
dp[i][j] = min(0, arr[i][j])
else:
choice1, choice2 = 78263476, 78263476
if i + 1 < len(arr):
choice1 = min(dp[i + 1][j] + arr[i][j], 0)
if j + 1 < len(arr[0]):
choice2 = min(dp[i][j + 1] + arr[i][j], 0)
if abs(choice1) > abs(choice2):
dp[i][j] = choice2
else:
dp[i][j] = choice1
return abs(dp[0][0]) + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | import sys
class Solution:
def minPoints(self, dungeon, m, n):
memo = [[(-1) for _ in range(n)] for i in range(m)]
return self.recursion(dungeon, 0, 0, memo)
def recursion(self, grid, i, j, memo):
m = len(grid)
n = len(grid[0])
if i == m - 1 and j == n - 1:
return 1 if grid[i][j] >= 0 else -grid[i][j] + 1
if i == m or j == n:
return sys.maxsize
if memo[i][j] != -1:
return memo[i][j]
res = (
min(
self.recursion(grid, i, j + 1, memo),
self.recursion(grid, i + 1, j, memo),
)
- grid[i][j]
)
memo[i][j] = 1 if res <= 0 else res
return memo[i][j] | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR RETURN VAR VAR VAR |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, M, N):
d = [[(0) for i in range(N)] for j in range(M)]
d[M - 1][N - 1] = 0
if points[M - 1][N - 1] < 0:
d[M - 1][N - 1] = points[M - 1][N - 1]
for i in range(M - 1, -1, -1):
for j in range(N - 1, -1, -1):
if i == M - 1 and j == N - 1:
continue
if i + 1 < M and j + 1 < N:
d[i][j] = points[i][j] + max(d[i + 1][j], d[i][j + 1])
if d[i][j] > 0:
d[i][j] = 0
elif i + 1 < M:
d[i][j] = points[i][j] + d[i + 1][j]
if d[i][j] > 0:
d[i][j] = 0
elif j + 1 < N:
d[i][j] = points[i][j] + d[i][j + 1]
if d[i][j] > 0:
d[i][j] = 0
return abs(d[0][0]) + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, M, N):
dp = [([float("inf")] * (N + 1)) for i in range(M + 1)]
dp[M][N - 1] = 1
dp[M - 1][N] = 1
for i in reversed(range(M)):
for j in reversed(range(N)):
dp[i][j] = max(min(dp[i + 1][j], dp[i][j + 1]) - points[i][j], 1)
return dp[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, m, n):
dp = []
for i in range(m + 1):
dp.append([10**9] * (n + 1))
dp[m - 1][n] = 1
dp[m][n - 1] = 1
for i in range(m - 1, -1, -1):
for j in range(n - 1, -1, -1):
x = min(dp[i + 1][j], dp[i][j + 1]) - points[i][j]
if x <= 0:
dp[i][j] = 1
else:
dp[i][j] = x
return dp[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given a grid of size M*N with each cell consisting of an integer which represents points. We can move across a cell only if we have positive points. Whenever we pass through a cell, points in that cell are added to our overall points, the task is to find minimum initial points to reach cell (m-1, n-1) from (0, 0) by following these certain set of rules :
1. From a cell (i, j) we can move to (i + 1, j) or (i, j + 1).
2. We cannot move from (i, j) if your overall points at (i, j) are <= 0.
3. We have to reach at (n-1, m-1) with minimum positive points i.e., > 0.
Example 1:
Input: M = 3, N = 3
arr[][] = {{-2,-3,3},
{-5,-10,1},
{10,30,-5}};
Output: 7
Explanation: 7 is the minimum value to
reach the destination with positive
throughout the path. Below is the path.
(0,0) -> (0,1) -> (0,2) -> (1, 2) -> (2, 2)
We start from (0, 0) with 7, we reach
(0, 1) with 5, (0, 2) with 2, (1, 2)
with 5, (2, 2) with and finally we have
1 point (we needed greater than 0 points
at the end).
Example 2:
Input: M = 3, N = 2
arr[][] = {{2,3},
{5,10},
{10,30}};
Output: 1
Explanation: Take any path, all of them
are positive. So, required one point
at the start
Your Task:
You don't need to read input or print anything. Complete the function minPoints() which takes N, M and 2-d vector as input parameters and returns the integer value
Expected Time Complexity: O(N*M)
Expected Auxiliary Space: O(N*M)
Constraints:
1 β€ N β€ 10^{3} | class Solution:
def minPoints(self, points, M, N):
min_st_cost = [([float("inf")] * N) for i in range(M)]
min_st_cost[M - 1][N - 1] = (
abs(points[M - 1][N - 1]) if points[M - 1][N - 1] < 0 else 0
)
for i in range(M - 1, -1, -1):
for j in range(N - 1, -1, -1):
neighbours = [(i - 1, j), (i, j - 1)]
curr_coins = min_st_cost[i][j]
for neighbour in neighbours:
curr_i, curr_j = neighbour
if curr_i < 0 or curr_j < 0:
continue
new_coins = curr_coins - points[curr_i][curr_j]
if new_coins < 0:
new_coins = 0
min_st_cost[curr_i][curr_j] = min(
new_coins, min_st_cost[curr_i][curr_j]
)
final_value = min_st_cost[0][0] + 1
return final_value | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
a = len(s1)
b = len(s2)
dp = [([0] * (b + 1)) for i in range(a + 1)]
for i in range(1, b + 1):
dp[0][i] = i
for i in range(1, a + 1):
dp[i][0] = i
for i in range(1, a + 1):
charA = s1[i - 1]
for j in range(1, b + 1):
charB = s2[j - 1]
if charB == charA:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1])
return dp[a][b] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
n = len(s1)
m = len(s2)
prev = [(0) for _ in range(m + 1)]
curr = [(0) for _ in range(m + 1)]
for row in range(1, n + 1):
for col in range(1, m + 1):
if s1[row - 1] == s2[col - 1]:
curr[col] = 1 + prev[col - 1]
else:
curr[col] = max(prev[col], curr[col - 1])
prev = list(curr)
return n + m - 2 * prev[m] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
m = len(s1)
n = len(s2)
memo = [[(-1) for _ in range(n + 1)] for _ in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0:
memo[i][j] = 0
elif s1[i - 1] == s2[j - 1]:
memo[i][j] = 1 + memo[i - 1][j - 1]
else:
memo[i][j] = max(memo[i][j - 1], memo[i - 1][j])
ans = m - memo[-1][-1] + (n - memo[-1][-1])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER RETURN VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
l1 = len(s1)
l2 = len(s2)
dp = [[(0) for x in range(l2 + 1)] for x in range(l1 + 1)]
for i in range(l1 + 1):
for j in range(l2 + 1):
if i * j == 0:
dp[i][j] = 0
for i in range(1, l1 + 1):
for j in range(1, l2 + 1):
ind1 = i - 1
ind2 = j - 1
if s1[ind1] == s2[ind2]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
dele = max(l1, l2) - dp[l1][l2]
ins = min(l1, l2) - dp[l1][l2]
return ins + dele | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP 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 BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def ls(self, x, y, s1, s2, dp):
if x == 0 or y == 0:
return 0
if dp[x][y] != -1:
return dp[x][y]
if s1[x - 1] == s2[y - 1]:
dp[x][y] = 1 + self.ls(x - 1, y - 1, s1, s2, dp)
return dp[x][y]
else:
dp[x][y] = max(self.ls(x - 1, y, s1, s2, dp), self.ls(x, y - 1, s1, s2, dp))
return dp[x][y]
def minOperations(self, s1, s2):
m = len(s1)
n = len(s2)
dp = [[(-1) for _ in range(n + 1)] for _ in range(m + 1)]
lcs = self.ls(m, n, s1, s2, dp)
noofdeletions = len(s1) - lcs
noofinsertions = len(s2) - lcs
return noofdeletions + noofinsertions | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
x = len(s1)
y = len(s2)
subset = [([-1] * (y + 1)) for i in range(x + 1)]
for i in range(x + 1):
subset[i][0] = 0
for j in range(y + 1):
subset[0][j] = 0
for i in range(1, x + 1):
for j in range(1, y + 1):
if s1[i - 1] == s2[j - 1]:
subset[i][j] = 1 + subset[i - 1][j - 1]
else:
subset[i][j] = max(subset[i - 1][j], subset[i][j - 1])
return len(s1) - subset[x][y] + len(s2) - subset[x][y] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def f(self, s, t, x, y):
prev = [0] * (y + 1)
for ind1 in range(1, x + 1):
curr = [0] * (y + 1)
for ind2 in range(1, y + 1):
if s[ind1 - 1] == t[ind2 - 1]:
curr[ind2] = 1 + prev[ind2 - 1]
else:
curr[ind2] = max(prev[ind2], curr[ind2 - 1])
prev = curr
return prev[y]
def minOperations(self, s1, s2):
n = len(s1)
m = len(s2)
lcs = self.f(s1, s2, n, m)
deletions = n - lcs
insetions = m - lcs
return deletions + insetions | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
l1 = len(s1)
l2 = len(s2)
dp = [[(0) for i in range(l2 + 1)] for j in range(l1 + 1)]
for i in range(1, l1 + 1):
for j in range(1, l2 + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return l1 - dp[l1][l2] + (l2 - dp[l1][l2])
dp = [[(-1) for i in range(l2)] for j in range(l1)]
def solve(m, n):
if m == l1 or n == l2:
return 0
elif dp[m][n] != -1:
return dp[m][n]
elif s1[m] == s2[n]:
dp[m][n] = 1 + solve(m + 1, n + 1)
else:
dp[m][n] = max(solve(m + 1, n), solve(m, n + 1))
return dp[m][n]
a = solve(0, 0)
return l1 - a + (l2 - a) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | import itertools
class Solution:
def minOperations(self, s1, s2):
m = len(s1)
n = len(s2)
dp = [([0] * (n + 1)) for _ in range(m + 1)]
for i, j in itertools.product(range(1, m + 1), range(1, n + 1)):
dp[i][j] = (
1 + dp[i - 1][j - 1]
if s1[i - 1] == s2[j - 1]
else max(dp[i - 1][j], dp[i][j - 1])
)
ins = m - dp[m][n]
dele = n - dp[m][n]
return ins + dele | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER 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 BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
dp = [[(0) for i in range(len(s2) + 1)] for _ in range(len(s1) + 1)]
for i in range(len(s1) + 1):
for j in range(len(s2) + 1):
if i == len(s1) and j != len(s2):
dp[i][j] = len(s2) - j
elif i != len(s1) and j == len(s2):
dp[i][j] = len(s1) - i
for i in range(len(s1) - 1, -1, -1):
for j in range(len(s2) - 1, -1, -1):
if s1[i] == s2[j]:
dp[i][j] = dp[i + 1][j + 1]
else:
inse = 1 + dp[i][j + 1]
delet = 1 + dp[i + 1][j]
dp[i][j] = min(inse, delet)
return dp[0][0] | CLASS_DEF 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 FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
r = len(s1)
c = len(s2)
dp = [[(0) for i in range(c + 1)] for j in range(r + 1)]
for i in range(1, r + 1):
for j in range(1, c + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return r + c - dp[-1][-1] - dp[-1][-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
def find(s1, s2, i, j, dp):
if i == len(s1) and j == len(s2):
return 0
if i == len(s1) and j != len(s2):
return len(s2) - j
if i != len(s1) and j == len(s2):
return len(s1) - i
if dp[i][j] != -1:
return dp[i][j]
skip = 9999999
inse = 9999999
delet = 999999
if s1[i] == s2[j]:
skip = find(s1, s2, i + 1, j + 1, dp)
else:
inse = 1 + find(s1, s2, i, j + 1, dp)
delet = 1 + find(s1, s2, i + 1, j, dp)
dp[i][j] = min(skip, inse, delet)
return dp[i][j]
dp = [[(-1) for i in range(len(s2) + 1)] for _ in range(len(s1) + 1)]
return find(s1, s2, 0, 0, dp) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR 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 RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def find_lcs(self, dp, txt1, txt2, m, n):
if m == 0 or n == 0:
return 0
if dp[m][n] != -1:
return dp[m][n]
if txt1[m - 1] == txt2[n - 1]:
dp[m][n] = 1 + self.find_lcs(dp, txt1, txt2, m - 1, n - 1)
return dp[m][n]
else:
dp[m][n] = max(
self.find_lcs(dp, txt1, txt2, m - 1, n),
self.find_lcs(dp, txt1, txt2, m, n - 1),
)
return dp[m][n]
def minOperations(self, s1, s2):
m = len(s1)
n = len(s2)
dp = [[(-1) for i in range(n + 1)] for j in range(m + 1)]
count = self.find_lcs(dp, s1, s2, m, n)
return m + n - 2 * count | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def helper(self, i, j, s1, s2, dp):
if i < 0 or j < 0:
return 0
if dp[i][j] != -1:
return dp[i][j]
if s1[i] == s2[j]:
return 1 + self.helper(i - 1, j - 1, s1, s2, dp)
else:
dp[i][j] = max(
self.helper(i - 1, j, s1, s2, dp), self.helper(i, j - 1, s1, s2, dp)
)
return dp[i][j]
def minOperations(self, s1, s2):
m = len(s1)
n = len(s2)
dp = [([-1] * (n + 1)) for i in range(m + 1)]
lenOfComSubsequence = self.helper(m - 1, n - 1, s1, s2, dp)
return m + n - 2 * lenOfComSubsequence | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | def maxLength(dp):
ans = 0
for i in range(len(dp)):
for j in range(len(dp[0])):
ans = max(ans, dp[i][j])
return ans
def helper(s1, s2, dp, x, y):
if x == 0 or y == 0:
return 0
if dp[x][y] != -1:
return dp[x][y]
if s1[x - 1] == s2[y - 1]:
dp[x][y] = 1 + helper(s1, s2, dp, x - 1, y - 1)
return dp[x][y]
else:
dp[x][y] = max(helper(s1, s2, dp, x - 1, y), helper(s1, s2, dp, x, y - 1))
return dp[x][y]
class Solution:
def minOperations(self, s1, s2):
x, y = len(s1), len(s2)
dp = [[(-1) for _ in range(len(s2) + 1)] for _ in range(len(s1) + 1)]
helper(s1, s2, dp, x, y)
temp = maxLength(dp)
del_cnt = x - temp
ins_cnt = y - temp
return del_cnt + ins_cnt | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR 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 EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
x = len(s1)
y = len(s2)
dp = [[(-1) for i in range(y)] for j in range(x)]
def answer(i, j, s1, s2):
if i == -1 or j == -1:
return 0
if dp[i][j] != -1:
return dp[i][j]
if s1[i] == s2[j]:
dp[i][j] = 1 + answer(i - 1, j - 1, s1, s2)
return dp[i][j]
else:
dp[i][j] = max(answer(i, j - 1, s1, s2), answer(i - 1, j, s1, s2))
return dp[i][j]
return x - 2 * answer(x - 1, y - 1, s1, s2) + y | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def subs(self, s1, s2):
n = len(s1)
m = len(s2)
prev = [0] * (m + 1)
curr = [0] * (m + 1)
for i in range(1, n + 1):
for j in range(1, m + 1):
if s1[i - 1] == s2[j - 1]:
curr[j] = 1 + prev[j - 1]
else:
curr[j] = max(prev[j], curr[j - 1])
prev = curr[:]
return prev[-1]
def minOperations(self, s1, s2):
n = len(s1)
m = len(s2)
sub = self.subs(s1, s2)
dele = n - sub
ins = m - sub
return dele + ins | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
lcs = LCSDP(s1, s2, len(s1), len(s2))
numOfDel = len(s1) - lcs
numOfIns = len(s2) - lcs
return numOfDel + numOfIns
def LCSDP(X, Y, n, m):
dp = [[(-1) for i in range(m + 1)] for j in range(n + 1)]
for i in range(n + 1):
for j in range(m + 1):
if i == 0 or j == 0:
dp[i][j] = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if X[i - 1] == Y[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[n][m] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
st1 = s1
st2 = s2
m = len(st1)
n = len(st2)
dp = [[(0) for i in range(n + 1)] for j in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if st1[i - 1] == st2[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
length = dp[m][n]
deletion = m - length
insertion = n - length
return insertion + deletion | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
dp = [[(-1) for j in range(len(s2) + 1)] for i in range(len(s1) + 1)]
for i in range(len(s1) + 1):
dp[i][0] = 0
for i in range(len(s2) + 1):
dp[0][i] = 0
for ind1 in range(1, len(s1) + 1):
for ind2 in range(1, len(s2) + 1):
match = notmatch = 0
if s1[ind1 - 1] == s2[ind2 - 1]:
match = 1 + dp[ind1 - 1][ind2 - 1]
else:
notmatch = max(dp[ind1 - 1][ind2], dp[ind1][ind2 - 1])
dp[ind1][ind2] = max(match, notmatch)
return len(s1) + len(s2) - 2 * dp[len(s1)][len(s2)] | CLASS_DEF 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 ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
def computeLCS(str1, str2):
m, n = len(str1), len(str2)
dp = [[(-1) for j in range(n + 1)] for i in range(m + 1)]
for j in range(n + 1):
dp[0][j] = 0
for i in range(1, m + 1):
dp[i][0] = 0
for i in range(1, m + 1):
for j in range(1, n + 1):
ch1, ch2 = str1[i - 1], str2[j - 1]
if ch1 == ch2:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j])
return dp[m][n]
l1, l2 = len(s1), len(s2)
lcs = computeLCS(s1, s2)
ans = l1 - lcs + (l2 - lcs)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER 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 BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def func(self, X, Y, m, n, t):
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0:
t[i][j] = 0
for i in range(1, m + 1):
for j in range(1, n + 1):
if X[i - 1] == Y[j - 1]:
t[i][j] = 1 + t[i - 1][j - 1]
else:
t[i][j] = max(t[i - 1][j], t[i][j - 1])
return t[m][n]
def LCS(self, X, Y, m, n):
t = [[(0) for i in range(n + 1)] for i in range(m + 1)]
c = self.func(X, Y, m, n, t)
return c
def minOperations(self, s1, s2):
m = len(s1)
n = len(s2)
i = m - self.LCS(s1, s2, m, n)
d = n - self.LCS(s1, s2, m, n)
return i + d | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
m = len(s1)
n = len(s2)
dp = [([0] * (n + 1)) for _ in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
x = i
y = j
if x == 0 or y == 0:
dp[i][j] = 0
elif s1[x - 1] == s2[y - 1]:
dp[i][j] = 1 + dp[x - 1][y - 1]
else:
dp[i][j] = max(dp[x - 1][y], dp[x][y - 1])
ans = dp[m][n]
de = abs(m - ans)
ins = abs(ans - n)
return de + ins | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def lcs(self, s1: str, s2: str) -> int:
dp = [([0] * (len(s2) + 1)) for j in range(len(s1) + 1)]
for i in range(1, len(s1) + 1):
for j in range(1, len(s2) + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[-1][-1]
def minOperations(self, s1, s2):
return len(s1) - self.lcs(s1, s2) + len(s2) - self.lcs(s1, s2) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
m = len(s1)
n = len(s2)
X = s1
Y = s2
dp = [([-1] * (n + 1)) for i in range(m + 1)]
def lcs(x, y, n, m):
if n == 0 or m == 0:
return 0
if dp[n][m] != -1:
return dp[n][m]
if x[n - 1] == y[m - 1]:
dp[n][m] = 1 + lcs(x, y, n - 1, m - 1)
else:
dp[n][m] = max(lcs(x, y, n - 1, m), lcs(x, y, n, m - 1))
return dp[n][m]
ans = lcs(X, Y, m, n)
return m + n - 2 * ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, string1, string2):
cache = {}
def LCS(indx1, indx2):
if indx1 >= len(string1) or indx2 >= len(string2):
return abs(len(string1) - indx1) + abs(len(string2) - indx2)
if (indx1, indx2) in cache:
return cache[indx1, indx2]
if string1[indx1] == string2[indx2]:
cache[indx1, indx2] = LCS(indx1 + 1, indx2 + 1)
else:
cache[indx1, indx2] = min(
1 + LCS(indx1 + 1, indx2), 1 + LCS(indx1, indx2 + 1)
)
return cache[indx1, indx2]
return LCS(0, 0) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
lcs = tabulation(s1, s2, len(s1), len(s2))
return len(s1) + len(s2) - 2 * lcs
def tabulation(s1, s2, n, m) -> int:
t = [([0] * (m + 1)) for i in range(n + 1)]
ans = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if s1[i - 1] == s2[j - 1]:
t[i][j] = 1 + t[i - 1][j - 1]
else:
t[i][j] = max(t[i - 1][j], t[i][j - 1])
return t[n][m] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def lcs(self, s1, s2, x, y):
t = [[(-1) for _ in range(y + 1)] for _ in range(x + 1)]
for i in range(x + 1):
for j in range(y + 1):
if i == 0 or j == 0:
t[i][j] = 0
for i in range(1, x + 1):
for j in range(1, y + 1):
if s1[i - 1] == s2[j - 1]:
t[i][j] = 1 + t[i - 1][j - 1]
else:
t[i][j] = max(t[i][j - 1], t[i - 1][j])
return t[x][y]
def minOperations(self, s1, s2):
a = len(s1)
b = len(s2)
lcs_ = self.lcs(s1, s2, a, b)
return a + b - 2 * lcs_ | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def f(self, s1, s2, ind1, ind2, dp):
if ind1 < 0 or ind2 < 0:
return 0
if dp[ind1][ind2] != -1:
return dp[ind1][ind2]
if s1[ind1] == s2[ind2]:
dp[ind1][ind2] = 1 + self.f(s1, s2, ind1 - 1, ind2 - 1, dp)
return dp[ind1][ind2]
dp[ind1][ind2] = 0 + max(
self.f(s1, s2, ind1, ind2 - 1, dp), self.f(s1, s2, ind1 - 1, ind2, dp)
)
return dp[ind1][ind2]
def minOperations(self, s1, s2):
dp = [[(-1) for i in range(len(s2))] for j in range(len(s1))]
a = self.f(s1, s2, len(s1) - 1, len(s2) - 1, dp)
deletion = len(s1) - a
insertion = len(s2) - a
return deletion + insertion | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
m = len(s1)
n = len(s2)
prev = [(0) for j in range(n + 1)]
curr = [(0) for j in range(n + 1)]
for index1 in range(1, m + 1):
for index2 in range(1, n + 1):
if s1[index1 - 1] == s2[index2 - 1]:
curr[index2] = prev[index2 - 1] + 1
else:
curr[index2] = max(curr[index2 - 1], prev[index2])
prev = curr[:]
res = curr[n]
s1_remaining = abs(m - res)
s2_remaining = abs(n - res)
return s1_remaining + s2_remaining | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
n = len(s1)
m = len(s2)
c = self.fun(m, n)
return n - c + m - c
def fun(self, n, m):
dp = [[(0) for _ in range(n + 1)] for _ in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0:
dp[i][j] = 0
elif s1[i - 1] == s2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[m][n] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
dp = [[(0) for j in range(len(s2) + 1)] for i in range(len(s1) + 1)]
for i in range(1, len(s1) + 1):
for j in range(1, len(s2) + 1):
if s1[i - 1] == s2[j - 1]:
answer = 1 + dp[i - 1][j - 1]
else:
answer = max(dp[i][j - 1], dp[i - 1][j])
dp[i][j] = answer
modification = len(s1) + len(s2) - 2 * dp[-1][-1]
return modification | CLASS_DEF 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 NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER RETURN VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
delete = 0
insert = 0
n = len(s1)
m = len(s2)
res = self.f(s1, s2, n, m)
delete = n - res
insert = m - res
return delete + insert
def f(self, s1, s2, n, m):
dp = [[(0) for i in range(m + 1)] for j in range(n + 1)]
ans = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[n][m] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
dp = dict()
def go(i, j):
if i < 0 and j < 0:
return 0
if i < 0:
return j + 1
if j < 0:
return i + 1
if (i, j) in dp:
return dp[i, j]
if s1[i] == s2[j]:
ans = go(i - 1, j - 1)
else:
ans = 1 + min(go(i - 1, j), go(i, j - 1))
dp[i, j] = ans
return ans
a = len(s1)
b = len(s2)
return go(a - 1, b - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
dp = []
def gen(self, s1, s2, r1, r2):
if r1 < 0 or r2 < 0:
return abs(r1 - r2)
l = 99999
if self.dp[r1][r2] != -1:
return self.dp[r1][r2]
if s1[r1] == s2[r2]:
l = self.gen(s1, s2, r1 - 1, r2 - 1)
m = self.gen(s1, s2, r1 - 1, r2) + 1
n = self.gen(s1, s2, r1, r2 - 1) + 1
self.dp[r1][r2] = min(l, m, n)
return min(l, m, n)
def minOperations(self, s1, s2):
self.dp = []
for i in range(len(s1) + 1):
b = []
for j in range(len(s2)):
b.append(-1)
self.dp.append(b)
a = self.gen(s1, s2, len(s1) - 1, len(s2) - 1)
return a | CLASS_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
def lcs(x, y, s1, s2):
T = [([0] * (y + 1)) for i in range(x + 1)]
for i in range(1, x + 1):
for j in range(1, y + 1):
if s1[i - 1] == s2[j - 1]:
T[i][j] = 1 + T[i - 1][j - 1]
else:
T[i][j] = max(T[i - 1][j], T[i][j - 1])
return T[x][y]
return len(s1) + len(s2) - 2 * lcs(len(s1), len(s2), s1, s2) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
dp = [([float("inf")] * (len(s2) + 1)) for i in range(len(s1) + 1)]
for i in range(len(s1)):
dp[i][-1] = i + 1
for j in range(len(s2)):
dp[-1][j] = j + 1
dp[-1][-1] = 0
for i in range(len(s1)):
for j in range(len(s2)):
dp[i][j] = (
dp[i - 1][j - 1]
if s1[i] == s2[j]
else min(dp[i - 1][j], dp[i][j - 1]) + 1
)
return dp[len(s1) - 1][len(s2) - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, s1, s2):
dp = [[(-1) for a in range(len(s2))] for b in range(len(s1))]
def solve(i, j):
if i < 0 or j < 0:
return 0
if dp[i][j] != -1:
return dp[i][j]
if s1[i] == s2[j]:
dp[i][j] = 1 + solve(i - 1, j - 1)
else:
dp[i][j] = max(solve(i - 1, j), solve(i, j - 1))
return dp[i][j]
a = solve(len(s1) - 1, len(s2) - 1)
lS1 = len(s1) - a
lS2 = len(s2) - a
return lS1 + lS2 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def minOperations(self, str1, str2):
m, n = len(str1), len(str2)
dp = [[(0) for x in range(n + 1)] for x in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0:
dp[i][j] = j
elif j == 0:
dp[i][j] = i
elif str1[i - 1] == str2[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i][j - 1], dp[i - 1][j])
return dp[m][n] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR |
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
Example 1:
Input: str1 = "heap", str2 = "pea"
Output: 3
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is
inserted at the beginning One thing to
note, though p was required yet it was
removed/deleted first from its position
and then it is inserted to some other
position. Thus, p contributes one to the
deletion_count and one to the
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 β€ |str1|, |str2| β€ 1000
All the characters are lower case English alphabets | class Solution:
def SpaceOptimized(self, ind1, ind2, str1, str2):
prev = [0] * (ind2 + 1)
dp = [0] * (ind2 + 1)
for row in range(1, ind1 + 1):
for col in range(1, ind2 + 1):
if str1[row - 1] == str2[col - 1]:
dp[col] = 1 + prev[col - 1]
else:
val1 = val2 = 0
val1 = prev[col]
val2 = dp[col - 1]
dp[col] = max(val1, val2)
prev = list(dp)
return prev[ind2]
def minOperations(self, s1, s2):
k = self.SpaceOptimized(len(s1), len(s2), s1, s2)
deletions = len(s1) - k
insertions = len(s2) - k
return deletions + insertions | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR |
Akshara is a Maths teacher at Dynamic Public School.One day she decided to take an unusual test of all her students.She took all her students to a fair.There she took them to a candy room.The room had 2 doors and behind each door was unlimited supply of candies.The excitement of the students could not be measured.Each student wanted a specific amount of candies.Now here was the test.
At a time 1 student could enter 1 door.After he exits another will enter.No time is lost when there is change of students.For proccessing 1 candy it took 1 second.The teacher gave exactly X amount of time for all the students
to get their specific amount of candies.So the students ask you for help in determining whether it is possible for all the students to get their candies or not in X amount of time.
Input:
The first line contains T denoting the number of test cases.Each testcase consist of two lines containing n and x denoting the number of students and the time given by the teacher.The next line contains n space separated numbers denoting the amount of candy each student wants.
Output:
For each testcase print YES if it is possible or NO.
Constraints:
1 β€ T β€ 25
1 β€ N β€ 100
1 β€ x β€ 100000
0 β€ A[i] β€ 100
SAMPLE INPUT
2
3 4
2 4 2
3 3
3 3 3
SAMPLE OUTPUT
YES
NO
Explanation
In the first testcase:
At t=0 Student 3 enters door 1 and student 2 enters door 2.
At t=2 Student 1 enters door 1 and student 3 exits.
At t=4 Student 2 and 1 exits. | from sys import stdin
def getInt():
return list(map(int, stdin.readline().split()))
def solve():
n, x = getInt()
A = getInt()
A.sort()
s = sum(A)
W = [False] * (s + 1)
W[0] = True
for e in A:
for w in range(s, e - 1, -1):
W[w] = W[w] or W[w - e]
ret = 100000000
for i, w in enumerate(W):
if w:
ret = min(ret, max(i, s - i))
if ret <= x:
print("YES")
else:
print("NO")
if __name__ == "__main__":
(t,) = getInt()
for _ in range(t):
solve() | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Automatons, or Finite State Machines (FSM), are extremely useful to programmers when it comes to software design. You will be given a simplistic version of an FSM to code for a basic TCP session.
The outcome of this exercise will be to return the correct state of the TCP FSM based on the array of events given.
---------------------------------
The input array of events will consist of one or more of the following strings:
```
APP_PASSIVE_OPEN, APP_ACTIVE_OPEN, APP_SEND, APP_CLOSE, APP_TIMEOUT, RCV_SYN, RCV_ACK, RCV_SYN_ACK, RCV_FIN, RCV_FIN_ACK
```
---------------------------------
The states are as follows and should be returned in all capital letters as shown:
```
CLOSED, LISTEN, SYN_SENT, SYN_RCVD, ESTABLISHED, CLOSE_WAIT, LAST_ACK, FIN_WAIT_1, FIN_WAIT_2, CLOSING, TIME_WAIT
```
---------------------------------
The input will be an array of events. Your job is to traverse the FSM as determined by the events, and return the proper state as a string, all caps, as shown above.
If an event is not applicable to the current state, your code will return `"ERROR"`.
### Action of each event upon each state:
(the format is `INITIAL_STATE: EVENT -> NEW_STATE`)
```
CLOSED: APP_PASSIVE_OPEN -> LISTEN
CLOSED: APP_ACTIVE_OPEN -> SYN_SENT
LISTEN: RCV_SYN -> SYN_RCVD
LISTEN: APP_SEND -> SYN_SENT
LISTEN: APP_CLOSE -> CLOSED
SYN_RCVD: APP_CLOSE -> FIN_WAIT_1
SYN_RCVD: RCV_ACK -> ESTABLISHED
SYN_SENT: RCV_SYN -> SYN_RCVD
SYN_SENT: RCV_SYN_ACK -> ESTABLISHED
SYN_SENT: APP_CLOSE -> CLOSED
ESTABLISHED: APP_CLOSE -> FIN_WAIT_1
ESTABLISHED: RCV_FIN -> CLOSE_WAIT
FIN_WAIT_1: RCV_FIN -> CLOSING
FIN_WAIT_1: RCV_FIN_ACK -> TIME_WAIT
FIN_WAIT_1: RCV_ACK -> FIN_WAIT_2
CLOSING: RCV_ACK -> TIME_WAIT
FIN_WAIT_2: RCV_FIN -> TIME_WAIT
TIME_WAIT: APP_TIMEOUT -> CLOSED
CLOSE_WAIT: APP_CLOSE -> LAST_ACK
LAST_ACK: RCV_ACK -> CLOSED
```

## Examples
```
["APP_PASSIVE_OPEN", "APP_SEND", "RCV_SYN_ACK"] => "ESTABLISHED"
["APP_ACTIVE_OPEN"] => "SYN_SENT"
["APP_ACTIVE_OPEN", "RCV_SYN_ACK", "APP_CLOSE", "RCV_FIN_ACK", "RCV_ACK"] => "ERROR"
```
This kata is similar to [Design a Simple Automaton (Finite State Machine)](https://www.codewars.com/kata/design-a-simple-automaton-finite-state-machine), and you may wish to try that kata before tackling this one.
See wikipedia page [Transmission Control Protocol]( http://en.wikipedia.org/wiki/Transmission_Control_Protocol)
for further details.
See http://www.medianet.kent.edu/techreports/TR2005-07-22-tcp-EFSM.pdf page 4, for the FSM diagram used for this kata. | STATES = {
"CLOSED": {"APP_PASSIVE_OPEN": "LISTEN", "APP_ACTIVE_OPEN": "SYN_SENT"},
"LISTEN": {"RCV_SYN": "SYN_RCVD", "APP_SEND": "SYN_SENT", "APP_CLOSE": "CLOSED"},
"SYN_RCVD": {"APP_CLOSE": "FIN_WAIT_1", "RCV_ACK": "ESTABLISHED"},
"SYN_SENT": {
"RCV_SYN": "SYN_RCVD",
"RCV_SYN_ACK": "ESTABLISHED",
"APP_CLOSE": "CLOSED",
},
"ESTABLISHED": {"APP_CLOSE": "FIN_WAIT_1", "RCV_FIN": "CLOSE_WAIT"},
"FIN_WAIT_1": {
"RCV_FIN": "CLOSING",
"RCV_FIN_ACK": "TIME_WAIT",
"RCV_ACK": "FIN_WAIT_2",
},
"CLOSING": {"RCV_ACK": "TIME_WAIT"},
"FIN_WAIT_2": {"RCV_FIN": "TIME_WAIT"},
"TIME_WAIT": {"APP_TIMEOUT": "CLOSED"},
"CLOSE_WAIT": {"APP_CLOSE": "LAST_ACK"},
"LAST_ACK": {"RCV_ACK": "CLOSED"},
}
def traverse_TCP_states(events):
state = "CLOSED"
try:
for e in events:
state = STATES[state][e]
return state
except KeyError:
return "ERROR" | ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING DICT STRING STRING STRING STRING DICT STRING STRING STRING STRING STRING STRING DICT STRING STRING STRING STRING DICT STRING STRING STRING STRING STRING STRING DICT STRING STRING STRING STRING DICT STRING STRING STRING STRING STRING STRING DICT STRING STRING DICT STRING STRING DICT STRING STRING DICT STRING STRING DICT STRING STRING FUNC_DEF ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR RETURN STRING |
Automatons, or Finite State Machines (FSM), are extremely useful to programmers when it comes to software design. You will be given a simplistic version of an FSM to code for a basic TCP session.
The outcome of this exercise will be to return the correct state of the TCP FSM based on the array of events given.
---------------------------------
The input array of events will consist of one or more of the following strings:
```
APP_PASSIVE_OPEN, APP_ACTIVE_OPEN, APP_SEND, APP_CLOSE, APP_TIMEOUT, RCV_SYN, RCV_ACK, RCV_SYN_ACK, RCV_FIN, RCV_FIN_ACK
```
---------------------------------
The states are as follows and should be returned in all capital letters as shown:
```
CLOSED, LISTEN, SYN_SENT, SYN_RCVD, ESTABLISHED, CLOSE_WAIT, LAST_ACK, FIN_WAIT_1, FIN_WAIT_2, CLOSING, TIME_WAIT
```
---------------------------------
The input will be an array of events. Your job is to traverse the FSM as determined by the events, and return the proper state as a string, all caps, as shown above.
If an event is not applicable to the current state, your code will return `"ERROR"`.
### Action of each event upon each state:
(the format is `INITIAL_STATE: EVENT -> NEW_STATE`)
```
CLOSED: APP_PASSIVE_OPEN -> LISTEN
CLOSED: APP_ACTIVE_OPEN -> SYN_SENT
LISTEN: RCV_SYN -> SYN_RCVD
LISTEN: APP_SEND -> SYN_SENT
LISTEN: APP_CLOSE -> CLOSED
SYN_RCVD: APP_CLOSE -> FIN_WAIT_1
SYN_RCVD: RCV_ACK -> ESTABLISHED
SYN_SENT: RCV_SYN -> SYN_RCVD
SYN_SENT: RCV_SYN_ACK -> ESTABLISHED
SYN_SENT: APP_CLOSE -> CLOSED
ESTABLISHED: APP_CLOSE -> FIN_WAIT_1
ESTABLISHED: RCV_FIN -> CLOSE_WAIT
FIN_WAIT_1: RCV_FIN -> CLOSING
FIN_WAIT_1: RCV_FIN_ACK -> TIME_WAIT
FIN_WAIT_1: RCV_ACK -> FIN_WAIT_2
CLOSING: RCV_ACK -> TIME_WAIT
FIN_WAIT_2: RCV_FIN -> TIME_WAIT
TIME_WAIT: APP_TIMEOUT -> CLOSED
CLOSE_WAIT: APP_CLOSE -> LAST_ACK
LAST_ACK: RCV_ACK -> CLOSED
```

## Examples
```
["APP_PASSIVE_OPEN", "APP_SEND", "RCV_SYN_ACK"] => "ESTABLISHED"
["APP_ACTIVE_OPEN"] => "SYN_SENT"
["APP_ACTIVE_OPEN", "RCV_SYN_ACK", "APP_CLOSE", "RCV_FIN_ACK", "RCV_ACK"] => "ERROR"
```
This kata is similar to [Design a Simple Automaton (Finite State Machine)](https://www.codewars.com/kata/design-a-simple-automaton-finite-state-machine), and you may wish to try that kata before tackling this one.
See wikipedia page [Transmission Control Protocol]( http://en.wikipedia.org/wiki/Transmission_Control_Protocol)
for further details.
See http://www.medianet.kent.edu/techreports/TR2005-07-22-tcp-EFSM.pdf page 4, for the FSM diagram used for this kata. | NEW_STATE = {
"CLOSED__APP_PASSIVE_OPEN": "LISTEN",
"CLOSED__APP_ACTIVE_OPEN": "SYN_SENT",
"LISTEN__RCV_SYN": "SYN_RCVD",
"LISTEN__APP_SEND": "SYN_SENT",
"LISTEN__APP_CLOSE": "CLOSED",
"SYN_RCVD__APP_CLOSE": "FIN_WAIT_1",
"SYN_RCVD__RCV_ACK": "ESTABLISHED",
"SYN_SENT__RCV_SYN": "SYN_RCVD",
"SYN_SENT__RCV_SYN_ACK": "ESTABLISHED",
"SYN_SENT__APP_CLOSE": "CLOSED",
"ESTABLISHED__APP_CLOSE": "FIN_WAIT_1",
"ESTABLISHED__RCV_FIN": "CLOSE_WAIT",
"FIN_WAIT_1__RCV_FIN": "CLOSING",
"FIN_WAIT_1__RCV_FIN_ACK": "TIME_WAIT",
"FIN_WAIT_1__RCV_ACK": "FIN_WAIT_2",
"CLOSING__RCV_ACK": "TIME_WAIT",
"FIN_WAIT_2__RCV_FIN": "TIME_WAIT",
"TIME_WAIT__APP_TIMEOUT": "CLOSED",
"CLOSE_WAIT__APP_CLOSE": "LAST_ACK",
"LAST_ACK__RCV_ACK": "CLOSED",
}
def traverse_TCP_states(events, state="CLOSED"):
for event in events:
QUERY = "%s__%s" % (state, event)
if QUERY in NEW_STATE:
state = NEW_STATE[QUERY]
else:
return "ERROR"
return state | ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF STRING FOR VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR IF VAR VAR ASSIGN VAR VAR VAR RETURN STRING RETURN VAR |
Automatons, or Finite State Machines (FSM), are extremely useful to programmers when it comes to software design. You will be given a simplistic version of an FSM to code for a basic TCP session.
The outcome of this exercise will be to return the correct state of the TCP FSM based on the array of events given.
---------------------------------
The input array of events will consist of one or more of the following strings:
```
APP_PASSIVE_OPEN, APP_ACTIVE_OPEN, APP_SEND, APP_CLOSE, APP_TIMEOUT, RCV_SYN, RCV_ACK, RCV_SYN_ACK, RCV_FIN, RCV_FIN_ACK
```
---------------------------------
The states are as follows and should be returned in all capital letters as shown:
```
CLOSED, LISTEN, SYN_SENT, SYN_RCVD, ESTABLISHED, CLOSE_WAIT, LAST_ACK, FIN_WAIT_1, FIN_WAIT_2, CLOSING, TIME_WAIT
```
---------------------------------
The input will be an array of events. Your job is to traverse the FSM as determined by the events, and return the proper state as a string, all caps, as shown above.
If an event is not applicable to the current state, your code will return `"ERROR"`.
### Action of each event upon each state:
(the format is `INITIAL_STATE: EVENT -> NEW_STATE`)
```
CLOSED: APP_PASSIVE_OPEN -> LISTEN
CLOSED: APP_ACTIVE_OPEN -> SYN_SENT
LISTEN: RCV_SYN -> SYN_RCVD
LISTEN: APP_SEND -> SYN_SENT
LISTEN: APP_CLOSE -> CLOSED
SYN_RCVD: APP_CLOSE -> FIN_WAIT_1
SYN_RCVD: RCV_ACK -> ESTABLISHED
SYN_SENT: RCV_SYN -> SYN_RCVD
SYN_SENT: RCV_SYN_ACK -> ESTABLISHED
SYN_SENT: APP_CLOSE -> CLOSED
ESTABLISHED: APP_CLOSE -> FIN_WAIT_1
ESTABLISHED: RCV_FIN -> CLOSE_WAIT
FIN_WAIT_1: RCV_FIN -> CLOSING
FIN_WAIT_1: RCV_FIN_ACK -> TIME_WAIT
FIN_WAIT_1: RCV_ACK -> FIN_WAIT_2
CLOSING: RCV_ACK -> TIME_WAIT
FIN_WAIT_2: RCV_FIN -> TIME_WAIT
TIME_WAIT: APP_TIMEOUT -> CLOSED
CLOSE_WAIT: APP_CLOSE -> LAST_ACK
LAST_ACK: RCV_ACK -> CLOSED
```

## Examples
```
["APP_PASSIVE_OPEN", "APP_SEND", "RCV_SYN_ACK"] => "ESTABLISHED"
["APP_ACTIVE_OPEN"] => "SYN_SENT"
["APP_ACTIVE_OPEN", "RCV_SYN_ACK", "APP_CLOSE", "RCV_FIN_ACK", "RCV_ACK"] => "ERROR"
```
This kata is similar to [Design a Simple Automaton (Finite State Machine)](https://www.codewars.com/kata/design-a-simple-automaton-finite-state-machine), and you may wish to try that kata before tackling this one.
See wikipedia page [Transmission Control Protocol]( http://en.wikipedia.org/wiki/Transmission_Control_Protocol)
for further details.
See http://www.medianet.kent.edu/techreports/TR2005-07-22-tcp-EFSM.pdf page 4, for the FSM diagram used for this kata. | def traverse_TCP_states(events):
lib = {
("CLOSED", "APP_PASSIVE_OPEN"): "LISTEN",
("CLOSED", "APP_ACTIVE_OPEN"): "SYN_SENT",
("LISTEN", "RCV_SYN"): "SYN_RCVD",
("LISTEN", "APP_SEND"): "SYN_SENT",
("LISTEN", "APP_CLOSE"): "CLOSED",
("SYN_RCVD", "APP_CLOSE"): "FIN_WAIT_1",
("SYN_RCVD", "RCV_ACK"): "ESTABLISHED",
("SYN_SENT", "RCV_SYN"): "SYN_RCVD",
("SYN_SENT", "RCV_SYN_ACK"): "ESTABLISHED",
("SYN_SENT", "APP_CLOSE"): "CLOSED",
("ESTABLISHED", "APP_CLOSE"): "FIN_WAIT_1",
("ESTABLISHED", "RCV_FIN"): "CLOSE_WAIT",
("FIN_WAIT_1", "RCV_FIN"): "CLOSING",
("FIN_WAIT_1", "RCV_FIN_ACK"): "TIME_WAIT",
("FIN_WAIT_1", "RCV_ACK"): "FIN_WAIT_2",
("CLOSING", "RCV_ACK"): "TIME_WAIT",
("FIN_WAIT_2", "RCV_FIN"): "TIME_WAIT",
("TIME_WAIT", "APP_TIMEOUT"): "CLOSED",
("CLOSE_WAIT", "APP_CLOSE"): "LAST_ACK",
("LAST_ACK", "RCV_ACK"): "CLOSED",
}
state = "CLOSED"
for command in events:
state = lib.get((state, command), "ERROR")
return state | FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING RETURN VAR |
You are given a text consisting of $n$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $w_i$ is the $i$-th word of text. All words consist only of lowercase Latin letters.
Let's denote a segment of words $w[i..j]$ as a sequence of words $w_i, w_{i + 1}, \dots, w_j$. Two segments of words $w[i_1 .. j_1]$ and $w[i_2 .. j_2]$ are considered equal if $j_1 - i_1 = j_2 - i_2$, $j_1 \ge i_1$, $j_2 \ge i_2$, and for every $t \in [0, j_1 - i_1]$ $w_{i_1 + t} = w_{i_2 + t}$. For example, for the text "to be or not to be" the segments $w[1..2]$ and $w[5..6]$ are equal, they correspond to the words "to be".
An abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text "a ab a a b ab a a b c" you can replace segments of words $w[2..4]$ and $w[6..8]$ with an abbreviation "AAA" and obtain the text "a AAA b AAA b c", or you can replace segments of words $w[2..5]$ and $w[6..9]$ with an abbreviation "AAAB" and obtain the text "a AAAB AAAB c".
What is the minimum length of the text after at most one abbreviation?
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 300$) β the number of words in the text.
The next line contains $n$ space-separated words of the text $w_1, w_2, \dots, w_n$. Each word consists only of lowercase Latin letters.
It is guaranteed that the length of text does not exceed $10^5$.
-----Output-----
Print one integer β the minimum length of the text after at most one abbreviation.
-----Examples-----
Input
6
to be or not to be
Output
12
Input
10
a ab a a b ab a a b c
Output
13
Input
6
aa bb aa aa bb bb
Output
11
-----Note-----
In the first example you can obtain the text "TB or not TB".
In the second example you can obtain the text "a AAAB AAAB c".
In the third example you can obtain the text "AB aa AB bb". | from sys import stdin
def kmp(pat, txt):
leng = 0
i = 1
ans = 0
M = len(pat)
N = len(txt)
lps = [0] * M
j = 0
while i < M:
if pat[i] == pat[leng]:
leng += 1
lps[i] = leng
i += 1
elif leng != 0:
leng = lps[leng - 1]
else:
lps[i] = 0
i += 1
i = 0
while i < N:
if pat[j] == txt[i]:
i += 1
j += 1
if j == M:
if (i - j == 0 or txt[i - j - 1] == " ") and (
i - j + len(pat) == len(txt) or txt[i - j + len(pat)] == " "
):
ans += 1
i = i - j + len(pat)
j = 0
else:
j = lps[j - 1]
elif i < N and pat[j] != txt[i]:
if j != 0:
j = lps[j - 1]
else:
i += 1
return ans
n = int(stdin.readline().strip())
s1 = stdin.readline().strip().split()
s = []
x = 1
d = dict()
d1 = dict()
st = set()
ans = n - 1
s2 = ""
for i in s1:
ans += len(i)
if i not in st:
d.update({i: x})
d1.update({x: len(i)})
x += 1
st.add(i)
s.append(d[i])
s2 += str(d[i])
if len(s) < n:
s2 += " "
acum = 0
s3 = s2.split()
tot = ans
for i in range(n):
x = 0
y = 0
for j in range(i, n):
x += d1[s[j]] - 1
y += len(s3[j])
z = kmp(s2[acum : acum + y + j - i], s2)
if z > 1:
aux = tot - z * (x + j - i)
if aux < ans:
ans = aux
acum += len(s3[i]) + 1
print(ans) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER STRING BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR DICT VAR VAR EXPR FUNC_CALL VAR DICT VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a text consisting of $n$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $w_i$ is the $i$-th word of text. All words consist only of lowercase Latin letters.
Let's denote a segment of words $w[i..j]$ as a sequence of words $w_i, w_{i + 1}, \dots, w_j$. Two segments of words $w[i_1 .. j_1]$ and $w[i_2 .. j_2]$ are considered equal if $j_1 - i_1 = j_2 - i_2$, $j_1 \ge i_1$, $j_2 \ge i_2$, and for every $t \in [0, j_1 - i_1]$ $w_{i_1 + t} = w_{i_2 + t}$. For example, for the text "to be or not to be" the segments $w[1..2]$ and $w[5..6]$ are equal, they correspond to the words "to be".
An abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text "a ab a a b ab a a b c" you can replace segments of words $w[2..4]$ and $w[6..8]$ with an abbreviation "AAA" and obtain the text "a AAA b AAA b c", or you can replace segments of words $w[2..5]$ and $w[6..9]$ with an abbreviation "AAAB" and obtain the text "a AAAB AAAB c".
What is the minimum length of the text after at most one abbreviation?
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 300$) β the number of words in the text.
The next line contains $n$ space-separated words of the text $w_1, w_2, \dots, w_n$. Each word consists only of lowercase Latin letters.
It is guaranteed that the length of text does not exceed $10^5$.
-----Output-----
Print one integer β the minimum length of the text after at most one abbreviation.
-----Examples-----
Input
6
to be or not to be
Output
12
Input
10
a ab a a b ab a a b c
Output
13
Input
6
aa bb aa aa bb bb
Output
11
-----Note-----
In the first example you can obtain the text "TB or not TB".
In the second example you can obtain the text "a AAAB AAAB c".
In the third example you can obtain the text "AB aa AB bb". | N = 303
eq = []
dp = []
for i in range(N):
eq.append([False] * N)
for i in range(N):
dp.append([0] * N)
n = int(input())
s = input()
allsum = len(s)
s = s.split()
for i in range(n):
eq[i][i] = True
for j in range(i):
eq[i][j] = eq[j][i] = s[i] == s[j]
for i in range(n - 1, -1, -1):
for j in range(n - 1, -1, -1):
if eq[i][j]:
if i < n - 1 and j < n - 1:
dp[i][j] = dp[i + 1][j + 1] + 1
else:
dp[i][j] = 1
ans = allsum
for i in range(n):
su = 0
for j in range(n - i):
su += len(s[i + j])
cnt = 1
pos = i + j + 1
while pos < n:
if dp[i][pos] > j:
cnt += 1
pos += j
pos += 1
cur = allsum - su * cnt + (j + 1) * cnt - j * cnt
if cnt > 1 and ans > cur:
ans = cur
print(ans) | ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR 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 NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a text consisting of $n$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $w_i$ is the $i$-th word of text. All words consist only of lowercase Latin letters.
Let's denote a segment of words $w[i..j]$ as a sequence of words $w_i, w_{i + 1}, \dots, w_j$. Two segments of words $w[i_1 .. j_1]$ and $w[i_2 .. j_2]$ are considered equal if $j_1 - i_1 = j_2 - i_2$, $j_1 \ge i_1$, $j_2 \ge i_2$, and for every $t \in [0, j_1 - i_1]$ $w_{i_1 + t} = w_{i_2 + t}$. For example, for the text "to be or not to be" the segments $w[1..2]$ and $w[5..6]$ are equal, they correspond to the words "to be".
An abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text "a ab a a b ab a a b c" you can replace segments of words $w[2..4]$ and $w[6..8]$ with an abbreviation "AAA" and obtain the text "a AAA b AAA b c", or you can replace segments of words $w[2..5]$ and $w[6..9]$ with an abbreviation "AAAB" and obtain the text "a AAAB AAAB c".
What is the minimum length of the text after at most one abbreviation?
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 300$) β the number of words in the text.
The next line contains $n$ space-separated words of the text $w_1, w_2, \dots, w_n$. Each word consists only of lowercase Latin letters.
It is guaranteed that the length of text does not exceed $10^5$.
-----Output-----
Print one integer β the minimum length of the text after at most one abbreviation.
-----Examples-----
Input
6
to be or not to be
Output
12
Input
10
a ab a a b ab a a b c
Output
13
Input
6
aa bb aa aa bb bb
Output
11
-----Note-----
In the first example you can obtain the text "TB or not TB".
In the second example you can obtain the text "a AAAB AAAB c".
In the third example you can obtain the text "AB aa AB bb". | import sys
input = sys.stdin.readline
n = int(input())
s = input()
a = list(s.split())
eq = [[(0) for i in range(n)] for j in range(n)]
dp = [[(0) for i in range(n)] for j in range(n)]
for i in range(n):
eq[i][i] = 1
for j in range(0, i):
if a[i] == a[j]:
eq[i][j] += 1
eq[j][i] += 1
for i in range(n - 1, -1, -1):
for j in range(n - 1, -1, -1):
if eq[i][j] == 1:
if i < n - 1 and j < n - 1:
dp[i][j] = dp[i + 1][j + 1] + 1
else:
dp[i][j] = 1
allsum = n - 1
for k in a:
allsum += len(k)
ans = allsum
for i in range(n):
sx = 0
j = 0
while i + j < n:
sx += len(a[i + j])
cnt = 1
pos = i + j + 1
while pos < n:
if dp[i][pos] > j:
cnt += 1
pos += j
pos += 1
cur = allsum - sx * cnt + (j + 1) * cnt - j * cnt
if cnt > 1 and ans > cur:
ans = cur
j += 1
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a text consisting of $n$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $w_i$ is the $i$-th word of text. All words consist only of lowercase Latin letters.
Let's denote a segment of words $w[i..j]$ as a sequence of words $w_i, w_{i + 1}, \dots, w_j$. Two segments of words $w[i_1 .. j_1]$ and $w[i_2 .. j_2]$ are considered equal if $j_1 - i_1 = j_2 - i_2$, $j_1 \ge i_1$, $j_2 \ge i_2$, and for every $t \in [0, j_1 - i_1]$ $w_{i_1 + t} = w_{i_2 + t}$. For example, for the text "to be or not to be" the segments $w[1..2]$ and $w[5..6]$ are equal, they correspond to the words "to be".
An abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text "a ab a a b ab a a b c" you can replace segments of words $w[2..4]$ and $w[6..8]$ with an abbreviation "AAA" and obtain the text "a AAA b AAA b c", or you can replace segments of words $w[2..5]$ and $w[6..9]$ with an abbreviation "AAAB" and obtain the text "a AAAB AAAB c".
What is the minimum length of the text after at most one abbreviation?
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 300$) β the number of words in the text.
The next line contains $n$ space-separated words of the text $w_1, w_2, \dots, w_n$. Each word consists only of lowercase Latin letters.
It is guaranteed that the length of text does not exceed $10^5$.
-----Output-----
Print one integer β the minimum length of the text after at most one abbreviation.
-----Examples-----
Input
6
to be or not to be
Output
12
Input
10
a ab a a b ab a a b c
Output
13
Input
6
aa bb aa aa bb bb
Output
11
-----Note-----
In the first example you can obtain the text "TB or not TB".
In the second example you can obtain the text "a AAAB AAAB c".
In the third example you can obtain the text "AB aa AB bb". | n = int(input())
arr = input()
final = len(arr)
arr = arr.split()
lens = [(0) for x in range(n)]
visit = [(0) for x in range(n)]
cnt = 0
ans = 0
for i in range(n):
if visit[i]:
continue
lens[cnt] = len(arr[i])
for j in range(i + 1, n):
if arr[j] == arr[i]:
arr[j] = cnt
visit[j] = 1
arr[i] = cnt
cnt += 1
for i in range(n):
for j in range(i, n):
temp = arr[i : j + 1]
ind = 1
found = 0
len2 = j - i + 1
cur = 0
kmp = [(0) for x in range(len2)]
while ind < len2:
if temp[ind] == temp[cur]:
cur += 1
kmp[ind] = cur
ind += 1
elif cur != 0:
cur -= 1
else:
kmp[ind] = 0
ind += 1
ind = 0
cur = 0
while ind < n:
if arr[ind] == temp[cur]:
ind += 1
cur += 1
if cur == len2:
found += 1
cur = 0
elif ind < n and temp[cur] != arr[ind]:
if cur != 0:
cur = kmp[cur - 1]
else:
ind += 1
if found > 1:
res = 0
for k in temp:
res += (lens[k] - 1) * found
res += (len(temp) - 1) * found
ans = max(ans, res)
print(final - ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
You are given a function f which is defined as :
Your task is to find the value of
where M is given in input.
------ Input Format ------
First line contains T, the number of test cases.
First line of each test case contain 3 space separated integers N, M and Q.
Next Q line follows, each line contain r.
------ Output Format ------
For each test case, output Q lines, each line containing the required answer.
------ Constraints ------
2 β€ N β€ 10^{6}
1 β€ M β€ 10^{9}
2 β€ Sum of N over all test cases β€ 10^{6}
1 β€ Sum of Q over all test cases β€ 2*10^{5}
1 β€ T β€ 10^{5}
1 < r < N
Sample Input
2
5 114 1
2
50 3874 3
31
17
21
Sample Output
72
3718
624
1144
Explanation for first test case
f[1] = 1
f[2] = 2
f[3] = 1*2^{2} * 3 = 12
f[4] =1*2^{3}*3^{2}*4 = 8*9*4 = 288
f[5] = 1*2^{4}*3^{3}*4^{2}*5 =34560
value of f[5] / (f[2]*f[3]) = 1440 and 1440 %114 is 72 | t = int(input())
for each_t in range(t):
n, m, q = input().split()
n, m, q = int(n), int(m), int(q)
a = [0] * (n + 1)
if n % 2 == 0:
mid = n // 2
a[mid] = mid + 1
b = mid
c = mid + 2
else:
mid = n // 2 + 1
a[mid] = 1
b = mid
c = mid + 1
x = mid - 1
while x >= 1:
a[x] = a[x + 1] % m * b % m * c % m % m
b -= 1
c += 1
x -= 1
a[0] = 1
for i in range(1, mid + 1):
a[i] = a[i - 1] * a[i] % m
for each_q in range(q):
r = int(input())
r = min(r, n - r)
print(a[r] % m) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
You are given a function f which is defined as :
Your task is to find the value of
where M is given in input.
------ Input Format ------
First line contains T, the number of test cases.
First line of each test case contain 3 space separated integers N, M and Q.
Next Q line follows, each line contain r.
------ Output Format ------
For each test case, output Q lines, each line containing the required answer.
------ Constraints ------
2 β€ N β€ 10^{6}
1 β€ M β€ 10^{9}
2 β€ Sum of N over all test cases β€ 10^{6}
1 β€ Sum of Q over all test cases β€ 2*10^{5}
1 β€ T β€ 10^{5}
1 < r < N
Sample Input
2
5 114 1
2
50 3874 3
31
17
21
Sample Output
72
3718
624
1144
Explanation for first test case
f[1] = 1
f[2] = 2
f[3] = 1*2^{2} * 3 = 12
f[4] =1*2^{3}*3^{2}*4 = 8*9*4 = 288
f[5] = 1*2^{4}*3^{3}*4^{2}*5 =34560
value of f[5] / (f[2]*f[3]) = 1440 and 1440 %114 is 72 | for _ in range(int(input())):
n, mod, q = map(int, input().split())
ls = [0] * n
ls[1] = n
for i in range(2, n):
ls[1] = ls[1] * i % mod
n += 1
if n % 2:
num, den, cur = n // 2 + 1, n // 2, n // 2 + 1
else:
num, den, cur = n // 2, n // 2, 1
n -= 1
ls[den] = cur
num += 1
den -= 1
while den > 1:
cur = cur * (den + 1) % mod
cur = cur * num % mod
ls[den] = cur
num += 1
den -= 1
for i in range(2, n // 2 + 1):
ls[i] = ls[i] * ls[i - 1] % mod
for __ in range(q):
x = int(input())
if x > n / 2:
x = n - x
print(ls[x]) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
b = {(0, 0): 1}
ans, i, x = 0, 0, 0
for a in map(int, input().split()):
x ^= a
i += 1
t = b.get((x, i % 2), 0)
ans += t
b[x, i % 2] = t + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
a = [int(x) for x in input().split()]
d0 = {}
d1 = {}
c = 0
count = 0
for i in range(n):
c = c ^ a[i]
if c == 0 and i % 2 == 1:
count = count + 1
if i % 2 == 0:
l = d0.get(c, 0)
count = count + l
if l == 0:
d0[c] = 0
d0[c] = d0[c] + 1
else:
l = d1.get(c, 0)
count = count + l
if l == 0:
d1[c] = 0
d1[c] = d1[c] + 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
s = list(map(int, input().rstrip().split()))
resp = [s[0]]
resd = [0, s[0] ^ s[1]]
for i in range(2, n):
if i % 2 == 0:
resp += [resd[-1] ^ s[i]]
else:
resd += [resp[-1] ^ s[i]]
def f(lis):
dic = {}
s = 0
for el in lis:
try:
dic[el] += 1
except:
dic[el] = 1
for d in dic:
s += dic[d] * (dic[d] - 1) / 2
return int(s)
print(f(resp) + f(resd)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR LIST BIN_OP VAR NUMBER VAR VAR VAR LIST BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | def main():
n = int(input())
a = list(map(int, input().split()))
cnt = [([0] * (1 << 21)) for _ in range(2)]
cnt[1][0] = 1
curr, ret = 0, 0
for i, x in enumerate(a):
curr ^= x
ret += cnt[i % 2][curr]
cnt[i % 2][curr] += 1
print(ret)
main() | FUNC_DEF 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 BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
a = [int(x) for x in input().split()]
pref = [0]
count = [[0, 0] for x in range(2**20 + 10)]
for i in range(0, n):
pref.append(pref[i] ^ a[i])
index = 0
ans = 0
for p in pref:
ans += count[p][index % 2]
count[p][index % 2] += 1
index += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
A = list(map(int, input().split()))
even = {}
odd = {}
cnt = 0
B = [A[0]]
for i in range(1, n):
B.append(B[-1] ^ A[i])
for u in B:
even[u] = 0
odd[u] = 0
for x in range(n):
if x % 2 == 0:
even[B[x]] += 1
else:
odd[B[x]] += 1
for t in even:
cnt += even[t] * (even[t] - 1) // 2
cnt += odd[t] * (odd[t] - 1) // 2
if 0 in odd:
print(cnt + odd[0])
else:
print(cnt) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | def get():
return list(map(int, input().split()))
n = int(input())
a = get()
xor = (n + 1) * [0]
for i in range(1, n + 1):
xor[i] = xor[i - 1] ^ a[i - 1]
ans = 0
b = [{}, {}]
for i in range(2):
for j in range(i, n + 1, 2):
if not xor[j] in b[i]:
b[i][xor[j]] = 0
ans += b[i][xor[j]]
b[i][xor[j]] += 1
print(ans) | 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 BIN_OP BIN_OP VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST DICT DICT FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
a = list(map(int, input().split()))
x, cnt = 0, 0
s = [[0] * 2**20 + 3 * [0], [0] * 2**20 + 3 * [0]]
s[1][0] = 1
for i in range(n):
x ^= a[i]
cnt += s[i % 2][x]
s[i % 2][x] += 1
print(cnt) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER BIN_OP NUMBER LIST NUMBER BIN_OP BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER BIN_OP NUMBER LIST NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
a = list(map(int, input().split()))
cnt = [[(0) for i in range(int(2 << 20) + 10)], [(0) for i in range(int(2 << 20) + 10)]]
cnt[1][0] = 1
x = 0
ans = 0
for i in range(n):
x ^= a[i]
ans += cnt[i % 2][x]
cnt[i % 2][x] += 1
print(ans) | 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 NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
AX = [0, A[0]]
for i in range(1, n):
AX.append(AX[i] ^ A[i])
LIST = sorted(set(AX))
DICT = dict()
for i in range(len(LIST)):
DICT[LIST[i]] = i
NUMLIST = [[] for i in range(len(LIST))]
for i in range(n + 1):
NUMLIST[DICT[AX[i]]].append(i)
ANS = 0
for lis in NUMLIST:
l1 = [l for l in lis if l % 2 == 1]
l2 = [l for l in lis if l % 2 == 0]
ANS += len(l1) * (len(l1) - 1) // 2 + len(l2) * (len(l2) - 1) // 2
print(ANS) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | list_int_input = lambda inp: list(map(int, inp.split()))
int_input = lambda inp: int(inp)
string_to_list_input = lambda inp: list(inp)
n = int(input())
a = list_int_input(input())
a = [0] + a
xl = []
for ind, val in enumerate(a):
if ind > 0:
val = xl[-1] ^ val
xl.append(val)
el = xl[::2]
ol = xl[1::2]
de, do = {}, {}
for i in el:
de.setdefault(i, 0)
de[i] += 1
for i in ol:
do.setdefault(i, 0)
do[i] += 1
summ = 0
for k, v in de.items():
summ += int(v * (v - 1) / 2)
for k, v in do.items():
summ += int(v * (v - 1) / 2)
print(summ) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR DICT DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
lst = list(map(int, input().split()))
total = 0
for i in range(1, n):
lst[i] = lst[i] ^ lst[i - 1]
def ikili(n):
return n * (n - 1) / 2
dct = {(0): [0, 0]}
for i in lst:
dct[i] = [0, 0]
for i in range(n):
if i % 2 == 0:
dct[lst[i]][0] += 1
else:
dct[lst[i]][1] += 1
total += dct[0][1]
for i in dct:
total += ikili(dct[i][0]) + ikili(dct[i][1])
print(int(total)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR DICT NUMBER LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | def main():
n = int(input())
a = map(int, input().split())
cnt = [{} for _ in range(2)]
xor = 0
result = 0
cnt[1][0] = 1
for i, x in enumerate(a):
xor ^= x
index = i & 1
result += int(cnt[index].get(xor) or 0)
cnt[index][xor] = int(cnt[index].get(xor) or 0) + 1
print(result)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
a = list(map(int, input().split()))
xor = [a[0]]
for i in range(1, n):
xor.append(xor[-1] ^ a[i])
d = {(0): [1, 0]}
for i in range(n):
if xor[i] not in d:
d[xor[i]] = [0, 0]
d[xor[i]][(i + 1) % 2] += 1
funny = 0
for k in d:
odd = d[k][1]
even = d[k][0]
funny += odd * (odd - 1) // 2
funny += even * (even - 1) // 2
print(funny) | 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 EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
arr = list(map(int, input().split()))
pre = [0] * (n + 1)
for i in range(n):
pre[i + 1] = pre[i] ^ arr[i]
d = {}
d1 = {}
for i in range(n + 1):
if i % 2 == 0:
if pre[i] in d:
d[pre[i]] += 1
else:
d[pre[i]] = 1
elif pre[i] in d1:
d1[pre[i]] += 1
else:
d1[pre[i]] = 1
ans = 0
for key, value in d.items():
ans += value * (value - 1) // 2
for key, value in d1.items():
ans += value * (value - 1) // 2
print(ans) | 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 BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
a = list(map(int, input().split()))
d0 = {}
d1 = {}
cx = 0
ans = 0
for i in range(n):
cx = cx ^ a[i]
if cx == 0 and i % 2 == 1:
ans += 1
if i % 2 == 1:
ans += d1.get(cx, 0)
if cx not in d1:
d1[cx] = 0
d1[cx] += 1
else:
ans += d0.get(cx, 0)
if cx not in d0:
d0[cx] = 0
d0[cx] += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | input()
N = 1 << 20
d = [1] + [0] * N, [0] * N
r = s = i = 0
for x in map(int, input().split()):
s ^= x
i ^= 1
r += d[i][s]
d[i][s] += 1
print(r) | EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
a = [int(x) for x in input().split()]
pref = [0]
for i in range(n):
pref.append(pref[-1] ^ a[i])
dp = [[(0) for i in range(2**20 + 5)] for j in range(2)]
ans = 0
for i in range(len(pref)):
ans += dp[i % 2][pref[i]]
dp[i % 2][pref[i]] += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
arr_str = input().split()
arr = [int(x) for x in arr_str]
cum_sum_arr = []
cum_sum_arr.append(0)
cum_sum = 0
for i in range(0, len(arr)):
cum_sum = cum_sum ^ arr[i]
cum_sum_arr.append(cum_sum)
count_pair = 0
sum2ind_odd = dict()
sum2ind_even = dict()
sum2ind_even[0] = 1
for r in range(1, len(cum_sum_arr)):
if r % 2 == 0:
if cum_sum_arr[r] in sum2ind_even:
count_pair += sum2ind_even[cum_sum_arr[r]]
sum2ind_even[cum_sum_arr[r]] += 1
else:
sum2ind_even[cum_sum_arr[r]] = 1
elif cum_sum_arr[r] in sum2ind_odd:
count_pair += sum2ind_odd[cum_sum_arr[r]]
sum2ind_odd[cum_sum_arr[r]] += 1
else:
sum2ind_odd[cum_sum_arr[r]] = 1
print(count_pair) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
a = [int(i) for i in input().split(" ")]
class SegmentTree(object):
def __init__(self, nums):
self.n = len(nums)
self.tree = [0] * (2 * self.n)
def buildTree(nums):
self.tree[self.n :] = nums[:]
for i in range(self.n - 1, 0, -1):
self.tree[i] = self.tree[2 * i] ^ self.tree[2 * i + 1]
buildTree(nums)
def update(self, i, val):
i += self.n
self.tree[i] = val
while i > 0:
self.tree[i // 2] = self.tree[i // 2 * 2] + self.tree[i // 2 * 2 + 1]
i //= 2
def sumRange(self, i, j):
i, j = i + self.n, j + self.n
sums = 0
while i <= j:
if i % 2 == 1:
sums ^= self.tree[i]
i += 1
if j % 2 == 0:
sums ^= self.tree[j]
j -= 1
i //= 2
j //= 2
return sums
res = 0
now = 0
os = [0] * (1 << 20)
os[0] = 1
js = [0] * (1 << 20)
for i in range(1, n + 1):
now ^= a[i - 1]
if i & 1:
res += js[now]
js[now] += 1
else:
res += os[now]
os[now] += 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
arr = list(map(int, input().strip().split()))
mp = {}
pre = [0] * n
pre[0] = arr[0]
for i in range(1, n):
pre[i] = pre[i - 1] ^ arr[i]
ans = 0
for i in range(n):
x = 0 ^ pre[i]
if x in mp:
ans += mp[x][(i + 1) % 2]
if pre[i] == 0:
if (i + 1) % 2 == 0:
ans += 1
if pre[i] in mp:
mp[pre[i]][(i + 1) % 2] += 1
else:
a = []
if (i + 1) % 2 == 0:
a = [1, 0]
else:
a = [0, 1]
mp[pre[i]] = a
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | input()
d = {(0, 0): 1}
s = i = 0
for x in map(int, input().split()):
s ^= x
i ^= 1
d[s, i] = d.get((s, i), 0) + 1
print(sum(d[x] * (d[x] - 1) // 2 for x in d)) | EXPR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.