Solution: Here I give a O(N^2) solution by using DP. The key data structure we need is dp[i] which records the minimum cost to covert a subarray a[0, i] into sorted array and the last element of the sorted array should be a[i]. For the example array a[] = {5,4,7,3}, dp[0] = 0, dp[1] = 1 (since we need to decrease "5" by 1), dp[2] = 1, dp[3] = 7. After we had filled up dp[], we need one more data structure: aggr[]. aggr[i] = a[0] + a[1] + ... + a[i]. Then the minmum cost to convert the whole array cost_min = min{dp[i] + aggr[N-1] - aggr[i]}. Here "aggr[N-1] - aggr[i]" means the cost that we delete all the elements with a subscript larger than i.
Then the key is to calculate dp[i]. There are several observations here:
- if a[i] >= a[j] (j<i), we can construct a sorted array ended at a[i] by deleting a[j+1], a[j+2], ... , a[i-1] and then append a[i]. The cost of the resulting sorted array is dp[j] + the cost of deleting a[j+1], a[j+2], ... , a[i-1].
- if a[i] < a[j], to have a[i] as the last element, we need to decrease a[j] by a[j] - a[i]. Moreover, we may not stop at a[j] and we need to keep look backward until we found a a[m] such that a[m] <= a[i], then the cost of the resulting sorted array is dp[m+1] + ∑(a[k] - a[i]) where m<k<j.
- To compute dp[i], we need to look at all the j such that j<i, depending on a[j] and a[i], we need to calculate the cost based on step 1 or 2. Then the minmum cost we encounter is the value of dp[i].
int min_convert_cost(int a[], int n) { int dp[n]; int aggr[n]; aggr[0] = a[0]; for(int i=1; i<n; i++) aggr[i] = aggr[i-1] + a[i]; dp[0] = 0; for(int i=1; i<n; i++) { dp[i] = INT_MAX; for(int j=i-1; j>=0; j--) { int cost_i = 0; if(a[i] >= a[j]) { cost_i = dp[j] + aggr[i-1] - aggr[j]; } else { cost_i += aggr[i-1] - aggr[j]; while(a[j]>a[i] && j >=0) { cost_i += a[j]-a[i]; j--; } cost_i += dp[j+1]; } if(cost_i < dp[i]) dp[i] = cost_i; } } int min = INT_MAX; for(int i=0; i<n; i++) { int cost_i = dp[i] + aggr[n-1] - aggr[i]; if(cost_i<min) min = cost_i; } return min; }
This line should be cost_i += dp[j]; instead of cost_i += dp[j+1];
ReplyDeletebecause we are already adding cost i.e a[k]-a[i] for all m a[j] where j < i
The key observations are:
ReplyDelete1. You will never decrement (reduce the value of) a number at the end of the array. i.e. since a non-decreasing order is desired, we always want a large number at the end of the array, and it never makes sense to reduce it further.
2. One will only reduce intermediate elements (this follows from the previous observation)
3. There will never be a subset (or substring) of elements removed from the middle of the array. i.e. One will never encounter a case when some elements from both ends of the array are retained, but a subset (or substring) of the elements are completely eliminated. For example, consider: 1, 2, 3, 7, 7, 4, 5, 6. Here, we'll reduce both the 7's to 4's to get: 1, 2, 3, 4, 4, 4, 5, 6, but we won't ever eliminate the 7's.
4. One will only ever delete elements from the end of the array. This is because retaining elements at the end of the array may cause massive reductions to all elements in the array. i.e. the only decision we need to make is to decide which suffix of elements to delete from the array, and that will dictate how the subtractions will dictate the rest of the elements in the array.
So, for every suffix, we delete it and then adjust the rest of the elements to be sorted starting from the last element. This can be trivially accomplished using an O(n^2) algorithm.