【力扣 - 最大子数组和】

题目描述

给你一个整数数组 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(log⁡n) 的时间内求到任意区间内的答案,我们甚至可以修改序列中的值,做一些简单的维护,之后仍然可以在 O(log⁡n) 的时间内求到任意区间内的答案,对于大规模查询的情况下,这种方法的优势便体现了出来。这棵树就是一种神奇的数据结构——线段树。

作者:力扣官方题解
链接:https://leetcode.cn/problems/maximum-subarray/solutions/228009/zui-da-zi-xu-he-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

六月悉茗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值