-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumPointsToReach.java
More file actions
36 lines (26 loc) · 920 Bytes
/
MinimumPointsToReach.java
File metadata and controls
36 lines (26 loc) · 920 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
package dp.gridproblems;
import java.util.Arrays;
public class MinimumPointsToReach {
static int[][] dp;
public static int dfs(int i, int j, int[][] arr, int n, int m) {
if (i == n - 1 && j == m - 1) {
return arr[i][j] > 0 ? 1 : Math.abs(arr[i][j]) + 1;
}
if (dp[i][j] != Integer.MIN_VALUE) return dp[i][j];
int ans = Integer.MAX_VALUE;
if (j + 1 < m) {
int x = -arr[i][j] + dfs(i, j + 1, arr, n, m);
ans = Math.min(ans, x);
}
if (i + 1 < n) {
int y = -arr[i][j] + dfs(i + 1, j, arr, n, m);
ans = Math.min(ans, y);
}
return dp[i][j] = ans <= 0 ? 1 : ans;
}
public int minPoints(int[][] points, int n, int m) {
dp = new int[n + 1][m + 1];
for (int[] temp : dp) Arrays.fill(temp, Integer.MIN_VALUE);
return dfs(0, 0, points, n, m);
}
}