-
Notifications
You must be signed in to change notification settings - Fork 0
/
BalloonShooting.java
65 lines (59 loc) · 2.11 KB
/
BalloonShooting.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class BalloonShooting {
/**
* 1. if left and right remains ballon not hit: L * X * R
* 2. if on the left no balloon left but right remains: X * R; otherwise: X * L
* 3. if both left and right nothing left: X
*
* assume that all scores are positive
*/
public static void main(String[] args) {
BalloonShooting b = new BalloonShooting();
int[] ballons = new int[] {3,2,5};
System.out.println(b.shoot(ballons));
}
public int memoDP(int[] ballons) {
if (ballons == null || ballons.length == 0) return 0;
int[][] dp = new int[ballons.length+1][ballons.length];
for (int hits = 1; hits < ballons.length+1; hits++) {
for (int bln = 0; bln < ballons.length; bln++) {
dp[hits][bln] = 1;
getScore(ballons, dp[hits], bln);
}
}
}
public int shoot(int[] ballons) {
if (ballons == null || ballons.length == 0) return 0;
int[] record = new int[ballons.length];
// return backtrack(ballons, record);
int ret = 0;
for (int i = 0; i < ballons.length; i++) {
record[i] = 1;
ret = Math.max(backtrack(ballons, record, i), ret);
record[i] = 0;
}
return ret;
}
public int backtrack(int[] ballons, int[] record, int index) {
// if (index == ballons.length) return 0;
int ret = getScore(ballons, record, index);
int tmp = 0;
for (int i = 0; i < ballons.length; i++) {
if (record[i] != 0) continue;
record[i] = 1;
tmp = Math.max(tmp, backtrack(ballons, record, i));
record[i] = 0;
}
return ret + tmp;
}
private int getScore(int[] ballons, int[] record, int index) {
int score = ballons[index];
int left = 1, right = 1;
for (int i = index-1; i >= 0; i--) {
if (record[i] == 0) left = ballons[i];
}
for (int i = index+1; i < ballons.length; i++) {
if (record[i] == 0) right = ballons[i];
}
return score * left * right;
}
}