題目


There are n buildings in a line. You are given an integer array heights of size n that represents the heights of the buildings in the line.

The ocean is to the right of the buildings. A building has an ocean view if the building can see the ocean without obstructions. Formally, a building has an ocean view if all the buildings to its right have a smaller height.

Return a list of indices (0-indexed) of buildings that have an ocean view, sorted in increasing order.

Example 1:

1Input: heights = [4,2,3,1]
2Output: [0,2,3]
3Explanation: Building 1 (0-indexed) does not have an ocean view because building 2 is taller.

Example 2:

1Input: heights = [4,3,2,1]
2Output: [0,1,2,3]
3Explanation: All the buildings have an ocean view.

Example 3:

1Input: heights = [1,3,2,4]
2Output: [3]
3Explanation: Only building 3 has an ocean view.

Constraints:

$$ \begin{array}{l} 1 <= heights.length <= 10^5 \\ 1 <= heights[i] <= 10^9 \end{array} $$

我的思路


我們現在要看有多少建築物可以看到右邊的海洋,所以當我在第 i 個建築物時,i 建築物右邊的全部建築物都需要比 i 建築物矮。

所以我會從最右開始往左看,用一個變數去記錄當下最高建築物的高度,如果當下的建築物沒有比記錄的建築物高,也就代表可以看到海洋。

由於回傳的結果必須是由左到右的,所以這邊我會先用 Stack 將可看見海洋的建築物存下來,最後在將 Stack 裡面的東西轉換回 Array 返回。

程式碼


 1class Solution {
 2    public int[] findBuildings(int[] heights) {
 3        // 初始化最右邊的建築物
 4        int heightest = heights[heights.length - 1];
 5        Stack<Integer> stack = new Stack();
 6        // 最右邊的放子一定看的到海
 7        stack.push(heights.length - 1);
 8        
 9        for(int i = heights.length - 1; i >= 0; i--) {
10            if (heights[i] > heightest) {
11                stack.push(i);
12                heightest = heights[i];
13            }
14        }
15        
16        int size = stack.size();
17        int[] result = new int[size];
18        for(int i = 0; i < size; i++) {
19            result[i] = stack.pop();
20        }
21        return result;
22    }
23}

Time complexity: O(N)

Space complexity: O(N)