solulubeta
Sign in
DSA Basics / Warm-up
01.04 · Problem

The Tallest Building on the Skyline

Easy Single pass · running maximum Free

A drone flies over your city in a straight line and records the height of every building it passes, in order. The news channel only wants one figure for the headline: the height of the tallest building on that route.

Return the largest height in the list.

Examples

Example 1

Input:  heights = [12, 27, 9, 18]
Output: 27

The drone sees 12, then 27, then 9, then 18. The running best goes 12 → 27 → 27 → 27, because nothing after 27 is taller. The answer is 27.

Example 2

Input:  heights = [5]
Output: 5

Example 3

Input:  heights = [100, 100, 100]
Output: 100

Constraints

heights has 1 to 10,000 entries, each between 1 and 1,000,000.

Return the biggest value in the list. With only one building, that building is the tallest.

How to solve it

Hint

Remember the best you have seen so far and replace it whenever you see something taller.

Approach: Single pass · running maximum
  1. Take the first height as the best so far.
  2. Walk the rest of the list; whenever a height beats the best so far, make it the new best.
  3. Return the best.

Check with the example. Starting at 12, then 27 beats it, 9 and 18 do not, so 27 stays.

Complexity: O(n) time, O(1) space.

The reference solutions in all four languages are in the Solutions tab of the workspace below.

Loading the workspace…
Mark this problem as solved when you are finished.

Item 4 of 90 · updated 2026-10-12