description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
def helper(i, j, a):
if i in [-1, len(grid)] or j in [-1, len(grid[0])] or grid[i][j] in [-1, 0]:
return a
y = grid[i][j]
x = a + y
grid[i][j] = -1
a = max(
helper(i + 1, j, x),
helper(i - 1, j, x),
helper(i, j + 1, x),
helper(i, j - 1, x),
)
grid[i][j] = y
return a
ans = -1
for i in range(len(grid)):
for j in range(len(grid[0])):
ans = max(ans, helper(i, j, 0))
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR LIST NUMBER FUNC_CALL VAR VAR VAR LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR LIST NUMBER NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def __init__(self):
self.moves = [(1, 0), (-1, 0), (0, 1), (0, -1)]
def getMaximumGold(self, grid: List[List[int]]) -> int:
best = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if self.canMove(i, j, grid, set()):
total = self.moveNext(i, j, grid, set(), 0)
best = max(best, total)
return best
def moveNext(self, x, y, grid, visited, total):
visited.add((x, y))
total = total + grid[x][y]
best = total
for move in self.moves:
if self.canMove(x + move[0], y + move[1], grid, visited):
s = self.moveNext(x + move[0], y + move[1], grid, visited.copy(), total)
if s > best:
best = s
return best
def canMove(self, x, y, grid, visited):
return (
(x >= 0 and x < len(grid))
and (y >= 0 and y < len(grid[0]))
and grid[x][y] != 0
and (x, y) not in visited
) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF RETURN VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, a: List[List[int]]) -> int:
def dfs(a, r, c, curSum, res):
res[0] = max(res[0], curSum)
temp = a[r][c]
a[r][c] = -1
directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
for d in directions:
nr, nc = r + d[0], c + d[1]
if nr >= 0 and nr < len(a) and nc >= 0 and nc < len(a[0]):
if a[nr][nc] > 0:
dfs(a, nr, nc, curSum + a[nr][nc], res)
a[r][c] = temp
return
res = [0]
for i in range(0, len(a)):
for j in range(0, len(a[0])):
if a[i][j] > 0:
dfs(a, i, j, a[i][j], res)
return res[0] | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR NUMBER VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def _getMaximumGold(self, cur_sum, i, j, grid):
m = len(grid)
n = len(grid[0])
max_sum = cur_sum
if i + 1 < m and grid[i + 1][j] != 0:
val = grid[i + 1][j]
grid[i + 1][j] = 0
temp_max_sum = self._getMaximumGold(cur_sum + val, i + 1, j, grid)
grid[i + 1][j] = val
if temp_max_sum > max_sum:
max_sum = temp_max_sum
if j + 1 < n and grid[i][j + 1] != 0:
val = grid[i][j + 1]
grid[i][j + 1] = 0
temp_max_sum = self._getMaximumGold(cur_sum + val, i, j + 1, grid)
grid[i][j + 1] = val
if temp_max_sum > max_sum:
max_sum = temp_max_sum
if i - 1 >= 0 and grid[i - 1][j] != 0:
val = grid[i - 1][j]
grid[i - 1][j] = 0
temp_max_sum = self._getMaximumGold(cur_sum + val, i - 1, j, grid)
grid[i - 1][j] = val
if temp_max_sum > max_sum:
max_sum = temp_max_sum
if j - 1 >= 0 and grid[i][j - 1] != 0:
val = grid[i][j - 1]
grid[i][j - 1] = 0
temp_max_sum = self._getMaximumGold(cur_sum + val, i, j - 1, grid)
grid[i][j - 1] = val
if temp_max_sum > max_sum:
max_sum = temp_max_sum
return max_sum
def getMaximumGold(self, grid: List[List[int]]) -> int:
if grid is None:
return 0
m = len(grid)
n = len(grid[0])
total_max_sum = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 0:
continue
start = grid[i][j]
grid[i][j] = 0
max_sum = self._getMaximumGold(start, i, j, grid)
if max_sum > total_max_sum:
total_max_sum = max_sum
grid[i][j] = start
return total_max_sum | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
self.ret = 0
self.grid = grid
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] != 0:
self.dfs(i, j, 0)
return self.ret
def dfs(self, i, j, cur_gold):
self.ret = max(self.ret, cur_gold)
if (0 <= i < len(self.grid) and 0 <= j < len(self.grid[0])) and self.grid[i][
j
] != 0:
gold = self.grid[i][j]
for m, n in [(-1, 0), (0, -1), (0, 1), (1, 0)]:
self.grid[i][j] = 0
self.dfs(i + m, j + n, cur_gold + gold)
self.grid[i][j] = gold | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | def is_valid_cell(row, col, rows, cols):
return 0 <= row < rows and 0 <= col < cols
class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
rows = len(grid)
cols = len(grid[0])
max_amount = 0
def backtrack(row, col, rows, cols, grid):
if not is_valid_cell(row, col, rows, cols) or grid[row][col] == 0:
return 0
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
original = grid[row][col]
grid[row][col] = 0
max_sum = 0
for dr, dc in directions:
r = row + dr
c = col + dc
max_sum = max(max_sum, backtrack(r, c, rows, cols, grid))
grid[row][col] = original
return original + max_sum
for row in range(rows):
for col in range(cols):
ans = backtrack(row, col, rows, cols, grid)
max_amount = max(max_amount, ans)
return max_amount | FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
def dfs(i, j, seen):
if (
i < 0
or j < 0
or j >= len(grid[0])
or i >= len(grid)
or (i, j) in seen
or grid[i][j] == 0
):
return 0
seen.add((i, j))
t = grid[i][j] + max(
dfs(i + 1, j, seen),
dfs(i, j + 1, seen),
dfs(i - 1, j, seen),
dfs(i, j - 1, seen),
)
seen.remove((i, j))
return t
ans = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
ans = max(dfs(i, j, set()), ans)
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
self.orig_grid = grid
self.M = len(grid)
self.N = len(grid[0])
max_gold = 0
for i in range(self.M):
for j in range(self.N):
self.soln = [[(0) for j in range(self.N)] for i in range(self.M)]
curr = self.trackGold(i, j)
max_gold = max(max_gold, curr)
return max_gold
def trackGold(self, i, j):
if (
i < 0
or i >= self.M
or j < 0
or j >= self.N
or self.orig_grid[i][j] == 0
or self.soln[i][j] == 1
):
return 0
self.soln[i][j] = 1
future_gold = max(
self.trackGold(i + 1, j),
self.trackGold(i, j + 1),
self.trackGold(i - 1, j),
self.trackGold(i, j - 1),
)
curr_gold = self.orig_grid[i][j] + future_gold
self.soln[i][j] = 0
return curr_gold | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
self.maxGold = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] > 0:
used = set()
used.add((i, j))
self.dfs(i, j, 0, grid, used)
return self.maxGold
def dfs(self, i, j, total, grid, used):
total += grid[i][j]
if total > self.maxGold:
self.maxGold = total
for ni, nj in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
if (
0 <= ni < len(grid)
and 0 <= nj < len(grid[0])
and (ni, nj) not in used
and grid[ni][nj] > 0
):
used.add((ni, nj))
self.dfs(ni, nj, total, grid, used)
used.remove((ni, nj)) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
n = len(grid)
m = len(grid[0])
p = 0
for i in range(n):
for j in range(m):
if grid[i][j] != 0:
p = max(p, self.dfs(grid, i, j, m, n))
return p
def dfs(self, grid, starti, startj, m, n):
visited = [[(0) for i in range(m)] for j in range(n)]
return self.dfsUtil(grid, starti, startj, visited, m, n)
def dfsUtil(self, grid, i, j, visited, m, n):
ct = grid[i][j]
k = 0
if visited[i][j] == 0 and grid[i][j] != 0:
visited[i][j] = 1
neighbours = [[i + 1, j], [i - 1, j], [i, j + 1], [i, j - 1]]
for neighbour in neighbours:
if (
neighbour[0] >= 0
and neighbour[0] < n
and neighbour[1] >= 0
and neighbour[1] < m
):
k = max(
k, self.dfsUtil(grid, neighbour[0], neighbour[1], visited, m, n)
)
visited[i][j] = 0
return ct + k
else:
return 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR RETURN NUMBER |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
def dfs(i: int, j: int, sum_so_far: int, seen: set) -> int:
if i < 0 or i >= m or j < 0 or j >= n or not grid[i][j] or (i, j) in seen:
return sum_so_far
seen.add((i, j))
sum_so_far += grid[i][j]
mx = 0
for x, y in ((i, j + 1), (i, j - 1), (i + 1, j), (i - 1, j)):
mx = max(dfs(x, y, sum_so_far, seen), mx)
seen.discard((i, j))
return mx
m, n = len(grid), len(grid[0])
all_gold_path = []
for row in range(m):
for col in range(n):
all_gold_path.append(dfs(row, col, 0, set()))
return max(all_gold_path) | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getNeighbors(self, root, grid):
deltas = [(0, 1), (1, 0), (0, -1), (-1, 0)]
x, y = root
return [
(x + dx, y + dy)
for dx, dy in deltas
if 0 <= x + dx < len(grid) and 0 <= y + dy < len(grid[x])
]
def dfs(self, root, grid, visited):
if root in visited:
return 0
visited.add(root)
gold = 0
for nx, ny in self.getNeighbors(root, grid):
if (nx, ny) in visited:
continue
if grid[nx][ny] == 0:
continue
gold = max(gold, self.dfs((nx, ny), grid, visited))
visited.remove(root)
return gold + grid[root[0]][root[1]]
def getMaximumGold(self, grid: List[List[int]]) -> int:
maxGold = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 0:
continue
maxGold = max(maxGold, self.dfs((i, j), grid, set()))
return maxGold
[[0, 0, 19, 5, 8], [11, 20, 14, 1, 0], [0, 0, 1, 1, 1], [0, 2, 0, 2, 0]] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR RETURN BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR RETURN VAR VAR EXPR LIST LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
visited = set()
max_gold = 0
def dfs(r, c, max_gold):
if (r, c) in visited or grid[r][c] == 0:
return max_gold
visited.add((r, c))
for i, j in ((1, 0), (0, 1), (-1, 0), (0, -1)):
if 0 <= r + i < len(grid) and 0 <= c + j < len(grid[0]):
max_gold = max(max_gold, grid[r][c] + dfs(r + i, c + j, 0))
visited.remove((r, c))
return max_gold
for r in range(len(grid)):
for c in range(len(grid[0])):
if grid[r][c] > 0:
max_gold = max(max_gold, dfs(r, c, 0))
return max_gold | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
if not grid:
return 0
def dfs(i, j, gold, visited):
nonlocal ans
if (
i < 0
or i >= rows
or j < 0
or j >= cols
or (i, j) in visited
or grid[i][j] == 0
):
ans = max(ans, gold)
return
visited.add((i, j))
for di, dj in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
dfs(i + di, j + dj, gold + grid[i][j], visited)
visited.remove((i, j))
ans, rows, cols = 0, len(grid), len(grid[0])
for row in range(rows):
for col in range(cols):
dfs(row, col, 0, set())
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
def nextPos(r, c):
if r > 0:
yield r - 1, c
if r < m - 1:
yield r + 1, c
if c > 0:
yield r, c - 1
if c < n - 1:
yield r, c + 1
self.mx = 0
def backtrack(r, c, visited, gold):
self.mx = max(self.mx, gold)
for nr, nc in nextPos(r, c):
if not visited[nr][nc] and grid[nr][nc] > 0:
gold += grid[nr][nc]
visited[nr][nc] = True
backtrack(nr, nc, visited, gold)
gold -= grid[nr][nc]
visited[nr][nc] = False
m = len(grid)
n = len(grid[0])
for i in range(m):
for j in range(n):
visited = [[(False) for _ in range(n)] for _ in range(m)]
visited[i][j] = True
if grid[i][j] > 0:
backtrack(i, j, visited, grid[i][j])
return self.mx | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER EXPR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER EXPR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
max_total = 0
num_rows = len(grid)
num_cols = len(grid[0])
for i in range(num_rows):
for j in range(num_cols):
if grid[i][j] == 0:
continue
start_pos = i, j
visited = {(i, j)}
stack = [(start_pos, visited, grid[i][j])]
while stack:
(x, y), visited, running_total = stack.pop(0)
neighbors = (x, y - 1), (x, y + 1), (x - 1, y), (x + 1, y)
valid_neighbors = [
(i, j)
for i, j in neighbors
if 0 <= i < num_rows
and 0 <= j < num_cols
and grid[i][j] != 0
and (i, j) not in visited
]
if not valid_neighbors:
if running_total > max_total:
max_total = running_total
continue
else:
for x, y in valid_neighbors:
copy = visited.copy()
copy.add((x, y))
stack.append(((x, y), copy, running_total + grid[x][y]))
return max_total | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR IF VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
d = [[-1, 0], [0, 1], [1, 0], [0, -1]]
m = len(grid)
if m == 0:
return 0
n = len(grid[0])
visited = []
visited_in = []
for i in range(m):
for j in range(n):
visited_in.append(False)
visited.append(visited_in)
visited_in = []
def inArea(x, y):
return x >= 0 and x < m and y >= 0 and y < n
def DFS(grid, startx, starty):
res = 0
visited[startx][starty] = True
for i in range(4):
newx = startx + d[i][0]
newy = starty + d[i][1]
if (
inArea(newx, newy)
and not visited[newx][newy]
and grid[newx][newy] != 0
):
res = max(res, grid[newx][newy] + DFS(grid, newx, newy))
visited[startx][starty] = False
return res
maxRes = []
record_each_result = []
for i in range(m):
for j in range(n):
if grid[i][j] != 0 and not visited[i][j]:
result = grid[i][j] + DFS(grid, i, j)
record_each_result.append(result)
final_result = max(record_each_result)
return final_result | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF RETURN VAR NUMBER VAR VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
if not grid:
return 0
m, n = len(grid), len(grid[0])
result = 0
best_path = set()
def dfs(i, j, visited, res):
nonlocal result, best_path
if res > result:
best_path = visited
result = res
for a, b in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
ci, cj = a + i, b + j
if (
0 <= ci < m
and 0 <= cj < n
and (ci, cj) not in visited
and grid[ci][cj] != 0
):
dfs(ci, cj, visited | set([(ci, cj)]), res + grid[ci][cj])
for i in range(m):
for j in range(n):
if grid[i][j] != 0 and (i, j) not in best_path:
dfs(i, j, set([(i, j)]), grid[i][j])
return result | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def __init__(self):
self.max = 0
def getMaximumGold(self, grid: List[List[int]]) -> int:
for i in range(len(grid)):
for j in range(len(grid[i])):
self.max = max(self.dfs(grid, i, j), self.max)
return self.max
def dfs(self, grid, i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[i]) or grid[i][j] == 0:
return 0
temp = grid[i][j]
grid[i][j] = 0
up = temp + self.dfs(grid, i - 1, j)
down = temp + self.dfs(grid, i + 1, j)
left = temp + self.dfs(grid, i, j - 1)
right = temp + self.dfs(grid, i, j + 1)
grid[i][j] = temp
return max(max(up, down), max(left, right)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def __init__(self):
self.next_moves = [(0, 1), (0, -1), (1, 0), (-1, 0)]
def getMaximumGold(self, grid: List[List[int]]) -> int:
def dfs(row, col):
if grid[row][col] == 0:
return 0
gold = grid[row][col]
grid[row][col] = 0
max_child = 0
for move in self.next_moves:
next_row, next_col = row + move[0], col + move[1]
if -1 < next_row < n and -1 < next_col < m:
max_child = max(max_child, dfs(next_row, next_col))
grid[row][col] = gold
return gold + max_child
n = len(grid)
m = len(grid[0])
max_gold = 0
for i in range(n):
for j in range(m):
max_gold = max(max_gold, dfs(i, j))
return max_gold | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
def dfs(row, col, cur_sum, visited):
if (
row < 0
or col < 0
or row >= len(grid)
or col >= len(grid[0])
or visited[row][col] == 1
or grid[row][col] == 0
):
return
visited[row][col] = 1
cur_sum += grid[row][col]
self.max_path = max(self.max_path, cur_sum)
dfs(row + 1, col, cur_sum, visited)
dfs(row - 1, col, cur_sum, visited)
dfs(row, col + 1, cur_sum, visited)
dfs(row, col - 1, cur_sum, visited)
visited[row][col] = 0
visited = [[(0) for _ in range(len(grid[0]))] for _ in range(len(grid))]
self.max_path = 0
for row in range(len(grid)):
for col in range(len(grid[0])):
if grid[row][col] != 0:
dfs(row, col, 0, visited)
return self.max_path | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
def visit(i, j):
visited[i][j] = True
gold = 0
if i > 0 and not visited[i - 1][j]:
gold = max(gold, visit(i - 1, j))
if i < m - 1 and not visited[i + 1][j]:
gold = max(gold, visit(i + 1, j))
if j > 0 and not visited[i][j - 1]:
gold = max(gold, visit(i, j - 1))
if j < n - 1 and not visited[i][j + 1]:
gold = max(gold, visit(i, j + 1))
visited[i][j] = False
return gold + grid[i][j]
visited = [([False] * n) for i in range(m)]
def reset():
for i in range(m):
for j in range(n):
visited[i][j] = grid[i][j] == 0
max_gold = 0
for i in range(m):
for j in range(n):
if grid[i][j]:
reset()
max_gold = max(max_gold, visit(i, j))
return max_gold | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
res = 0
def dfs(x, y, grid):
if (
x < 0
or x > len(grid) - 1
or y < 0
or y > len(grid[0]) - 1
or grid[x][y] == 0
):
return 0
temp = grid[x][y]
grid[x][y] = 0
a = dfs(x + 1, y, grid) + temp
b = dfs(x, y + 1, grid) + temp
c = dfs(x - 1, y, grid) + temp
d = dfs(x, y - 1, grid) + temp
res = max([a, b, c, d])
grid[x][y] = temp
return res
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] > 0:
res = max(dfs(i, j, grid), res)
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
res = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
res = max(res, self.find(grid, i, j))
return res
def find(self, grid, i, j):
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or not grid[i][j]:
return 0
current = grid[i][j]
grid[i][j] = 0
up = self.find(grid, i - 1, j)
down = self.find(grid, i + 1, j)
left = self.find(grid, i, j - 1)
right = self.find(grid, i, j + 1)
maxx = max(up, down, left, right)
grid[i][j] = current
return maxx + current | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
csum = msum = 0
def backtrack(r, c):
direct = [(r - 1, c), (r + 1, c), (r, c + 1), (r, c - 1)]
nonlocal msum, csum
isbool = True
for p, q in direct:
if 0 <= p < m and 0 <= q < n and 0 < grid[p][q]:
isbool = False
value = grid[p][q]
csum += grid[p][q]
grid[p][q] = 0
backtrack(p, q)
grid[p][q] = value
csum -= value
if isbool:
msum = max(msum, csum)
for i in range(m):
for j in range(n):
if grid[i][j] != 0:
csum = value = grid[i][j]
grid[i][j] = 0
backtrack(i, j)
grid[i][j] = value
return msum | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
ans = 0
def bfs(a, s, d, f):
nonlocal ans
q = collections.deque([(a, s, d, f)])
while q:
j, k, tot, seen = q.popleft()
for m, n in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
jm, kn = j + m, k + n
if (
jm > -1
and kn > -1
and jm < len(grid)
and kn < len(grid[0])
and grid[jm][kn]
and (jm, kn) not in seen
):
q.append((jm, kn, tot + grid[jm][kn], seen | {(jm, kn)}))
else:
ans = max(ans, tot)
for i in range(len(grid)):
for x in range(len(grid[0])):
if grid[i][x]:
bfs(i, x, grid[i][x], {(i, x)})
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FOR VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
checked = set()
maximum = 0
for x in range(len(grid)):
for y in range(len(grid[0])):
if grid[x][y]:
visited = set()
value = self.recursive(0, x, y, grid, visited)
if value > maximum:
maximum = value
return maximum
def recursive(self, total, x, y, grid, visited):
if grid[x][y] == 0:
return total
if (x, y) in visited:
return total
visited = set(visited)
visited.add((x, y))
total = total + grid[x][y]
left, right, top, btm = total, total, total, total
if x > 0:
left = self.recursive(total, x - 1, y, grid, visited)
if x < len(grid) - 1:
right = self.recursive(total, x + 1, y, grid, visited)
if y > 0:
top = self.recursive(total, x, y - 1, grid, visited)
if y < len(grid[x]) - 1:
btm = self.recursive(total, x, y + 1, grid, visited)
return max(left, right, top, btm, total) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR IF VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | import sys
class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
rows = len(grid)
cols = len(grid[0])
def isValid(start, grid):
valid = []
r, c = start
up, right, left, down = True, True, True, True
if r == 0:
up = False
if r == len(grid) - 1:
down = False
if c == 0:
left = False
if c == len(grid[0]) - 1:
right = False
if down and grid[r + 1][c] != 0:
valid.append((r + 1, c))
if up and grid[r - 1][c] != 0:
valid.append((r - 1, c))
if right and grid[r][c + 1] != 0:
valid.append((r, c + 1))
if left and grid[r][c - 1] != 0:
valid.append((r, c - 1))
return valid
def findMaxPath(maxV, path, start, grid):
validList = isValid(start, grid)
if not validList:
if sum(path) > maxV:
maxV = sum(path)
for r, c in validList:
valI = grid[r][c]
grid[r][c] = 0
path.append(valI)
maxV = findMaxPath(maxV, path, (r, c), grid)
path.pop()
grid[r][c] = valI
return maxV
maxV = -sys.maxsize - 1
for r in range(rows):
for c in range(cols):
if grid[r][c] != 0:
val = grid[r][c]
grid[r][c] = 0
maxV = findMaxPath(maxV, [val], (r, c), grid)
grid[r][c] = val
return maxV | IMPORT CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
self.max_gold = 0
def backtrack(grid, row, col, current):
if 0 <= row < len(grid) and 0 <= col < len(grid[0]) and grid[row][col]:
temp = grid[row][col]
grid[row][col] = 0
for drow, dcol in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
backtrack(grid, row + drow, col + dcol, current + temp)
grid[row][col] = temp
else:
self.max_gold = max(self.max_gold, current)
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j]:
backtrack(grid, i, j, 0)
return self.max_gold | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
if not grid or not grid[0]:
return 0
maxGold = 0
m, n = len(grid), len(grid[0])
def dfs(i, j):
res = 0
if grid[i][j] == 0:
return 0
tmp = grid[i][j]
grid[i][j] = 0
for x, y in [[i + 1, j], [i - 1, j], [i, j + 1], [i, j - 1]]:
if 0 <= x < m and 0 <= y < n:
res = max(res, dfs(x, y))
grid[i][j] = tmp
return res + grid[i][j]
for i in range(m):
for j in range(n):
maxGold = max(maxGold, dfs(i, j))
return maxGold | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR LIST LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, A: List[List[int]]) -> int:
def dfs(i, j):
if not (0 <= i < m and 0 <= j < n):
return 0
if A[i][j] == 0:
return 0
if (i, j) in path:
return 0
path.add((i, j))
gold = 0
for ni, nj in ((i - 1, j), (i, j + 1), (i + 1, j), (i, j - 1)):
gold = max(gold, dfs(ni, nj))
path.discard((i, j))
return A[i][j] + gold
ans = 0
maxpath = set()
path = set()
m, n = len(A), len(A[0])
for i in range(m):
for j in range(n):
if (i, j) in maxpath:
pass
gold = dfs(i, j)
if gold > ans:
ans = gold
path.clear()
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF NUMBER VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def dfs(self, cur_gold, max_gold, grid, visited, i, j):
if (
i < 0
or i >= len(grid)
or j < 0
or j >= len(grid[0])
or visited[i][j] == 1
or grid[i][j] == 0
):
return
visited[i][j] = 1
self.dfs(cur_gold + grid[i][j], max_gold, grid, visited, i + 1, j)
self.dfs(cur_gold + grid[i][j], max_gold, grid, visited, i - 1, j)
self.dfs(cur_gold + grid[i][j], max_gold, grid, visited, i, j + 1)
self.dfs(cur_gold + grid[i][j], max_gold, grid, visited, i, j - 1)
max_gold[0] = max(max_gold[0], cur_gold + grid[i][j])
visited[i][j] = 0
def getMaximumGold(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
visited = [[(0) for i in range(n)] for j in range(m)]
res = 0
for i in range(m):
for j in range(n):
if grid[i][j] > 0:
max_gold = [0]
visited = [[(0) for i in range(n)] for j in range(m)]
self.dfs(0, max_gold, grid, visited, i, j)
res = max(res, max_gold[0])
return res | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF VAR VAR VAR 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
self.res = 0
m, n = len(grid), len(grid[0])
def dfs(x, y, visited, cnt):
if any(
0 <= nx < m
and 0 <= ny < n
and grid[nx][ny] != 0
and (nx, ny) not in visited
for nx, ny in [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]]
):
for nx, ny in [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]]:
if (
0 <= nx < m
and 0 <= ny < n
and grid[nx][ny] != 0
and (nx, ny) not in visited
):
dfs(nx, ny, visited | {(nx, ny)}, cnt + grid[nx][ny])
else:
self.res = max(self.res, cnt)
for i in range(m):
for j in range(n):
if grid[i][j] != 0:
dfs(i, j, {(i, j)}, grid[i][j])
return self.res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR LIST LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER FOR VAR VAR LIST LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def explore(self, grid, row, col):
if (
row < 0
or row >= len(grid)
or col < 0
or col >= len(grid[0])
or grid[row][col] == 0
):
return 0
total = curr_gold = grid[row][col]
max_gold = grid[row][col] = 0
for row_offset, col_offset in ((-1, 0), (0, 1), (1, 0), (0, -1)):
max_gold = max(
max_gold, self.explore(grid, row + row_offset, col + col_offset)
)
grid[row][col] = curr_gold
return total + max_gold
def getMaximumGold(self, grid: List[List[int]]) -> int:
max_gold = 0
for row in range(len(grid)):
for col in range(len(grid[0])):
max_gold = max(max_gold, self.explore(grid, row, col))
return max_gold | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
self.nums = grid.copy()
self.results = []
self.visited = [
[(False) for i in range(len(grid[0]))] for i in range(len(grid))
]
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] != 0:
maxVal = self.getTheGold(i, j, 0)
return max(self.results)
def getTheGold(self, i, j, value):
if self.isSafe(i, j):
self.visited[i][j] = True
self.getTheGold(i + 1, j, value + self.nums[i][j])
self.getTheGold(i, j + 1, value + self.nums[i][j])
self.getTheGold(i - 1, j, value + self.nums[i][j])
self.getTheGold(i, j - 1, value + self.nums[i][j])
self.visited[i][j] = False
else:
return self.results.append(value)
def isSafe(self, i, j):
if i >= 0 and i < len(self.nums) and j >= 0 and j < len(self.nums[0]):
return self.nums[i][j] != 0 and self.visited[i][j] == False
return False | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST 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 FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
maxCount = 0
N = len(grid)
M = len(list(zip(*grid)))
for row in range(N):
for col in range(M):
if grid[row][col] != 0:
maxCount = max(self.backtrack(grid, row, col, N, M), maxCount)
return maxCount
def backtrack(self, grid, row, col, N, M):
value = grid[row][col]
grid[row][col] = -1
maxCollected = 0
if row + 1 < N and grid[row + 1][col] not in (0, -1):
maxCollected = max(self.backtrack(grid, row + 1, col, N, M), maxCollected)
if col + 1 < M and grid[row][col + 1] not in (0, -1):
maxCollected = max(self.backtrack(grid, row, col + 1, N, M), maxCollected)
if row - 1 >= 0 and grid[row - 1][col] not in (0, -1):
maxCollected = max(self.backtrack(grid, row - 1, col, N, M), maxCollected)
if col - 1 >= 0 and grid[row][col - 1] not in (0, -1):
maxCollected = max(self.backtrack(grid, row, col - 1, N, M), maxCollected)
grid[row][col] = value
return value + maxCollected | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
def getGold(i, j, s, money):
nonlocal mx
for c1, c2 in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]:
if (
(c1, c2) not in s
and 0 <= c1 < len(grid)
and 0 <= c2 < len(grid[0])
and grid[c1][c2] != 0
):
s.add((c1, c2))
getGold(c1, c2, s, money + grid[c1][c2])
s.remove((c1, c2))
mx = max(mx, money)
mx = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
s = set()
if grid[i][j] != 0:
s.add((i, j))
getGold(i, j, s, grid[i][j])
return mx | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
grid_height, grid_width = len(grid), len(grid[0])
visited = set()
def helper(i, j, gold=0):
if (
i < 0
or i >= grid_height
or j < 0
or j >= grid_width
or (i, j) in visited
or grid[i][j] == 0
):
return gold
visited.add((i, j))
res = 0
for a, b in ((-1, 0), (1, 0), (0, -1), (0, 1)):
res = max(helper(i + a, j + b, gold + grid[i][j]), res)
visited.remove((i, j))
return res
return max(helper(i, j) for i in range(grid_height) for j in range(grid_width)) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
ans = 0
row = len(grid)
if not row:
return 0
col = len(grid[0])
if not col:
return 0
dc = [0, 1, 0, -1]
dr = [-1, 0, 1, 0]
def dfs(i, j):
if i < 0 or i >= row or j < 0 or j >= col or grid[i][j] < 1:
return 0
t = 0
grid[i][j] = -grid[i][j]
for d in range(4):
t = max(t, dfs(i + dr[d], j + dc[d]))
grid[i][j] = -grid[i][j]
return t + grid[i][j]
for i in range(row):
for j in range(col):
if grid[i][j]:
gold = dfs(i, j)
ans = max(gold, ans)
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
seen = set()
n, m = len(grid), len(grid[0])
self.ans = 0
def dfs(r, c, cur=0):
seen.add((r, c))
cur += grid[r][c]
for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
x, y = r + dx, c + dy
if 0 <= x < n and 0 <= y < m and (x, y) not in seen and grid[x][y] != 0:
dfs(x, y, cur)
seen.remove((r, c))
self.ans = max(self.ans, cur)
for i in range(n):
for j in range(m):
if (i, j) not in seen and grid[i][j] != 0:
dfs(i, j)
return self.ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
self.ans = 0
m = len(grid)
n = len(grid[0])
def backtrack(i, j, sumup):
nonlocal n, m
if i < 0 or i > m - 1 or j < 0 or j > n - 1 or grid[i][j] == 0:
return
sumup += grid[i][j]
self.ans = max(self.ans, sumup)
temp = grid[i][j]
grid[i][j] = 0
backtrack(i - 1, j, sumup)
backtrack(i + 1, j, sumup)
backtrack(i, j - 1, sumup)
backtrack(i, j + 1, sumup)
grid[i][j] = temp
for i in range(m):
for j in range(n):
backtrack(i, j, 0)
return self.ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
n = len(grid)
m = len(grid[0])
values = [[(0) for j in range(0, m)] for i in range(0, n)]
visited = [[(0) for j in range(0, m)] for i in range(0, n)]
_max = 0
for i in range(0, n):
for j in range(0, m):
if grid[i][j] != 0:
self._clear(visited)
values[i][j] = self._dfs(i, j, grid, visited)
_max = max(_max, values[i][j])
return _max
def _clear(self, grid: List[List[int]]) -> None:
n = len(grid)
m = len(grid[0])
for i in range(0, n):
for j in range(0, m):
grid[i][j] = 0
def _dfs(
self, i: int, j: int, grid: List[List[int]], visited: List[List[int]]
) -> int:
if grid[i][j] == 0:
return 0
if visited[i][j] != 0:
return 0
n = len(grid)
m = len(grid[0])
visited[i][j] = 1
idx = [(0, 1), (0, -1), (1, 0), (-1, 0)]
_max = 0
for p in idx:
if i + p[0] >= 0 and i + p[0] < n and j + p[1] >= 0 and j + p[1] < m:
v = self._dfs(i + p[0], j + p[1], grid, visited)
_max = max(_max, v)
visited[i][j] = 0
return grid[i][j] + _max | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NONE FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaxGoldHelper(self, visited, grid, curx, cury):
if (curx, cury) in visited:
return 0
visited.add((curx, cury))
ret = grid[cury][curx]
max_recur = 0
for nextx, nexty in [
(curx - 1, cury),
(curx + 1, cury),
(curx, cury - 1),
(curx, cury + 1),
]:
if nextx < 0 or nextx >= len(grid[0]) or nexty < 0 or nexty >= len(grid):
continue
if grid[nexty][nextx] > 0:
max_recur = max(
max_recur, self.getMaxGoldHelper(visited, grid, nextx, nexty)
)
visited.remove((curx, cury))
return ret + max_recur
def getMaximumGold(self, grid: List[List[int]]) -> int:
max_seen = 0
for row in range(len(grid)):
for col in range(len(grid[row])):
max_seen = max(max_seen, self.getMaxGoldHelper(set(), grid, col, row))
return max_seen | CLASS_DEF FUNC_DEF IF VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
def is_valid(i, j):
if 0 <= i < m and 0 <= j < n:
return True
return False
def dfs(i, j, visited, cur_sum):
visited.add((i, j))
cur_sum += grid[i][j]
cur_max = 0
for d in directions:
ni, nj = i + d[0], j + d[1]
if is_valid(ni, nj) and (ni, nj) not in visited and grid[ni][nj] > 0:
cur_max = max(cur_max, dfs(ni, nj, visited, cur_sum))
else:
cur_max = max(cur_max, cur_sum)
visited.discard((i, j))
return cur_max
m, n = len(grid), len(grid[0])
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
max_gold = 0
visited = set()
for i in range(m):
for j in range(n):
if grid[i][j] != 0:
max_gold = max(max_gold, dfs(i, j, set(), 0))
return max_gold | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF NUMBER VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
nr, nc = len(grid), len(grid[0])
delta = [(1, 0), (-1, 0), (0, 1), (0, -1)]
def dfs(i, j):
gold, grid[i][j] = grid[i][j], 0
max_gold = 0
for dx, dy in delta:
x, y = i + dx, j + dy
if 0 <= x < nr and 0 <= y < nc and grid[x][y] > 0:
max_gold = max(max_gold, dfs(x, y))
grid[i][j] = gold
return max_gold + gold
return max(dfs(i, j) for i in range(nr) for j in range(nc) if grid[i][j] > 0) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
def getMaxGold(y, x, gold):
if y < 0 or y >= m or x < 0 or x >= n or grid[y][x] == 0:
return gold
gold += grid[y][x]
origin, grid[y][x] = grid[y][x], 0
maxGold = 0
for ax, ay in action:
maxGold = max(getMaxGold(y + ay, x + ax, gold), maxGold)
grid[y][x] = origin
return maxGold
action = [(1, 0), (0, 1), (-1, 0), (0, -1)]
m, n = len(grid), len(grid[0])
return max(getMaxGold(y, x, 0) for x in range(n) for y in range(m)) | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
def dfs(i: int, j: int, sum: int, seen: set) -> int:
mx = sum
for x, y in ((i, j + 1), (i, j - 1), (i + 1, j), (i - 1, j)):
if not (
x < 0
or x >= m
or y < 0
or y >= n
or not grid[x][y]
or (x, y) in seen
):
mx = max(dfs(x, y, sum + grid[x][y], seen | {(x, y)}), mx)
return mx
m, n = len(grid), len(grid[0])
res = 0
for j in range(n):
for i in range(m):
if grid[i][j] != 0:
seen = set()
seen.add((i, j))
sum = grid[i][j]
res = max(res, dfs(i, j, sum, seen))
return res | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
out = 0
for y, row in enumerate(grid):
for x, val in enumerate(row):
if val != 0:
cur_max = max_collect(x, y, grid)
out = max(out, cur_max)
return out
def max_collect(x, y, grid, visited=set([])):
if y < 0 or y >= len(grid):
return 0
if x < 0 or x >= len(grid[y]):
return 0
if grid[y][x] == 0:
return 0
if (x, y) in visited:
return 0
visited.add((x, y))
_max = 0
for xx, yy in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]:
_max = max(_max, max_collect(xx, yy, grid, visited))
visited.remove((x, y))
return _max + grid[y][x] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF FUNC_CALL VAR LIST IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
def dfs(i: int, j: int, sum_so_far: int, seen: set) -> int:
if i < 0 or i >= m or j < 0 or j >= n or not grid[i][j] or (i, j) in seen:
return sum_so_far
seen.add((i, j))
sum_so_far += grid[i][j]
mx = 0
for x, y in ((i, j + 1), (i, j - 1), (i + 1, j), (i - 1, j)):
mx = max(dfs(x, y, sum_so_far, seen), mx)
seen.discard((i, j))
return mx
m, n = len(grid), len(grid[0])
all_gold_path = 0
for row in range(m):
for col in range(n):
all_gold_path = max(all_gold_path, dfs(row, col, 0, set()))
return all_gold_path
class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
visited = set()
max_path = [0]
row_len = len(grid)
col_len = len(grid[0])
def gold_finder(cur_row, cur_col, visited, path_so_far):
if (
cur_row < 0
or cur_row >= row_len
or cur_col < 0
or cur_col >= col_len
or grid[cur_row][cur_col] == 0
):
max_path[0] = max(max_path[0], sum(path_so_far))
return
if (cur_row, cur_col) in visited:
return
visited.add((cur_row, cur_col))
gold_finder(
cur_row + 1, cur_col, visited, path_so_far + [grid[cur_row][cur_col]]
)
gold_finder(
cur_row - 1, cur_col, visited, path_so_far + [grid[cur_row][cur_col]]
)
gold_finder(
cur_row, cur_col + 1, visited, path_so_far + [grid[cur_row][cur_col]]
)
gold_finder(
cur_row, cur_col - 1, visited, path_so_far + [grid[cur_row][cur_col]]
)
visited.remove((cur_row, cur_col))
return
for row in range(row_len):
for col in range(col_len):
gold_finder(row, col, visited, [])
return max_path[0] | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN IF VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR LIST RETURN VAR NUMBER VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
self.gold = 0
def DFS(i, j):
if i < 0 or i >= m or j < 0 or j >= n:
return 0
if grid[i][j] == 0:
return 0
tmp = grid[i][j]
grid[i][j] = 0
self.gold = max(
tmp + DFS(i + 1, j),
tmp + DFS(i - 1, j),
tmp + DFS(i, j + 1),
tmp + DFS(i, j - 1),
)
grid[i][j] = tmp
return self.gold
res = 0
for i in range(m):
for j in range(n):
self.gold = 0
res = max(res, DFS(i, j))
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
if not grid:
return 0
results = []
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 0:
continue
tmp = grid[i][j]
grid[i][j] = "*"
self.helper([tmp], i, j, grid, results)
grid[i][j] = tmp
maxV = -sys.maxsize
for i in results:
if sum(i) > maxV:
maxV = sum(i)
return maxV
def helper(self, sub, ii, jj, grid, results):
if sub[-1] == 0:
results.append(sub.copy())
return
ll = [[ii - 1, jj], [ii + 1, jj], [ii, jj - 1], [ii, jj + 1]]
for l in ll:
if l[0] < 0 or l[0] > len(grid) - 1:
continue
if l[1] < 0 or l[1] > len(grid[0]) - 1:
continue
if grid[l[0]][l[1]] == "*":
continue
sub.append(grid[l[0]][l[1]])
tmp = grid[l[0]][l[1]]
grid[l[0]][l[1]] = "*"
self.helper(sub, l[0], l[1], grid, results)
grid[l[0]][l[1]] = tmp
sub.pop() | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN ASSIGN VAR LIST LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR |
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can't visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold. | class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
if not grid or not grid[0]:
return 0
r, c = len(grid), len(grid[0])
def dfs(i, j, curr, visited):
self.mx = max(self.mx, curr)
for di, dj in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
if (
0 <= i + di < r
and 0 <= j + dj < c
and grid[i + di][j + dj] != 0
and (i + di, j + dj) not in visited
):
visited.add((i + di, j + dj))
dfs(i + di, j + dj, curr + grid[i + di][j + dj], visited)
visited.remove((i + di, j + dj))
return
self.mx = 0
for i in range(r):
for j in range(c):
if grid[i][j] != 0:
dfs(i, j, grid[i][j], {(i, j)})
return self.mx | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want. | class Solution:
def letterCombinations(self, digits):
if not digits:
return []
MAPPING = ("0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz")
def directed_combinations(i, partial):
if i == len(digits):
result.append("".join(partial))
return
for c in MAPPING[int(digits[i])]:
directed_combinations(i + 1, partial + [c])
result = []
directed_combinations(0, [])
return result | CLASS_DEF FUNC_DEF IF VAR RETURN LIST ASSIGN VAR STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR |
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want. | class Solution:
def letterCombinations(self, digits):
interpret_digit = {
"1": "",
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
"0": " ",
}
all_combinations = [""] if digits else []
for digit in digits:
current_combinations = list()
for letter in interpret_digit[digit]:
for combination in all_combinations:
current_combinations.append(combination + letter)
all_combinations = current_combinations
return all_combinations | CLASS_DEF 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 ASSIGN VAR VAR LIST STRING LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want. | class Solution:
def letterCombinations(self, digits):
key = {
(2): "abc",
(3): "def",
(4): "ghi",
(5): "jkl",
(6): "mno",
(7): "pqrs",
(8): "tuv",
(9): "wxyz",
}
nonlocal out
out = [""]
def eachLetter(i):
if i < len(digits):
nonlocal out
if int(digits[i]) > 1:
x = []
for j in out:
for k in key[int(digits[i])]:
x.append(j + k)
out = x
eachLetter(i + 1)
eachLetter(0)
return [] if out[0] is "" else out | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST STRING FUNC_DEF IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR NUMBER STRING LIST VAR |
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want. | class Solution:
def __init__(self):
self.phone = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
def letterCombinations(self, digits):
if not digits:
return []
results = []
self.dfs(digits, results, "", 0)
return results
def dfs(self, digits, results, string, index):
if index == len(digits):
results.append(string)
else:
letters = self.phone[int(digits[index])]
for i in range(0, len(letters)):
self.dfs(digits, results, string + letters[i], index + 1) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF IF VAR RETURN LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR STRING NUMBER RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER |
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want. | class Solution:
def letterCombinations(self, digits):
def dfs(digits, current, result):
if not digits:
result.append(current)
return
for c in dic[digits[0]]:
dfs(digits[1:], current + c, result)
if not digits:
return []
dic = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}
result = []
dfs(digits, "", result)
return result | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR RETURN LIST ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR STRING VAR RETURN VAR |
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want. | class Solution:
def letterCombinations(self, digits):
if digits == None or len(digits) == 0:
return []
res = []
cur = ""
dmap = {}
dmap["0"] = [" "]
dmap["1"] = []
dmap["2"] = ["a", "b", "c"]
dmap["3"] = ["d", "e", "f"]
dmap["4"] = ["g", "h", "i"]
dmap["5"] = ["j", "k", "l"]
dmap["6"] = ["m", "n", "o"]
dmap["7"] = ["p", "q", "r", "s"]
dmap["8"] = ["t", "u", "v"]
dmap["9"] = ["w", "x", "y", "z"]
self.dfs(digits, dmap, 0, cur, res)
return res
def dfs(self, digits, dmap, idx, cur, res):
if idx == len(digits):
res.append(cur)
return
else:
c = digits[idx]
for i in dmap[c]:
self.dfs(digits, dmap, idx + 1, cur + i, res) | CLASS_DEF FUNC_DEF IF VAR NONE FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR DICT ASSIGN VAR STRING LIST STRING ASSIGN VAR STRING LIST ASSIGN VAR STRING LIST STRING STRING STRING ASSIGN VAR STRING LIST STRING STRING STRING ASSIGN VAR STRING LIST STRING STRING STRING ASSIGN VAR STRING LIST STRING STRING STRING ASSIGN VAR STRING LIST STRING STRING STRING ASSIGN VAR STRING LIST STRING STRING STRING STRING ASSIGN VAR STRING LIST STRING STRING STRING ASSIGN VAR STRING LIST STRING STRING STRING STRING EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR |
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want. | class Solution:
def letterCombinations(self, digits):
dic = {
"2": ["a", "b", "c"],
"3": ["d", "e", "f"],
"4": ["g", "h", "i"],
"5": ["j", "k", "l"],
"6": ["m", "n", "o"],
"7": ["p", "q", "r", "s"],
"8": ["t", "u", "v"],
"9": ["w", "x", "y", "z"],
}
if "0" in digits:
digits = digits.replace("0", "")
if "1" in digits:
digits = digits.replace("1", "")
count = 0
for a in "23456789":
if a not in digits:
count += 1
if count == 8:
return []
res = []
temp = [""]
i = 0
while i < len(digits):
for a in temp:
for b in dic[digits[i]]:
res.append(a + b)
temp = res
res = []
i += 1
return temp | CLASS_DEF FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING LIST STRING STRING STRING STRING IF STRING VAR ASSIGN VAR FUNC_CALL VAR STRING STRING IF STRING VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR NUMBER FOR VAR STRING IF VAR VAR VAR NUMBER IF VAR NUMBER RETURN LIST ASSIGN VAR LIST ASSIGN VAR LIST STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR NUMBER RETURN VAR |
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want. | class Solution:
def letterCombinations(self, digits):
self.dic = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}
if not digits:
return []
res = []
self.dfs(digits, 0, "", res)
return res
def dfs(self, digits, index, path, res):
if len(path) == len(digits):
res.append(path)
return
for i in range(len(self.dic[digits[index]])):
path_ = path + self.dic[digits[index]][i]
self.dfs(digits, index + 1, path_, res) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING IF VAR RETURN LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER STRING VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR |
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want. | class Solution:
def letterCombinations(self, digits):
if "" == digits:
return []
kvmaps = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}
rst = [""]
for digit in digits:
temp = [(s + c) for s in rst for c in kvmaps[digit]]
rst = temp
return rst | CLASS_DEF FUNC_DEF IF STRING VAR RETURN LIST ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want. | class Solution:
Dmap = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}
def letterCombinations(self, digits):
if "1" in digits or "0" in digits:
return []
if len(digits) == 0:
return []
return self.letterC(digits)
def letterC(self, digits):
if len(digits) == 0:
return [""]
res = []
for back in self.letterC(digits[1:]):
for fr in self.Dmap[digits[0]]:
res.append(fr + back)
return res | CLASS_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF IF STRING VAR STRING VAR RETURN LIST IF FUNC_CALL VAR VAR NUMBER RETURN LIST RETURN FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
l = arr.copy()
i = 0
c = 0
while i < len(arr):
arr[i] = l[c]
arr[i + 1] = l[n // 2 + c]
c += 1
i += 2
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
mid = n // 2
a = arr[:mid]
b = arr[mid:]
arr[0::2] = a
arr[1::2] = b
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
ans = [0] * n
k = 0
for i in range(0, n // 2):
ans[k] = arr[i]
k += 1
ans[k] = arr[n // 2 + i]
k += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
d = n // 2
for i in range(1, n - 2, 2):
arr.insert(i, arr[d])
arr.pop(d + 1)
d += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
for i in range(n // 2):
arr.append(arr[i])
arr.append(arr[n // 2 + i])
arr[:] = arr[n:]
return arr | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
a = arr[: n // 2]
b = arr[n // 2 :]
arr.clear()
for i in range(n // 2):
arr.append(a[i])
arr.append(b[i])
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
half = n // 2
l1 = arr[0:half]
l2 = arr[half:n]
del arr[:]
for i in range(half):
arr.append(l1[i])
arr.append(l2[i]) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
mid = n // 2
k = mid
i = 1
while mid < n:
arr.insert(i, arr[mid])
arr.pop(mid + 1)
i += 2
mid += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def r_shuffle(self, arr1, arr2, n, result):
if len(arr1) == len(arr2) == 1:
result.extend([arr1[0], arr2[0]])
else:
m = n // 2
self.r_shuffle(arr1[0:m], arr2[0:m], m, result)
self.r_shuffle(arr1[m:], arr2[m:], len(arr1[m:]), result)
def shuffleArray(self, arr, n):
result = []
middleI = n // 2
self.r_shuffle(arr[0:middleI], arr[middleI:], middleI, result)
for i in range(n):
arr[i] = result[i] | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
ans = []
mid = n // 2
for i in range(n // 2):
ans.append(arr[i])
ans.append(arr[i + mid])
for i in range(n):
arr[i] = ans[i] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
if len(arr) == 1:
return 0
else:
if n % 2 == 0:
mini = arr[0 : n // 2]
maxi = arr[n // 2 : n]
arr.clear()
lmin = len(mini)
lmax = len(maxi)
for i in range(lmax):
arr.append(mini[i])
arr.append(maxi[i])
if lmin > lmax:
arr.extend(maxi[lmax:lmin]) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
x = len(arr) // 2
y = arr[x:]
z = arr[:x]
ans = []
arr.clear()
for i in range(x):
ans.append(z[i])
ans.append(y[i])
arr.extend(ans)
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
x = n // 2
l = arr[:x]
r = arr[x:]
flg = 0
j = 0
k = 0
for i in range(n):
if flg == 0:
arr[i] = l[j]
j += 1
flg = 1
else:
arr[i] = r[k]
k += 1
flg = 0 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
l = []
mid = n / 2
a1 = arr[: int(mid)]
a2 = arr[int(mid) :]
for i in range(len(a2)):
l.append(a1[i])
l.append(a2[i])
for i in range(n):
arr[i] = l[i]
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
i = 0
p = []
k = len(arr) // 2
j = k
while j < n and i < k:
p.append(arr[i])
p.append(arr[j])
i += 1
j += 1
a[:] = p | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
l = []
k = arr[: n // 2]
j = arr[n // 2 :]
for i in range(len(k)):
l.append(k[i])
l.append(j[i])
for i in range(n):
arr[i] = l[i] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
a2 = []
for k in range(int(n / 2)):
a2.append(arr[k])
a2.append(arr[int(n / 2 + k)])
i = 0
for k in a2:
arr[i] = k
i += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
a = []
b = []
for i in range(n):
if i < n // 2:
a.append(arr[i])
elif i >= n // 2:
b.append(arr[i])
r = []
for k in range(n // 2):
r.append(a[k])
r.append(b[k])
arr[:] = r[:] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
a = list()
b = list()
for i in range(0, int(n / 2)):
a.append(arr[i])
for i in range(int(n / 2), n):
b.append(arr[i])
for i in range(0, n):
if i % 2 == 0:
arr[i] = a[int(i / 2)]
else:
arr[i] = b[int(i / 2)] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
def _solve(L, R):
if R - L <= 1:
return
M = R - L + 1
h = M // 2
hh = h // 2
if h % 2:
i, j, pv = L + hh, L + h, arr[L + h - 1]
while i < L + h - 1:
arr[i + hh] = arr[i]
arr[i] = arr[j]
i += 1
j += 1
arr[L + h + hh - 1] = pv
_solve(L, L + hh + hh - 1)
_solve(L + hh + hh, R)
else:
i, j = L + hh, L + h
while i < L + h:
arr[i], arr[j] = arr[j], arr[i]
i += 1
j += 1
_solve(L, L + h - 1)
_solve(L + h, R)
_solve(0, n - 1) | CLASS_DEF FUNC_DEF FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR WHILE VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
p = n // 2
for i in range(0, p):
t = arr.pop(0)
arr.append(t)
r = arr.pop(p - i - 1)
arr.append(r)
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
m = n // 2
l = []
l1 = []
for i in range(m):
l.append(arr[i])
for i in range(m, n):
l1.append(arr[i])
arr[::2] = l[:]
arr[1::2] = l1[:]
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
mid = n // 2
p = []
first = 0
second = n // 2
for index in range(n):
if index % 2 == 0:
p.append(arr[first])
first += 1
else:
p.append(arr[second])
second += 1
arr[:] = p | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
k = n // 2
a = []
for i in range(n // 2):
a.append(arr[i])
a.append(arr[i + k])
for i in range(0, n):
arr[i] = a[i]
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
l = len(arr)
p = [0] * l
for i in range(l):
if i < l // 2:
pos = i
pos = 2 * pos
p[pos] = arr[i]
else:
pos = i
pos = 2 * (pos - n) + 1
p[pos] = arr[i]
for i in range(l):
arr[i] = p[i] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
lis = []
s, m = 0, len(arr) // 2
while s < len(arr) // 2:
lis.append(arr[s])
lis.append(arr[m])
s += 1
m += 1
for i in range(len(arr)):
arr[i] = lis[i] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
a = arr[0 : n // 2]
b = arr[n // 2 :]
c1 = 0
c2 = 0
for i in range(n):
if i % 2 == 0:
arr[i] = a[c1]
c1 += 1
else:
arr[i] = b[c2]
c2 += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
t = n // 2
if n % 2 != 0:
t = t + 1
l = arr[:t]
f = arr[t:]
if n % 2 != 0:
k = 0
for i in range(0, t - 1):
arr[k] = l[i]
arr[k + 1] = f[i]
k = k + 2
arr[-1] = l[-1]
else:
k = 0
for i in range(0, t):
arr[k] = l[i]
arr[k + 1] = f[i]
k = k + 2 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
center, odd_condition = (n // 2, 1) if n % 2 == 0 else (n // 2 + 1, 0)
b1_array = arr[center:]
a1_array = arr[:center]
a1_count = 0
b1_count = 0
for i in range(n):
if i % 2 == 0:
arr[i] = a1_array[a1_count]
a1_count += 1
else:
arr[i] = b1_array[b1_count]
b1_count += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
k = list(arr)
a = 1
b = len(k) // 2
for i in range(1, len(arr) - 1, 2):
arr[i] = k[b]
arr[i + 1] = k[a]
b += 1
a += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
i = 0
mid = n // 2
j = mid
arr1 = [0] * n
m = 0
while i < mid and j < n:
arr1[m] = arr[i]
i += 1
m += 1
arr1[m] = arr[j]
j += 1
m += 1
for i in range(n):
arr[i] = arr1[i]
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
a = len(arr)
for i in range(1, a, 2):
arr.insert(i, None)
for i in range(a // 2):
arr[2 * i + 1] = arr[a + i]
for i in range(a // 2):
arr.pop()
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
x = 0
def solve(self, i, arr, n):
if i == n // 2 - 1:
return
a = arr[i - n // 2]
b = arr[i]
self.solve(i - 1, arr, n)
arr[i - n // 2 + self.x] = a
arr[i - n // 2 + 1 + self.x] = b
self.x += 1
def shuffleArray(self, arr, n):
i = 1
j = n // 2
ans = [(0) for _ in range(n)]
ans[0] = arr[0]
ans[n - 1] = arr[n - 1]
k = 1
while i < n // 2 and j < n:
ans[k] = arr[j]
k = k + 1
ans[k] = arr[i]
k = k + 1
i = i + 1
j = j + 1
for i in range(n):
arr[i] = ans[i]
return arr | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
b = arr[0 : n // 2]
c = arr[n // 2 : len(arr)]
d = []
for i in range(len(b)):
d.append(b[i])
d.append(c[i])
for i in range(len(arr)):
arr[i] = d[i]
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
arr1 = arr[: n // 2]
arr2 = arr[n // 2 :]
arr[::2] = arr1
arr[1::2] = arr2
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
d = []
d += arr
arr.clear()
for i in range(n // 2):
arr.append(d[i])
arr.append(d[i + n // 2]) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
arr1 = arr[: n // 2]
arr2 = arr[n // 2 :]
for i in range(n):
if i % 2 == 0:
arr[i] = arr1.pop(0)
elif i % 2 == 1:
arr[i] = arr2.pop(0)
arr.append("")
return arr | CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN VAR |
Given an array arr[] of n elements in the following format {a1, a2, a3, a4, .., an/2, b1, b2, b3, b4, ., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, , an/2, bn/2} without using extra space.
Example 1:
Input: n = 4, arr[] = {1, 2, 9, 15}
Output: 1 9 2 15
Explanation: a1=1 , a2=2 , b1=9 , b2=15
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
Example 2:
Input: n = 6
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 4 2 5 3 6
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function shuffleArray() that takes array arr[], and an integer n as parameters and modifies the given array according to the above-given pattern.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
0≤ arr[i]≤ 103 | class Solution:
def shuffleArray(self, arr, n):
res = []
l = 0
r = n // 2
for i in range(n):
if i % 2 == 0:
res.append(arr[l])
l += 1
else:
res.append(arr[r])
r += 1
for i in range(n):
arr[i] = res[i] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.