-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1219-PathWithMaximumGold.java
More file actions
35 lines (28 loc) · 918 Bytes
/
Copy path1219-PathWithMaximumGold.java
File metadata and controls
35 lines (28 loc) · 918 Bytes
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
class Solution {
int maxSum = 0;
public int getMaximumGold(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int[][] directions = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] != 0) {
dfs(grid, i, j, 0, directions);
}
}
}
return maxSum;
}
private void dfs(int[][] grid, int i, int j, int val, int[][] directions) {
if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0) {
maxSum = Math.max(maxSum, val);
return;
}
int temp = grid[i][j];
grid[i][j] = 0;
for (int[] d : directions) {
dfs(grid, i + d[0], j + d[1], temp + val, directions);
}
grid[i][j] = temp;
}
}