Solution to Hills

Question Description: An integer array store the heights of some consecutive hills. Find a minimum integer N, so that you could increase/decrease the heights of the hills by any integer in range [0, N]. After the adjustment, the heights of these hills should be in a strickly increasing order (PS: [3, 3, 4] is NOT strictly increasing order).

Example of Input and Output

1

2

3

4

5

6

7

8

9

Input:

[5,4,3,2,8]

Output:

3

Explanation:after the adjustment,the hills COULD be:

2,3,4,5,8

There may exist many more accepting finalstatus.We only consider its feasibility.

Firstly, I solve it in binary search method with O(NlogN). But after double think, a dynamic programming solution with O(N) is possible.

Solution to Hills

Python

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

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

#----------------------------------------------------------------

# Author: Sheng Yu codesays.com

# Written: 09/07/2014

# Last updated: 09/07/2014

#

# Filename: hill.py

# Python Used: 2.7

#-----------------------------------------------------------------

importrandom

def_tryHill_bs(v,h):

''' Every hill in v can increase/decrease at most h. Check whether we can

get a strictly increasing result, after adjusting the heights.

'''

preHeight=v[0]-h

forheight inv[1:]:

ifheight+h<=preHeight:returnFalse

else:preHeight=max(preHeight+1,height-h)

returnTrue

defhill_bs(v):

''' Use binary search algorithm to find the minimum change range h, which

Migration Notice

The current hosting service will be closed on September 30th. I am finding another free hosting service and migrating my blog. There might be some interruptions. Please be patient 🙂

Update on 2017/09/26: the service has been migrated to another hosting server. Please leave me a message if you met any error. Thanks!

Update on 2017/11/12: the previous service is quite unstable. I am migrating to another server. Thanks!

Admin 2017/09/20

Guideline for Comments

tl;dr: Please put your code into a <pre>YOUR CODE</pre> section.

Hello everyone!

If you want to ask a question about the solution. DO READ the post and comments firstly.

If you had some troubles in debugging your solution, please try to ask for help on StackOverflow, instead of here.

If you want to post some comments with code or symbol, here is the guidline.

1. To post your code, please add the code inside a <pre> </pre> section (preferred), or <code> </code>. And inside the pre or code section, you do not need to escape < > and &, e.g. no need to use &lt; instead of <.

2. To use special symbols < and > outside the pre block, please use "&lt;" and "&gt;" instead.

3. If you have a comment with lots of < and >, you could add the major part of your comment into a <pre class="decode:true crayon-inline "> YOUR COMMENTS </pre> section.

Finally, if you are posting the first comment here, it usually needs moderation. Please be patient and stay tuned. Thanks!

CodeSays.com Admin

Recent Comments

Sheng May 21, 2018 at 9:00 am on Solution to Passing-Cars by codilityNo. The if-closure ends with a "return" statement. No matter it's included within the loop or after, it will be executed at most once. In...

Sheng April 26, 2018 at 11:43 pm on Solution to Genomic-Range-Query by codilityThe "detected time complexity" is not always accurate. Because ""'A' in sub_S" is an O(N) operation, I would take your solution as O(N*M).