leetcode 1762
时间: 2023-08-28 21:06:34 浏览: 53
LeetCode problem 1762 is called "Buildings With an Ocean View". The problem statement asks you to find the indices of the buildings that have an ocean view, given an array representing the heights of the buildings. You need to return the indices in ascending order.
To solve this problem, you can use a simple iteration over the array from right to left. Initialize a variable to keep track of the maximum height encountered so far, and iterate from the last building to the first. If the current building height is greater than the maximum height, add its index to the result array and update the maximum height.
Here's an example solution in Python:
```
def findBuildings(heights):
n = len(heights)
max_height = 0
result = []
for i in range(n - 1, -1, -1):
if heights[i] > max_height:
result.append(i)
max_height = heights[i]
return result[::-1] # Reverse the result to obtain ascending order
# Example usage
heights = [4, 2, 3, 1]
print(findBuildings(heights)) # Output: [0, 2, 3]
```
This solution has a time complexity of O(n), where n is the number of buildings.