-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoldMining.java
More file actions
43 lines (33 loc) · 1.25 KB
/
Copy pathGoldMining.java
File metadata and controls
43 lines (33 loc) · 1.25 KB
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
package Arrays;
import java.util.Arrays;
/*
https://www.geeksforgeeks.org/gold-mine-problem/?ref=lbp
*/
public class GoldMining {
public static void main(String[] args) {
int gold[][]= { {1, 3, 1, 5},
{2, 2, 4, 1},
{5, 0, 2, 3},
{0, 6, 1, 2} };
System.out.println(solution(gold));
}
public static int solution(int[][] goldMine) {
int[][] dp = new int[goldMine.length][goldMine[0].length];
for (var arr : dp) {
Arrays.fill(arr, 0);
}
for (var col = goldMine[0].length - 1; col >= 0; col--) {
for (var row = 0; row < goldMine.length; row++) {
int right = (col == goldMine[0].length - 1) ? 0 : dp[row][col + 1];
int rightUp = ((col == goldMine[0].length -1) || (row == 0)) ? 0 : dp[row-1][col + 1];
int rightDown = ((col == goldMine[0].length - 1) || (row == goldMine.length - 1)) ? 0 : dp[row + 1][col + 1];
dp[row][col] = goldMine[row][col] + Math.max(right, Math.max(rightUp, rightDown));
}
}
int max = dp[0][0];
for (var i = 1; i < goldMine.length; i++) {
max = Math.max(max, dp[i][0]);
}
return max;
}
}