You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant; however, it should have different heights in different places. The height of the wall is specified by an array H of N positive integers. H[I] is the height of the wall from I to I+1 meters to the right of its left end. In particular, H[0] is the height of the wall's left end and H[N−1] is the height of the wall's right end.
The wall should be built of cuboid stone blocks (that is, all sides of such blocks are rectangular). Your task is to compute the minimum number of blocks needed to build the wall.
Write a function:
def solution(H)
that, given an array H of N positive integers specifying the height of the wall, returns the minimum number of blocks needed to build it.
For example, given array H containing N = 9 integers:
H[0] = 8 H[1] = 8 H[2] = 5 H[3] = 7 H[4] = 9 H[5] = 8 H[6] = 7 H[7] = 4 H[8] = 8the function should return 7. The figure shows one possible arrangement of seven blocks.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array H is an integer within the range [1..1,000,000,000].
Invalid result type, int expected, <class 'NoneType'> found.stdout:
8 8 5 7 9 8 7 4 8
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( H ) File "/tmp/solution.py", line 10, in solution while stack[-1] > hei: IndexError: list index out of range
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( H ) File "/tmp/solution.py", line 11, in solution while stack[-1] > hei: IndexError: list index out of rangestdout:
8 8
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( H ) File "/tmp/solution.py", line 11, in solution while stack[-1] > hei: IndexError: list index out of rangestdout:
stack ::: 8 stack ::: 8
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( H ) File "/tmp/solution.py", line 12, in solution while stack[-1] > hei: IndexError: list index out of rangestdout:
8 stack ::: 8 5 stack ::: 8
Invalid result type, int expected, <class 'NoneType'> found.stdout:
8 stack ::: 8 5 stack ::: 8 7 stack ::: 5 9 stack ::: 5 8 stack ::: 5 7 stack ::: 5 4 stack ::: 5 8 stack ::: 4
Invalid result type, int expected, <class 'NoneType'> found.
Invalid result type, int expected, <class 'NoneType'> found.
The solution obtained perfect score.