Problem : https://leetcode.com/problems/buildings-with-an-ocean-view/
Maintain a decreasing monotonic stack.
class Solution:
def findBuildings(self, heights: List[int]) -> List[int]:
stack = []
for i, h in enumerate(heights):
while stack and heights[stack[-1]] <= h:
stack.pop()
stack.append(i)
return stack
No comments:
Post a Comment