First Prefix Reaching a Target Score
Problem statement
You are given an integer array scores and an integer target.
Starting from index 0, add the scores in order. Return the first index i for which scores[0] + scores[1] + ... + scores[i] is greater than or equal to target.
If no prefix reaches the target, return -1.
Function
firstPrefixAtLeastTarget(scores: int[], target: int) → intExamples
Example 1
scores = [2,3,1,4]target = 6return = 2The prefix sums are 2, 5, and 6. Index 2 is the first index that reaches the target.
Example 2
scores = [-2,5,-1]target = 4return = -1The prefix sums are -2, 3, and 2, so none reaches 4.
Constraints
1 <= scores.length <= 105-109 <= scores[i], target <= 109- Every prefix sum fits in a signed 64-bit integer.