All problems
0306MediumArrayGreedyLongest Increasing Subsequence

Three Rising Gauge Marks

Tracked in this browser only
Write code

Trains the technique from

LeetCode 334Increasing Triplet Subsequence

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A river gauge writes one reading per hour into a log. readings[t] is the water level at hour t, measured against the datum mark, so it may be negative when the level sits below the mark.

The flood office wants to know whether the log contains a rising trio: three hours i < j < k with readings[i] < readings[j] < readings[k]. The three hours do not have to be next to each other, but they must appear in that order in the log, and each step must be a strict increase.

Return true if the log contains a rising trio and false otherwise. Your solution should run in O(n) time and use O(1) extra space.

Examples

Example 1

Input
readings = [9, 4, -2, 5, 1, 7]
Output
true

Hours 2, 3 and 5 hold -2, 5 and 7. Those hours are in increasing order and -2 < 5 < 7, so the log has a rising trio.

Example 2

Input
readings = [8, 8, 3, 3, -1, -1]
Output
false

No reading is ever followed later by a larger one, so not even a rising pair exists, let alone a trio.

Example 3

Input
readings = [6, 1, 7, 2, 8]
Output
true

Hours 1, 3 and 4 hold 1, 2 and 8, and 1 < 2 < 8.

Example 4

Input
readings = [4, 5, 1, 2]
Output
false

The rising pairs available are (4, 5) and (1, 2), and neither has a third larger reading after it, so there is no trio.

Example 5

Input
readings = [5, 5, 5, 5]
Output
false

Every reading equals the next, and equal readings are not a strict increase.

Constraints

  • 1 <= readings.length <= 5 * 10^5
  • -2^31 <= readings[i] <= 2^31 - 1

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def has_rising_trio(readings: list[int]) -> bool:
Java
public boolean hasRisingTrio(int[] readings)
September 7
Apply