Skip to content
BytePatterns

Taller Than Everything After

EasyArrays#running-maximum#linear-scan~15m

Problem

A row of buildings faces the sea at the right end of the row. A building has a clear view when it is strictly taller than every building to its right. Given the heights from left to right, return the heights of the buildings with a clear view, in the same left-to-right order.

Examples

Input:  heights = [16, 17, 4, 3, 5, 2]
Output: [17, 5, 2]
Why:    17 and 5 beat everything after them, and the last
        building always has a view
Input:  heights = [5, 5, 3]
Output: [5, 3]
Why:    the first 5 is only as tall as the second one, not strictly taller
Input:  heights = []
Output: []
Why:    edge case, an empty row has no buildings to report

Hints

0 / 3

Stuck on the idea rather than the code? Array Basics covers it.