Ctrl K

Break Into Single Pieces

Medium
You are given an array values and an integer threshold. Call a piece of the array *sturdy* if it has a single element or the sum of its elements is at least threshold. In one step you may take any existing piece of length at least 2 and cut it into two contiguous pieces, but only if both resulting pieces are sturdy. You start with one piece, the whole array. Return True if such steps can reach the state where every element is its own piece, and False otherwise.
Example 1
values=[1, 4, 3, 1], threshold=7 → True

Cut off the last 1: [1, 4, 3] (sum 8) and [1]. Then the first 1: [1] and [4, 3] (sum 7). Finally [4, 3] becomes [4] and [3].

Example 2
values=[3, 1, 3], threshold=5 → False

The first cut leaves either [3, 1] or [1, 3]; that sum of 4 is below 5 and the length is more than 1, so no cut is allowed.

Example 3
values=[9, 2], threshold=200 → True
Constraints
  • 1 ≤ len(values) ≤ 100
  • 1 ≤ values[i] ≤ 100
  • 1 ≤ threshold ≤ 200
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumReach the Last Cell MediumAs Many Trades as You Like MediumLongest Seesaw Subsequence MediumFewest Meetings to Cancel Medium
loading Python…
Input
values=[1, 4, 3, 1], threshold=7
Expected
True

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