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].
[8, 8, 5, 7, 9, 8, 7, 4, 8] [8, 8, 5, 7, 9, 8, 7, 4, 8] [8, 8, 5, 7, 9, 8, 7, 4, 8] [8, 8, 5, 7, 9, 8, 7, 4, 8] [8, 8, 5, 7, 9, 8, 7, 4, 8] [8, 8, 5, 7, 9, 8, 7, 4, 8] [8, 8, 5, 7, 9, 8, 7, 4, 8] [8, 8, 5, 7, 9, 8, 7, 4, 8] [8, 8, 5, 7, 9, 8, 7, 4, 8]
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 8, in solution if stack.empty(): stack.append(h) AttributeError: 'dict' object has no attribute 'empty'
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 8, in solution if len(stack) == 0: stack.append(h) AttributeError: 'dict' object has no attribute 'append'
8 5 7 9 8 7 4 8
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
print("remove from stack, recheck untill ok", h)
elif if h > stack[-1]:
print("remove from stack, recheck untill ok", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
elif h ==
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
return 0
remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 7 add a brick, add to stack 9 remove from stack, recheck untill ok 7 remove from stack, recheck untill ok 4
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
elif:
contine
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
elif:
print("do nothing", h)
return 0
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 70, in main sol = __import__('solution') File "/tmp/solution.py", line 14 elif: ^ SyntaxError: invalid syntax
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
do nothing 8 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 7 add a brick, add to stack 9 do nothing 8 remove from stack, recheck untill ok 7 remove from stack, recheck untill ok 4 do nothing 8
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
while h <
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
while len(stack) == 0 or h < stack[-1]:
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
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 print("remove from stack, recheck untill ok", h) OSError: [Errno 27] File too large Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='ANSI_X3.4-1968'> OSError: [Errno 27] File too largestdout:
do nothing 8 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove from stack, recheck untill ok 5 remove
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
while len(stack) == 0 or h < stack[-1]:
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
while len(stack) == 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
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 stack.pop() IndexError: pop from empty liststdout:
do nothing 8 remove from stack, recheck untill ok 5
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
while len(stack) == 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
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 stack.pop() IndexError: pop from empty liststdout:
do nothing 8 remove from stack, recheck untill ok 5
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
while len(stack) 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
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 len(stack) != 0 or h < stack[-1]: IndexError: list index out of rangestdout:
do nothing 8 remove from stack, recheck untill ok 5
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfBr
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0: stack.append(h)
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
tack.append(h)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
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 tack.append(h) NameError: name 'tack' is not defined
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stack.append(h)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stack.append(h)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
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 14, in solution while len(stack) != 0 or h < stack[-1]: IndexError: list index out of rangestdout:
do nothing 8 remove from stack, recheck untill ok 5
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stack.append(h)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stack.append(h)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
stack[-1] -
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stack.append(h)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
h - stack[-1]
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
else
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
else:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
else:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else: continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
else:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
else:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
else:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
else:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
elif h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
else:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
if h < stack[-1]:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
if len(stack) == 0:
stoneSize = h
else:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
if len(stack) == 0:
stoneSize = h
else:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
else
if len(stack) == 0:
stoneSize = h
else:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
if len(stack) == 0:
stoneSize = h
else:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
print("add a brick, add to stack", h)
else:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 or h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
return 0
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 9, in solution while len(stack) != 0 or h < stack[-1]: IndexError: list index out of range
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 s h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
return 0
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 and h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
return 0
add a brick, add to stack 8 remove from stack, recheck untill ok 5 add a brick, add to stack 5 add a brick, add to stack 7 add a brick, add to stack 9 add a brick, add to stack 8 add a brick, add to stack 7 remove from stack, recheck untill ok 4 add a brick, add to stack 4 add a brick, add to stack 8
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 and h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
add a brick, add to stack 8 remove from stack, recheck untill ok 5 add a brick, add to stack 5 add a brick, add to stack 7 add a brick, add to stack 9 add a brick, add to stack 8 add a brick, add to stack 7 remove from stack, recheck untill ok 4 add a brick, add to stack 4 add a brick, add to stack 8 8
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
for h in H:
while len(stack) != 0 and h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
cur
for h in H:
while len(stack) != 0 and h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < stack[-1]:
stack.pop()
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
currentHeight +
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > stack[-1]:
stoneSize = h - stack[-1]
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
add a brick, add to stack 8 remove from stack, recheck untill ok 5 add a brick, add to stack 5 add a brick, add to stack 7 add a brick, add to stack 9 remove from stack, recheck untill ok 8 add a brick, add to stack 8 remove from stack, recheck untill ok 7 add a brick, add to stack 7 remove from stack, recheck untill ok 4 add a brick, add to stack 4 add a brick, add to stack 8 8
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > currentHeight:
stoneSize = h - stack[-1]
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == stack[-1]:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > currentHeight:
stoneSize = h - stack[-1]
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == currentHeight:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
add a brick, add to stack 8 remove from stack, recheck untill ok 5 add a brick, add to stack 5 add a brick, add to stack 7 add a brick, add to stack 9 remove from stack, recheck untill ok 8 add a brick, add to stack 8 remove from stack, recheck untill ok 7 add a brick, add to stack 7 remove from stack, recheck untill ok 4 add a brick, add to stack 4 add a brick, add to stack 8 8
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > currentHeight:
stoneSize = h - currentHeight
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == currentHeight:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
print("remove from stack, recheck untill ok", h)
if len(stack) == 0:
stoneSize = h
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > currentHeight:
stoneSize = h - currentHeight
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == currentHeight:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
add a brick, add to stack 8 remove from stack, recheck untill ok 5 add a brick, add to stack 5 add a brick, add to stack 7 add a brick, add to stack 9 remove from stack, recheck untill ok 8 add a brick, add to stack 8 remove from stack, recheck untill ok 7 remove from stack, recheck untill ok 4 remove from stack, recheck untill ok 4 add a brick, add to stack 4 add a brick, add to stack 8 7
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
if len(stack) == 0:
stoneSize = h
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > currentHeight:
stoneSize = h - currentHeight
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h == currentHeight:
continue
print("do nothing", h)
print(numberOfStones)
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
if len(stack) == 0:
stoneSize = h
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
print("add a brick, add to stack", h)
elif h > currentHeight:
stoneSize = h - currentHeight
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h == currentHeight:
continue
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
if len(stack) == 0:
stoneSize = h
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h > currentHeight:
stoneSize = h - currentHeight
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h == currentHeight:
continue
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
if len(stack) == 0:
stoneSize = h
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h > currentHeight:
stoneSize = h - currentHeight
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h == currentHeight:
continue
return numberOfStones
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(H):
stack = []
numberOfStones = 0
currentHeight = 0
for h in H:
while len(stack) != 0 and h < currentHeight:
stone = stack.pop()
currentHeight = currentHeight - stone
if len(stack) == 0:
stoneSize = h
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h > currentHeight:
stoneSize = h - currentHeight
currentHeight = h
stack.append(stoneSize)
numberOfStones = numberOfStones +1
elif h == currentHeight:
continue
return numberOfStones
The solution obtained perfect score.