6
0
:0
0
Cost Benefit Segment
Cost Benefit Segment
Hard
Dynamic Programming
In "Cost Benefit 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,16,-1,17,-3,18]Output:
47For input nums = [-2,16,-1,17,-3,18], the contiguous subarray [16,-1,17,-3,18] gives the maximum sum 47. Therefore, return 47.
Example 2
Input:
nums = [16,-1,-2,20,-1,2]Output:
34For input nums = [16,-1,-2,20,-1,2], the contiguous subarray [16,-1,-2,20,-1,2] gives the maximum sum 34. Therefore, return 34.
Constraints
1 <= nums.length <= 105-104 <= nums[i] <= 104
solution.js
Loading...
Test Cases (2)
Test Case 1
Input:
[-2,16,-1,17,-3,18]Expected Output:
47Test Case 2
Input:
[16,-1,-2,20,-1,2]Expected Output:
34