Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created October 11, 2021 16:12
Show Gist options
  • Save codecakes/873fd28cac74d1428fdc32fc64cfb97b to your computer and use it in GitHub Desktop.
Save codecakes/873fd28cac74d1428fdc32fc64cfb97b to your computer and use it in GitHub Desktop.
Stack. Return an array in descending order with previous integer greater than current
"""https://www.algoexpert.io/questions/Sunset%20Views
Return an array in descending order given an array of positive non-zero integers either starting from left or right
"""
import functools
def sunsetViews(buildings, direction):
n = len(buildings)
def get_index(direction, n, idx):
return idx if direction == "EAST" else n-1-idx
get_index = functools.partial(get_index, direction, n)
stack = []
if not n: return stack
iter_buildings = buildings
if direction == "WEST":
iter_buildings = reversed(buildings)
# print(buildings)
for idx, num in enumerate(iter_buildings):
# print(stack, idx, num)
while stack and num >= buildings[stack[-1]]:
stack.pop()
stack += [get_index(idx)]
if direction == "WEST":
return list(reversed(stack))
return stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment