Ctrl K

Clearing in Increasing Sweeps

Hard
A sorting robot clears the array values in sweeps. In one sweep it moves left to right and picks up any set of numbers that are **strictly increasing** in the order they appear (they do not have to be adjacent). The remaining numbers keep their order. Return the smallest number of sweeps that leaves the array empty.
Example 1
values=[4, 6, 2, 5, 1, 3] → 3

Three sweeps: [4, 6], [2, 5], [1, 3]. Two are not enough: 6, 5 and 3 appear in decreasing order, so no two of them can share a sweep.

Example 2
values=[7, 7, 7] → 3

The increase must be strict, so equal numbers always go in different sweeps.

Example 3
values=[2, 3, 9] → 1

The array already increases strictly, so one sweep is enough.

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMedian Across Two Arrays HardMinimum of a Shifted Array With Repeats HardFrame Around an Ink Blot HardSmaller Values to the Right HardWindow Sums Within a Band Hard
loading Python…
Input
values=[4, 6, 2, 5, 1, 3]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 122 tests on the server, hidden ones included.