-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcut-rod.java
More file actions
84 lines (81 loc) · 1.84 KB
/
cut-rod.java
File metadata and controls
84 lines (81 loc) · 1.84 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
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.*;
class Solution{
static int[] p = new int[]{0,1,5,8,9,10,17,17,20,24,30};
static int[] r = new int[p.length];
public static void main(String[] args) {
Arrays.fill(r,Integer.MIN_VALUE);
System.out.println("--------------------");
System.out.println(cutRod(p,10));
System.out.println("--------------------");
System.out.println(memoizedCutRod(p,10,r));
System.out.println("--------------------");
System.out.println("--------------------");
System.out.println(bottomUpCutRod(p,10));
System.out.println("--------------------");
System.out.println(bottomUpCutRod2(p,7));
}
/**
int[] p : 价格数组
int n : 长度
function:返回长度为n的钢条的最大收益
*/
public static int cutRod(int[] p , int n){
if(n == 0) return 0;
int q = Integer.MIN_VALUE;
for(int i = 1;i<=n;i++){
q = Math.max(q,p[i] + cutRod(p,n-i));
}
return q;
}
/**
带备忘录的自顶向下方法
*/
public static int memoizedCutRod(int[] p,int n,int[] r){
if(r[n] >= 0) return r[n];
if(n==0) return 0;
int q = Integer.MIN_VALUE;
for(int i = 1;i<=n;i++){
q = Math.max(q,p[i] + memoizedCutRod(p,n-i,r));
}
r[n] = q;
return q;
}
/**
*/
public static int bottomUpCutRod(int[] p,int n){
int[] dp = new int[p.length];
dp[0] = 0;
for(int j = 1;j<=n;j++){
int q = Integer.MIN_VALUE;
for(int i =1;i<=j;i++){
q=Math.max(q,p[i] + dp[j-i]);
}
dp[j] = q;
}
return dp[n];
}
/**
*/
public static int bottomUpCutRod2(int[] p,int n){
int[] dp = new int[p.length];
int[] s = new int[p.length];
dp[0] = 0;
for(int j = 1;j<=n;j++){
int q = Integer.MIN_VALUE;
for(int i =1;i<=j;i++){
if(q < p[i] + dp[j-i]){
q = p[i]+dp[j-i];
s[j] = i;
}
}
dp[j] = q;
}
int t = n;
while(t>0){
System.out.print(s[t]+" ");
t-=s[t];
}
System.out.println();
return dp[n];
}
}