heights[i]. A height h is called **allowed** if every column taller than h has the same height. For instance, with [10, 8, 10, 8] the height 9 is allowed (only the 10s are above it), while 5 is not.
A single cut picks a height h that is allowed for the current heights and shortens every column taller than h to exactly h.
Return the fewest cuts after which every column has height exactly level, or -1 if that cannot happen.heights=[6, 3, 6, 4], level=3 → 2
Cut everything above 4 down to 4, then everything above 3 down to 3: two cuts.
heights=[5, 2, 7], level=4 → -1
The column of height 2 is below level 4 and nothing can be raised, so the answer is -1.
heights=[8, 8], level=1 → 1
heights=[6, 3, 6, 4], level=3
2
Run checks the first 3 tests in your browser. Submit checks all 123 tests on the server, hidden ones included.