题目描述
给你一个整数数组 nums
,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
子数组 是数组中的一个连续部分。
示例 1:
输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:6
解释:连续子数组 [4,-1,2,1]
的和最大,为 6
。
示例 2:
输入:nums = [1]
输出:1
示例 3:
输入:nums = [5,4,-1,7,8]
输出:23
提示:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
方法一:动态规划
思路和算法
代码
// Function to find the maximum sum of a contiguous subarray within the given array
int maxSubArray(int* nums, int numsSize) {
int sum = 0; // Variable to store the current sum of the subarray
int maxAns = nums[0]; // Variable to store the maximum sum found so far
// Iterate through the array to calculate the maximum subarray sum
for (int i = 0; i < numsSize; i++) {
// Update the current sum by choosing the maximum between extending the subarray or starting a new subarray
sum = fmax(sum + nums[i], nums[i]);
// Update the maximum sum found so far by comparing with the current sum
maxAns = fmax(maxAns, sum);
}
return maxAns; // Return the maximum sum of a contiguous subarray
}
复杂度
时间复杂度:O(n),其中 n 为 nums 数组的长度。我们只需要遍历一遍数组即可求得答案。
空间复杂度:O(1)。我们只需要常数空间存放若干变量。
方法二:分治
思路和算法
这个分治方法类似于「线段树求解最长公共上升子序列问题」的 pushUp
操作。
代码
// Structure to store the status of a subarray
struct Status {
int lSum; // Maximum sum of the left subarray
int rSum; // Maximum sum of the right subarray
int mSum; // Maximum sum of any subarray within the range
int iSum; // Total sum of the subarray
};
// Function to update the status after merging left and right subarrays
struct Status pushUp(struct Status l, struct Status r) {
int iSum = l.iSum + r.iSum; // Total sum of merged subarrays
int lSum = fmax(l.lSum, l.iSum + r.lSum); // Maximum sum of the left subarray
int rSum = fmax(r.rSum, r.iSum + l.rSum); // Maximum sum of the right subarray
int mSum = fmax(fmax(l.mSum, r.mSum), l.rSum + r.lSum); // Maximum sum of any subarray
return (struct Status){lSum, rSum, mSum, iSum}; // Return the updated status
}
// Function to get the status of a subarray within the range [l, r]
struct Status get(int* a, int l, int r) {
// Base case: when the range contains only one element
if (l == r) {
return (struct Status){a[l], a[l], a[l], a[l]}; // Return the status for a single element
}
// Calculate the middle index of the range
int m = (l + r) >> 1;
// Recursively get the status of the left and right subarrays
struct Status lSub = get(a, l, m);
struct Status rSub = get(a, m + 1, r);
// Merge the status of left and right subarrays
return pushUp(lSub, rSub);
}
// Function to find the maximum sum of any subarray within the given array
int maxSubArray(int* nums, int numsSize) {
// Get the status of the entire array and return the maximum subarray sum
return get(nums, 0, numsSize - 1).mSum;
}
复杂度分析
题外话
「方法二」相较于「方法一」来说,时间复杂度相同,但是因为使用了递归,并且维护了四个信息的结构体,运行的时间略长,空间复杂度也不如方法一优秀,而且难以理解。那么这种方法存在的意义是什么呢?
对于这道题而言,确实是如此的。但是仔细观察「方法二」,它不仅可以解决区间 [0,n−1]
,还可以用于解决任意的子区间 [l,r]
的问题。如果我们把 [0,n−1]
分治下去出现的所有子区间的信息都用堆式存储的方式记忆化下来,即建成一棵真正的树之后,我们就可以在 O(logn)
的时间内求到任意区间内的答案,我们甚至可以修改序列中的值,做一些简单的维护,之后仍然可以在 O(logn)
的时间内求到任意区间内的答案,对于大规模查询的情况下,这种方法的优势便体现了出来。这棵树就是一种神奇的数据结构——线段树。
作者:力扣官方题解
链接:https://leetcode.cn/problems/maximum-subarray/solutions/228009/zui-da-zi-xu-he-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。