-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode.java
More file actions
43 lines (37 loc) · 1005 Bytes
/
Copy pathLeetcode.java
File metadata and controls
43 lines (37 loc) · 1005 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
36
37
38
39
40
41
42
43
public class Leetcode{
// 3742. Maximum Path Score in a Grid
private int[][] grid;
private Integer[][][] f;
private final int inf = 1 << 30;
public int maxPathScore(int[][] grid, int k) {
this.grid = grid;
int m = grid.length;
int n = grid[0].length;
f = new Integer[m][n][k + 1];
int ans = dfs(m - 1, n - 1, k);
return ans < 0 ? -1 : ans;
}
private int dfs(int i, int j, int k) {
if (i < 0 || j < 0 || k < 0) {
return -inf;
}
if (i == 0 && j == 0) {
return 0;
}
if (f[i][j][k] != null) {
return f[i][j][k];
}
int res = grid[i][j];
int nk = k;
if (grid[i][j] > 0) {
--nk;
}
int a = dfs(i - 1, j, nk);
int b = dfs(i, j - 1, nk);
res += Math.max(a, b);
f[i][j][k] = res;
return res;
}
public static void main(String[] args) {
}
}