All problems
1023EasyArrayHash TableLinked ListHeap (Priority Queue)SimulationDoubly-Linked ListOrdered Set

Merging the Smallest Neighbouring Pair Until the Strip Rises

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3507Minimum Pair Removal to Sort Array I

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 strip of readings reads readings. While the strip still falls somewhere, meaning some reading sits below the one before it, do the following once:

  • among all pairs of neighbouring readings, take the pair whose two readings add to the least, and where several tie, take the leftmost of them;
  • replace that pair with a single reading holding their sum.

Return how many times this has to be done before the strip no longer falls anywhere.

Examples

Example 1

Input
readings = [4, 7, 3]
Output
1

The strip falls at the last step. The two neighbouring pairs add to eleven and ten, so the later pair is the one merged, leaving 4 and 10, which never falls.

Example 2

Input
readings = [9, 8, 7, 6, 5]
Output
4

The strip falls at every step. Each round merges the smallest neighbouring pair, and it takes four rounds before a single reading is left, which cannot fall.

Example 3

Input
readings = [-5, -3, -1]
Output
0

The readings already climb, so nothing is merged.

Constraints

  • 1 <= readings.length <= 50
  • -1000 <= readings[i] <= 1000

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 minimum_pair_removal(readings: list[int]) -> int:
Java
public int minimumPairRemoval(int[] readings)
September 7
Apply