Skip to content
6
0
:
0
0

Campaign Gain Segment

Easy

Dynamic Programming

In "Campaign Gain Segment", 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,5,-1,6,-3,7]
Output:14

For input nums = [-2,5,-1,6,-3,7], the contiguous subarray [5,-1,6,-3,7] gives the maximum sum 14. Therefore, return 14.

Example 2

Input:nums = [5,-1,-2,9,-1,2]
Output:12

For input nums = [5,-1,-2,9,-1,2], the contiguous subarray [5,-1,-2,9,-1,2] gives the maximum sum 12. Therefore, return 12.

Constraints

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

Test Cases (2)

Test Case 1
Input:
[-2,5,-1,6,-3,7]
Expected Output:
14
Test Case 2
Input:
[5,-1,-2,9,-1,2]
Expected Output:
12