-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimumCostToCutAStick_1547.java
36 lines (34 loc) · 1.19 KB
/
MinimumCostToCutAStick_1547.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 org.example;
import java.util.Arrays;
public class MinimumCostToCutAStick_1547 {
class Solution {
public int minCost(int n, int[] cuts) {
int[] newCuts = new int[cuts.length+2];
Arrays.sort(cuts);
System.arraycopy(cuts, 0, newCuts, 1, cuts.length);
newCuts[cuts.length+1] = n;
Arrays.sort(cuts);
int[][] memo = new int[cuts.length+2][cuts.length+2];
return dp(memo, newCuts, 0, cuts.length+1);
}
public int dp(int[][] memo, int[] cuts, int lIdx, int rIdx){
if (memo[lIdx][rIdx] != 0){
return memo[lIdx][rIdx];
}
int cost = Integer.MAX_VALUE;
int temp = 0;
for (int i=lIdx+1;i<rIdx;i++){
temp = dp(memo, cuts, lIdx, i) + dp(memo, cuts, i, rIdx) + cuts[rIdx] - cuts[lIdx];
if (temp<cost){
cost = temp;
}
}
if (temp == 0) {
cost = 0;
}
memo[lIdx][rIdx] = cost;
System.out.println("l index: "+lIdx+" r index: "+rIdx+" cost: "+cost);
return cost;
}
}
}