Created
March 25, 2022 16:22
-
-
Save prrraveen/717c4251b8a42a0fde550715a0ef0d3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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