Meta, LeetCode
1762. Buildings With an Ocean View
# Wrong
return result.sort() # .sort() returns None
# Correct: sort() is O(nlogn)
result.sort()
return result
# Better: remove sort(), use [::-1] to reverse list, O(n)
return result[::-1]
from typing import List
class Solution:
def findBuildings(self, heights: List[int]) -> List[int]:
max_heights = heights[-1]
result = [len(heights) - 1]
for i in range(len(heights) - 2, -1, -1):
if heights[i] > max_heights:
result.append(i)
max_heights = max(max_heights, heights[i])
return result[::-1]
Enjoy Reading This Article?
Here are some more articles you might like to read next: