-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcoinsInLine.java
More file actions
59 lines (43 loc) · 1.37 KB
/
coinsInLine.java
File metadata and controls
59 lines (43 loc) · 1.37 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.*;
class Pair<A, B>{
public A first;
public B second;
public Pair(A a, B b){
this.first = a;
this.second = b;
}
}
public class coinsInLine {
/*There are N coins (Assume N is even) in a line. Two players take turns to take a coin from one of the
* ends of the line until there are no more coins left. The player with the larger amount of money wins.
* Assume that you go first. Compute the maximum amount of money you can win.*/
public int maxcoinRecursive(List<Integer> l) {
int n = l.size();
@SuppressWarnings("rawtypes")
Pair[][] T = new Pair[n][n];
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
if (i == j)
T[i][j] = new Pair<Integer, Integer>(l.get(i), 0);
else
T[i][j] = new Pair<Integer, Integer>(0, 0);
}
for (int col = 1; col < n; col++){
for (int i = 0; i < n - col; i++){
int j = i + col;
if (l.get(i) + (int)T[i+1][j].second > l.get(j) + (int)T[i][j -1].second){
T[i][j].first = l.get(i) + (int) T[i+1][j].second;
T[i][j].second = (int) T[i +1][j].first;
}
else{
T[i][j].first = l.get(j) + (int) T[i][j -1].second;
T[i][j].second = (int) T[i][j -1].first;
}
}
}
return (int) T[0][n -1].first;
}
public static void main(String[] args) {
System.out.println(new coinsInLine().maxcoinRecursive(Arrays.asList(1,2,100,4)));
}
}