Skip to content
6
0
:
0
0

Traffic Spike Span

Medium

Dynamic Programming

In "Traffic Spike Span", each integer represents gain or loss at each time step.

Find the maximum possible sum of a contiguous non-empty segment.

Example 1

Input:nums = [-2,19,-1,20,-3,21]
Output:56

For input nums = [-2,19,-1,20,-3,21], the contiguous subarray [19,-1,20,-3,21] gives the maximum sum 56. Therefore, return 56.

Example 2

Input:nums = [19,-1,-2,23,-1,2]
Output:40

For input nums = [19,-1,-2,23,-1,2], the contiguous subarray [19,-1,-2,23,-1,2] gives the maximum sum 40. Therefore, return 40.

Constraints

  • 1 <= nums.length <= 105
  • -104 <= nums[i] <= 104
solution.js
Loading...

Test Cases (2)

Test Case 1
Input:
[-2,19,-1,20,-3,21]
Expected Output:
56
Test Case 2
Input:
[19,-1,-2,23,-1,2]
Expected Output:
40