Skip to the content

High tides

Problem

In the port of Salina, the buoy by the pier records the water level every hour. The fishers call it a high tide when a reading is higher than the one from the hour before and also higher than the one from the hour after.

Write a function that takes the list of levels (whole numbers, possibly negative) and returns how many high tides there are, as a whole number. The first and the last readings do not have two neighbors, so they never count. Being equal to a neighbor is not enough: it has to be higher than both. An empty list, or one with fewer than three readings, returns 0. With [2, 5, 3, 4, 4, 1, 6, 2]: the 5 is higher than 2 and 3, and the 6 is higher than 1 and 2. The two 4s do not count, because each one equals its neighbor. You return 2.

Examples

  • The example

    [2, 5, 3, 4, 4, 1, 6, 2] → 2

  • A single tide

    [1, 3, 2] → 1

  • It only rises

    [1, 2, 3, 4] → 0

  • High ends do not count

    [5, 1, 5, 1, 5] → 1

  • Below sea level

    [-3, -1, -4, 0, -2] → 2

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def high_tides(levels):
    pass

JavaScript

function highTides(levels) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →