weights. In one move you take off an end box, either the leftmost or the rightmost one. You need to take off boxes whose total weight is exactly target.
Return the fewest moves that achieve this, or -1 if a total of exactly target cannot be reached.weights=[3, 1, 7, 2, 2], target=6 → 3
Taking 3 and 1 from the left and 2 from the right gives 6 in three moves; fewer moves cannot reach exactly 6.
weights=[4, 9, 4], target=5 → -1
From the ends you can reach 4, 8, 13 or 17, but never 5.
weights=[2, 5, 1], target=8 → 3
weights=[3, 1, 7, 2, 2], target=6
3
Run checks the first 3 tests in your browser. Submit checks all 106 tests on the server, hidden ones included.