646. Buildings With an Ocean View

MediumStackMonotonic StackStack

Buildings stand in a row with the ocean to the right (after the last building). A building has an ocean view if every building to its right is strictly shorter. Return the indices of all buildings with an ocean view, in increasing order. The input is JSON {heights}.

Input: JSON {heights}.

Output: Array — the indices with an ocean view.

Examples

Example 1
Input: {"heights":[4,2,3,1]}
Output: [0,2,3]
Explanation: Buildings 0, 2, 3 see the ocean.
Example 2
Input: {"heights":[4,3,2,1]}
Output: [0,1,2,3]
Explanation: All see the ocean.
Example 3
Input: {"heights":[1,3,2,4]}
Output: [3]
Explanation: Only the tallest last building.

Constraints

Asked by

MetaAmazonGoogle
Solve this problem in the editor →