-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrodCuts.java
More file actions
71 lines (57 loc) · 1.44 KB
/
rodCuts.java
File metadata and controls
71 lines (57 loc) · 1.44 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
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.ArrayList;
import java.util.Arrays;
import java.util.prefs.BackingStoreException;
public class rodCuts {
// Return an array denoting the sequence in which you will make cuts. If two different sequences of cuts
// give same cost, return the lexicographically smallest
public ArrayList<Integer> ar, ans;
public long dp[][];
public int parent[][], n;
public long rec(int l, int r){
if (l +1 >= r)
return 0;
long ret = dp[l][r];
int bestind = 0;
if (ret != -1)
return ret;
ret = Long.MAX_VALUE;
for (int i = l + 1; i < r; i++){
long p = rec(l, i) + rec(i, r) + ar.get(r) - ar.get(l);
if (p < ret){
bestind = i;
ret = p;
}
}
parent[l][r] = bestind;
dp[l][r] = ret;
return ret;
}
public void back(int l, int r){
if (l + 1 >= r)
return;
ans.add(ar.get(parent[l][r]));
back(l, parent[l][r]);
back(parent[l][r], r);
}
public ArrayList<Integer> rodCut(int A, ArrayList<Integer> B) {
B.add(0, 0);
B.add(B.size(), A);
n = B.size();
ar = new ArrayList<>();
ans = new ArrayList<>();
dp = new long[n][n];
parent = new int[n][n];
ar.clear();
ar.addAll(B);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
dp[i][j] = -1;
rec(0, n -1);
back(0, n -1);
return ans;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new rodCuts().rodCut(6, new ArrayList<>(Arrays.asList(1,2, 5))));
}
}