2023-12-09 18:42:21 +08:00
< p > Given an integer array < code > nums< / code > , find the < span data-keyword = "subarray-nonempty" > subarray< / span > with the largest sum, and return < em > its sum< / em > .< / p >
2022-03-27 20:56:26 +08:00
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:56:26 +08:00
< pre >
< strong > Input:< / strong > nums = [-2,1,-3,4,-1,2,1,-5,4]
< strong > Output:< / strong > 6
2023-12-09 18:42:21 +08:00
< strong > Explanation:< / strong > The subarray [4,-1,2,1] has the largest sum 6.
2022-03-27 20:56:26 +08:00
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:56:26 +08:00
< pre >
< strong > Input:< / strong > nums = [1]
< strong > Output:< / strong > 1
2023-12-09 18:42:21 +08:00
< strong > Explanation:< / strong > The subarray [1] has the largest sum 1.
2022-03-27 20:56:26 +08:00
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 3:< / strong > < / p >
2022-03-27 20:56:26 +08:00
< pre >
< strong > Input:< / strong > nums = [5,4,-1,7,8]
< strong > Output:< / strong > 23
2023-12-09 18:42:21 +08:00
< strong > Explanation:< / strong > The subarray [5,4,-1,7,8] has the largest sum 23.
2022-03-27 20:56:26 +08:00
< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 1 < = nums.length < = 10< sup > 5< / sup > < / code > < / li >
< li > < code > -10< sup > 4< / sup > < = nums[i] < = 10< sup > 4< / sup > < / code > < / li >
< / ul >
< p > < / p >
< p > < strong > Follow up:< / strong > If you have figured out the < code > O(n)< / code > solution, try coding another solution using the < strong > divide and conquer< / strong > approach, which is more subtle.< / p >