Ctrl K

Most Blocks After Merging

Hard
You get an array values of positive integers. In one move you may pick a run of consecutive elements and replace it with a single number, their sum. For instance, merging [3, 5] in [1, 3, 5, 6] gives [1, 8, 6]. You may make any number of moves, including none. Return the greatest length a non-decreasing array can have after such moves.
Example 1
values=[6, 1, 2, 4] → 2

After the 6 we need a block summing to at least 6: 1 + 2 + 4 = 7 gives [6, 7]. Three blocks are impossible: the first block is at least 6, and the rest only adds up to one more such block.

Example 2
values=[2, 2, 5, 9] → 4

The array is already non-decreasing, so nothing needs merging.

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

For example, [4, 3 + 2, 7 + 1] = [4, 5, 8].

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 problemsWindows You Can Raise Into Order HardWater Between Walls HardBiggest Solid Panel Block HardShortest Stretch Reaching a Total HardFrog on the Posts Hard
loading Python…
Input
values=[6, 1, 2, 4]
Expected
2

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