values and an integer goal. A segment is a non-empty run of consecutive elements of the array.
Pick as many segments as you can so that each one sums to exactly goal and no two of them share an element. Return how many segments you picked.
Elements that end up in no segment are simply left out.values=[2, 4, -1, 3, 3, 1, 5], goal=6 → 3
Segments summing to 6 include [2, 4], [4, -1, 3], [3, 3] and [1, 5]. Without overlaps we can take [2, 4], [3, 3] and [1, 5]: three segments.
values=[3, 3, 3, 3, 3], goal=9 → 1
Any three neighbouring 3s add up to 9, but with only five elements just one such segment fits without overlapping.
values=[0, 5, 0], goal=0 → 2
Each zero on its own is a segment with sum 0.
values=[2, 4, -1, 3, 3, 1, 5], goal=6
3
Run checks the first 3 tests in your browser. Submit checks all 94 tests on the server, hidden ones included.