Jump Game VI
Real-world version: Hopping Along the Row of PadsThe statement here is our own: the same inputs, outputs and technique.
A row of pads holds the scores scores. You begin on the first pad and must finish on the last one. From a pad you may hop forward to any pad at most k places ahead, so long as it is still on the row.
Your total is the sum of the scores of every pad you stand on, counting the first and the last.
Return the largest total possible.
Examples
Example 1
- Input
- scores = [1, -100, 2], k = 2
- Output
- 3
A hop of two places clears the heavy loss in the middle, so only the two ends are stood on.
Example 2
- Input
- scores = [1, -100, 2], k = 1
- Output
- -97
A hop of one place forces every pad to be stood on, the loss included.
Example 3
- Input
- scores = [1, 2, 3, 4, 5], k = 2
- Output
- 15
Every pad scores something, so standing on all of them is best, and hops of one place manage exactly that.
Constraints
- 1 <= scores.length <= 10^5
- 1 <= k <= 10^5
- -10^4 <= scores[i] <= 10^4
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 max_result(scores: list[int], k: int) -> int:public int maxResult(int[] scores, int k)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
More on the same topics
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to LimitReal-world version: Steady Pressure SpanMedium
- Maximum Sum Circular SubarrayReal-world version: Best Arc of the Ring RoadMedium
- Continuous SubarraysReal-world version: Stretches Whose Readings Stay Within TwoMedium
- Shortest Subarray with Sum at Least KReal-world version: Shortest Run Meeting the Grid QuotaHard