Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Going from right to left
- """
- class Solution:
- def findBuildings(self, heights: List[int]) -> List[int]:
- # tranverse from right to left
- index = len(heights)-1
- greatest_till_now = heights[-1]
- can_view = [len(heights)-1]
- for index in range(len(heights)-2, -1, -1):
- if heights[index] > greatest_till_now:
- greatest_till_now = heights[index]
- can_view.append(index)
- return can_view[::-1]
- """
- Going from left to right. Pop all previous buildings from the stack
- if they are shorter or equal to the current one (because they can no longer see the ocean).
- Push the current building’s index onto the stack.
- """
- class Solution:
- def findBuildings(self, heights: List[int]) -> List[int]:
- stack = []
- for i in range(len(heights)):
- # Pop all previous buildings from the stack
- # if they are shorter or equal to the current one
- while stack and heights[stack[-1]] <= heights[i]:
- stack.pop()
- stack.append(i)
- return stack
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement