Skip to content
6
0
:
0
0

Maximum Subarray

Medium

Dynamic Programming

Given an integer array nums, find the subarray with the largest sum, and return its sum.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1

Input:nums = [-2,1,-3,4,-1,2,1,-5,4]
Output:6

The subarray [4,-1,2,1] has the largest sum 6.

Example 2

Input:nums = [1]
Output:1

The subarray [1] has the largest sum 1.

Constraints

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

Test Cases (2)

Test Case 1
Input:
[-2,1,-3,4,-1,2,1,-5,4]
Expected Output:
6
Test Case 2
Input:
[1]
Expected Output:
1