Ctrl K

Cut a Block to Make It Climb

Hard
You are given an array of positive integers heights: the heights of points along a route, in order. You want the route to be **strictly increasing**, and you may achieve that by cutting out one non-empty contiguous block (subarray). The remaining elements close up in their original order; an empty route also counts as strictly increasing. How many subarrays can you remove so that heights becomes strictly increasing? The array can be long, so trying every subarray will be too slow.
Example 1
heights=[2, 5, 3, 6] → 8

Eight blocks work: [5], [3], [5, 3], [2, 5], [3, 6], [2, 5, 3], [5, 3, 6] and the whole array. Removing [5], for instance, leaves 2, 3, 6, and removing [3, 6] leaves 2, 5. Cutting out just [2] fails: 5, 3, 6 remains.

Example 2
heights=[4, 4] → 3

You can remove either 4 or both of them; each time what remains (one element or nothing) is strictly increasing.

Example 3
heights=[1, 2, 3] → 6
Constraints
  • 1 ≤ len(heights) ≤ 10⁵
  • 1 ≤ heights[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsK-th Smallest Pair Gap HardBusy Server Pairs HardWidest Fence Span Around the Marked Board HardTwo Equal Teams With Close Totals HardFlowerbed Beauty on a Budget Hard
loading Python…
Input
heights=[2, 5, 3, 6]
Expected
8

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