6
0
:0
0
Top Segment Finder
Top Segment Finder
Hard
Dynamic Programming
In "Top Segment Finder", 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,21,-1,22,-3,23]Output:
62For input nums = [-2,21,-1,22,-3,23], the contiguous subarray [21,-1,22,-3,23] gives the maximum sum 62. Therefore, return 62.
Example 2
Input:
nums = [21,-1,-2,25,-1,2]Output:
44For input nums = [21,-1,-2,25,-1,2], the contiguous subarray [21,-1,-2,25,-1,2] gives the maximum sum 44. Therefore, return 44.
Constraints
1 <= nums.length <= 105-104 <= nums[i] <= 104
solution.js
Loading...
Test Cases (2)
Test Case 1
Input:
[-2,21,-1,22,-3,23]Expected Output:
62Test Case 2
Input:
[21,-1,-2,25,-1,2]Expected Output:
44