Skip to content

Latest commit

 

History

History
21 lines (19 loc) · 572 Bytes

Buildings With an Ocean View.md

File metadata and controls

21 lines (19 loc) · 572 Bytes

Screen Shot 2022-01-22 at 23 15 22

O(n) stack solution

/**
 * @param {number[]} heights
 * @return {number[]}
 */
var findBuildings = function(heights) {
    let stack = [];
    for(let i = 0; i < heights.length; i++) {
        // if current building's length
        while(stack.length && heights[i] >= heights[stack[stack.length - 1]]) {
            stack.pop();
        }
        stack.push(i);
    }
    return stack;
};