[虚拟机OA]Climb the hill 爬山

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

时间: 2024-10-17 02:20:01

[虚拟机OA]Climb the hill 爬山的相关文章

[虚拟机OA]Maximal Square 最大正方形

Given a 2D binary matrix filled with 0's and 1's,find the largest square containing only 1's and return its area. Input: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0 Output: 4 (参考LeetCode 221 Maximal Square) 思路: dp[i][j] 代表在以i, j 这个点为右下角的正方形变成若这一格的值是1, 则这个正方

[虚拟机OA]Group Anagram 变位词归类

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","

[虚拟机OA]Fun with Anagrams 玩转同构词

Two strings are anagrams if they are permutations of each other. For example, "aaagmnrs" is an anagram of "anagrams". Given an array of strings, remove each string that is an anagram of an earlier string, then return the remaining arra

[虚拟机OA]Break a Palindrome 破坏回文串

In this challenge, you will be given a palindrome which you must modify if possible. Change exactly one character of the string to another character in the range ascii[a-z] so that the string meets the following two conditions: • The new string is no

[虚拟机OA]Even Subarray 最多含有K个奇数的子数组

A subarray is a contiguous portion of an array. Given an array of integers, you must determine the number of distinct subarrays that can be formed having at most a given number of odd elements. Two subarrays are distinct if they differ at even one po

[虚拟机OA]Build the Subsequences 生成子序列

A subsequence of a string is obtained by deleting zero or more characters from the string while maintaining order. For example, the subsequences of string s = "xyz", not including the empty string, are "x", "xy", "xz&quo

[虚拟机OA]Team Formation 2 团队构成

FC Codelona is trying to assemble a team from a roster of available players. They have a minimum number of players they want to sign and each player needs to have a skill rating within a certain range. Given a list of players' skill levels with desir

英语语法最终珍藏版笔记- 21it 用法小结

it 用法小结 it 在英语中的意思较多,用法较广,现总结如下. 一.it作句子的真正主语 1.it 指前面已经提到过的人或事物,有时指心目中的或成为问题的人或事物,作真正主语. 例如: What’s this? -It is a sheep? 这是什么??这是一只绵羊. Who is it? -It’s me (I). 谁??是我. It’s the wind shaking the window. 是风刮得窗户响. 2.it指时间.季节.一般用在无人称动词的主语. 例如: What time

MIP启发式算法:爬山算法 (Hill climbing)

本文主要记录和分享学习到的知识,算不上原创. *参考文献见链接. 本文讲述的是求解MIP问题的启发式算法中的爬山算法 (Hill climbing). 目录 前言 Hill climbing 的过程 Hill climbing 的伪代码 前言 爬山算法是以local search为核心框架的启发式算法中最简单的算法,当然,结果一般也不太好,因为爬山算法有一个很大的缺点:不能跳出局部解. 就像我们在local search那篇文中提到,以local search 为框架的启发式算法需要综合考虑到算