forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath with Maximum Gold.java
53 lines (40 loc) · 1.48 KB
/
Path with Maximum Gold.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Solution {
public static int[][] copy(int[][] src) {
if (src == null) {
return null;
}
return Arrays.stream(src).map(int[]::clone).toArray(int[][]::new);
}
static int helper(int[][] grid, int row, int col, int total){
if(row < 0 || row >= grid.length || col < 0 || col >= grid[0].length){
return total;
}
if(grid[row][col] == 0){
return 0;
}
if(grid[row][col] == -1){
return 0;
}
total += grid[row][col];
int temp = grid[row][col];
grid[row][col] = -1;
total = Math.max(Math.max(Math.max(Math.max(helper(grid, row + 1, col, total),
helper(grid, row, col + 1, total)),
helper(grid, row, col - 1, total)),
helper(grid, row - 1, col, total)),
total);
grid[row][col] = temp;
return total;
}
public int getMaximumGold(int[][] grid) {
int currMax = Integer.MIN_VALUE;
int[][] tempGrid;
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[0].length; j++){
tempGrid = copy(grid);
currMax = Math.max(helper(tempGrid, i, j, 0), currMax);
}
}
return currMax;
}
}