Number of Ways to Split Array
Real-world version: Cuts Where the Front Holds Its OwnThe statement here is our own: the same inputs, outputs and technique.
A strip of readings reads readings. A cut falls between two neighbouring readings, so there is one fewer cut than there are readings, and every cut leaves a front piece and a back piece, neither of them empty.
A cut is sound when the front piece's total is at least the back piece's.
Return how many cuts are sound.
Examples
Example 1
- Input
- readings = [5, -5, 5, -5]
- Output
- 3
The whole strip totals nothing. After the first reading the front holds five and the back minus five, after the second both hold nothing, and after the third the front holds five again, so all three cuts are sound.
Example 2
- Input
- readings = [1, 6]
- Output
- 0
The only cut leaves a front of one against a back of six, so it is not sound.
Example 3
- Input
- readings = [3, 3, 3, 3]
- Output
- 2
The first cut leaves three against nine and fails. The second leaves six against six and passes, and the third leaves nine against three and passes too.
Constraints
- 2 <= readings.length <= 10^5
- -10^5 <= readings[i] <= 10^5
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def ways_to_split_array(readings: list[int]) -> int:public int waysToSplitArray(int[] readings)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's