Skip to content

Commit ae451cc

Browse files
committed
min cost to convert sorted array
1 parent cf658c2 commit ae451cc

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import java.util.Arrays;
2+
3+
/**
4+
* You are given an array of positive integers.
5+
* Convert it to a sorted array with minimum cost. Only valid operation are
6+
* <p/>
7+
* 1) Decrement -> cost = 1
8+
* 2) Delete an element completely from the array -> cost = value of element
9+
* <p/>
10+
* <p/>
11+
* For example:
12+
* 4,3,5,6, -> cost 1
13+
* 10,3,11,12 -> cost 3
14+
* <p/>
15+
* idea:
16+
* In each case, you have 2 choices. The first is to decrement
17+
* elements to the left by the amount needed to restore non-decreasing
18+
* order. The second is to delete the new element. The cost of each is
19+
* easy to calculate. Pick the choice with least cost and continue.
20+
* This algorithm is O(n^2).
21+
* <p/>
22+
* Remember C(n, m) is the cost of making a[1 .. n] into a non-decreasing
23+
* sequence with the last element being no more than m. And we always
24+
* draw m from the set V of values in a.
25+
* So here is the new DP:
26+
* C(1, m) = max(a[1] - m, 0) // first row only decrement is possible
27+
* C(n, m) = min (
28+
* a[n] + C(n - 1, m), // delete
29+
* (a[n] <= m) ? C(n - 1, a[n]) : C(n - 1, m) + a[n] - m // decrement
30+
* )
31+
*
32+
* Here is an example. Suppose we have a = [5, 1, 1, 1, 3, 1]. The
33+
least cost here is obtained by decrementing the 5 to 1 (cost 4) and
34+
deleting the final 1 (cost 1) for a total cost of 5.
35+
So let's try the algorithm. (You must view this with a fixed font.)
36+
Table of C(n, m) values:
37+
m = 1 3 5
38+
n = 1 : 4 2 0
39+
n = 2 : 4 3* 1*
40+
n = 3 : 4 4 2*
41+
n = 4 : 4 4 3*
42+
n = 5 : 6m 4 4
43+
n = 6 : 6 5* 5*
44+
Here * means C resulted from decrementing and "m" means that a
45+
decrement was based on the value of m rather than a[n].
46+
We take the answer from C(6,5) = 5.
47+
48+
* <p/>
49+
* Now the solution becomes easy to understand: it is a DP in two dimensions.
50+
* If we sort the elements of the distinct elements of the original sequence d into a sorted array s,
51+
* the length of d becomes the first dimension of the DP; the length of s becomes the second dimension.
52+
* <p/>
53+
* We declare dp[d.Length,s.Length]. The value of dp[i,j] is the cost of solving subproblem d[0 to i]
54+
* while keeping the last element of the solution under s[j].
55+
* Note: this cost includes the cost of eliminating d[i] if it is less than s[j].
56+
* <p/>
57+
* The first row dp[0,j] is computed as the cost of trimming d[0] to s[j],
58+
* or zero if d[0] < s[j]. The value of dp[i,j] next row is calculated
59+
* as the minimum of dp[i-1, 0 to j] + trim,
60+
* where trim is the cost of trimming d[i] to s[j],
61+
* or d[i] if it needs to be eliminated because s[j] is bigger than d[i].
62+
*/
63+
public class MinCostSortedArray {
64+
public static void main(String[] args) {
65+
int[] a1 = new int[]{4, 3, 5, 6};
66+
int[] a2 = new int[]{10, 3, 11, 12};
67+
int[] a3 = new int[]{1, 10, 2, 11, 12};
68+
int[] a4 = new int[]{5, 1, 1, 1, 3, 1};
69+
70+
minCostNonDecreasingArray(a1);
71+
minCostNonDecreasingArray(a2);
72+
minCostNonDecreasingArray(a3);
73+
minCostNonDecreasingArray(a4);
74+
75+
}
76+
77+
public static void minCostNonDecreasingArray(int[] a) {
78+
79+
System.out.println(Arrays.toString(a));
80+
if (a.length <= 1) {
81+
System.out.println("min cost: " + 0);
82+
return;
83+
}
84+
85+
int[] sorted = a.clone();
86+
Arrays.sort(sorted);
87+
88+
int[][] cost = new int[a.length][sorted.length];
89+
90+
int[] index = new int[a.length];
91+
for(int i =0; i < a.length; i++){
92+
for( int k =0 ; k < sorted.length; k++){
93+
if(sorted[k] == a[i])
94+
index[i] = k;
95+
}
96+
}
97+
98+
for (int j = 0; j < sorted.length; j++) {
99+
cost[0][j] = (sorted[j] < a[0]) ? (a[0] - sorted[j]) : 0; // to a[0]
100+
}
101+
102+
for (int i = 1; i < a.length; i++) {
103+
for (int j = 0; j < sorted.length; j++) {
104+
105+
int del_cost = cost[i-1][j] + a[i];
106+
int decr_cost = (a[i] > sorted[j]) ? (cost[i - 1][j] + (a[i] - sorted[j])) : cost[i-1][index[i]];
107+
108+
cost[i][j] = Math.min(del_cost, decr_cost);
109+
110+
}
111+
112+
}
113+
114+
int min = Integer.MAX_VALUE;
115+
for (int j = 0; j < cost[0].length; j++) {
116+
min = Math.min(min, cost[a.length - 1][j]);
117+
}
118+
// System.out.println(Arrays.deepToString(cost));
119+
System.out.println("min cost: " + min);
120+
}
121+
}

0 commit comments

Comments
 (0)