Jack was trying to go up the hill. He does not have any problem in climbing up or coming down the hill if the slope is consistently either increasing or decreasing. Areas where the slope is constant do not bother him in either situation.
Given a list of heights along his path, find the minimum amount to add or subtract to each offending height to make the slope meet Jack‘s requirements. Heights may be increased or decreased as necessary. The value of a change is absolute. In other words, if a height 10 is increased or decreased to 13 or 7, the change is 3.
The following is an example of an array describing a generally increasing set of heights making a slope: [ 0, 1, 2, 5, 6, 5, 7]. The minimum changes required will result from making the slope increasing along its length. Even though the slope varies, it is always increasing over the subarray [0, 1, 2, 5, 6], so no changes are made along that range. The height at array position 5, value = 5, must be raised to at least 6, making the slope flat, so add 1. Now test against the remaining value, position 6, value = 7. The new height 6 < 7 and the rule holds. The sum of all changes necessary is 1.
Function Description
Complete the climbTheHill function in the editor below. The function must return an integer that denotes the minimum cost required to make the slope increasing or decreasing along its length.
climbTheHill has the following parameters:
slope [slope[0],...,slope(n-1)]: an array of integers representing heights along a path
Sample Input
6 9 8 7 2 3 3
Sample Output
1
Explanation
The required sequence that can be formed is [9, 8, 7, 3, 3, 3] (decreasing). The minimum cost incurred is in changing slope[3]from 2 to 3.
思路:
代码:
1 public static int getMinimumCost(int[] input) { 2 if (input == null || input.length == 0) { 3 return 0; 4 } 5 return Math.min(getMinimumCost(input, false), getMinimumCost(input, true)); 6 } 7 private static int getMinimumCost(int[] input, boolean reverse) { 8 int[] sorted = Arrays.copyOf(input, input.length); 9 Arrays.sort(sorted); 10 if (reverse) { 11 for (int i = 0, j = sorted.length - 1; i < j; i++, j --) { 12 int temp = sorted[i]; 13 sorted[i] = sorted[j]; 14 sorted[j] = temp; 15 } 16 } 17 int[][] dp = new int[input.length][sorted.length]; 18 dp[0][0] = Math.abs(input[0] - sorted[0]); 19 for (int i = 1; i < input.length; i++) { 20 dp[i][0] = dp[i - 1][0] + Math.abs(input[i] - sorted[0]); 21 } 22 for (int j = 1; j < sorted.length; j++) { 23 dp[0][j] = Math.min(dp[0][j - 1], Math.abs(input[0] - sorted[j])); 24 } 25 for (int i = 1; i < input.length; i++) { 26 for (int j = 1; j < sorted.length; j++) { 27 dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j] + Math.abs(input[i] - sorted[j])); 28 } 29 } 30 return dp[input.length - 1][sorted.length - 1]; 31 } 32 33 public static void main(String[] args) { 34 System.out.println(getMinimumCost(new int[]{0,1,2,5,5,4,4})); 35 System.out.println(getMinimumCost(new int[]{7,5,6,5,2,1,0})); 36 System.out.println(getMinimumCost(new int[]{9,8,7,2,3,3})); 37 }
原文地址:https://www.cnblogs.com/liuliu5151/p/11509823.html