Skip to content

Instantly share code, notes, and snippets.

@prrraveen
Created March 25, 2022 16:22
Show Gist options
  • Save prrraveen/717c4251b8a42a0fde550715a0ef0d3e to your computer and use it in GitHub Desktop.
Save prrraveen/717c4251b8a42a0fde550715a0ef0d3e to your computer and use it in GitHub Desktop.
import collections
def solution(H):
result = 0
queue = collections.deque()
queue.append(H)
# print(F"queue = {queue}")
while queue:
# print(queue)
wall = queue.popleft()
if 0 in wall:
split_idx = wall.index(0)
if wall[:split_idx]:
queue.append(wall[:split_idx])
if wall[split_idx+1:]:
queue.append(wall[split_idx+1:])
continue
min_h = min(wall)
result += 1
wall = list(map(lambda x: x - min_h, wall))
# print(F"wall = {wall}")
queue.append(wall)
return result
xs = [8, 8, 5, 7, 9, 8, 7, 4, 8]
# xs = [8, 8, 5, 7, 9, 0, 7, 4, 8]
print(F"solution(xs) = {solution(xs)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment