forked from Mickey0521/Codility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberSolitaire.java
36 lines (29 loc) · 1.11 KB
/
NumberSolitaire.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
package NumberSolitaire;
class Solution {
public int solution(int[] A) {
// main idea:
// using "dynamic programming" to build up the solution
// (bottom up)
int[] dp = new int[A.length];
dp[0] = A[0];
// build up from "dp[1], dp[2], ..., dp[A.length-1]"
for(int i=1; i<A.length; i++){
// keep the biggest one
// be very careful: could be negtive (so use Integer.MIN_VALUE)
int max = Integer.MIN_VALUE;
// a die could be "1 to 6"
for(int die=1; die<=6; die++){
if( i-die >= 0){
// very important: not "A[i-die]+A[i]"
// instead, have to use "dp[i-die]+A[i]"
max = Math.max( dp[i-die]+A[i], max );
// dynamic programming:
// take the best:
// takeBest( dp[i-j] + value[j], curBest )
}
}
dp[i] = max; // keep the best one as the dp value
}
return dp[A.length-1];
}
}