53. Maximum Subarray

本文介绍了一种寻找数组中最大子数组和的算法,通过动态规划和分而治之两种方法实现。动态规划方法利用子问题求解当前问题,分而治之方法则通过递归分解问题,并结合中间结果得出最终答案。
Find the contiguous subarray within an array (containing at least one number) which has the largest sum. 

For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.


这是一道典型动态规划问题。其核心在于找到子问题(So when it comes to DP, the first thing for us to figure out is the format of the sub problem(or the state of each sub problem). The format of the sub problem can be helpful when we are trying to come up with the recursive relation.)

我们从第一个元素开始向后退,在第i个元素时,想要求的是必须包含这个元素且这个元素作为最后一个元素的子数组的最大值maxSubArray(int A[], int i),并且已经解得必须包含其之前一个元素的子数组的最大值maxSubArray(int A[], int i-1),那么maxSubArray(A, i) = maxSubArray(A, i - 1) > 0 ? maxSubArray(A, i - 1) : 0 + A[i]; 再用maxSubArray(A, i)和之前的max进行比较,较大者就是新的max。

public int maxSubArray(int[] A) {
        int n = A.length;
        int[] dp = new int[n];//dp[i] means the maximum subarray ending with A[i];
        dp[0] = A[0];
        int max = dp[0];
        
        for(int i = 1; i < n; i++){
            dp[i] = A[i] + (dp[i - 1] > 0 ? dp[i - 1] : 0);
            max = Math.max(max, dp[i]);
        }
        
        return max;
}


分而治之算法:

Step1. Select the middle element of the array.
So the maximum subarray may contain that middle element or not.

Step 2.1 If the maximum subarray does not contain the middle element, then we can apply the same algorithm to the the subarray to the left of the middle element and the subarray to the right of the middle element.

Step 2.2 If the maximum subarray does contain the middle element, then the result will be simply the maximum suffix subarray of the left subarray plus the maximum prefix subarray of the right subarray

Step 3 return the maximum of those three answer.

class Solution {
public:
    int maxSubArray(int A[], int n) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        if(n==0) return 0;
        return maxSubArrayHelperFunction(A,0,n-1);
    }
    
    int maxSubArrayHelperFunction(int A[], int left, int right) {
        if(right == left) return A[left];
        int middle = (left+right)/2;
        int leftans = maxSubArrayHelperFunction(A, left, middle);
        int rightans = maxSubArrayHelperFunction(A, middle+1, right);
        int leftmax = A[middle];
        int rightmax = A[middle+1];
        int temp = 0;
        for(int i=middle;i>=left;i--) {
            temp += A[i];
            if(temp > leftmax) leftmax = temp;
        }
        temp = 0;
        for(int i=middle+1;i<=right;i++) {
            temp += A[i];
            if(temp > rightmax) rightmax = temp;
        }
        return max(max(leftans, rightans),leftmax+rightmax);
    }
};


### 最大子数组和算法实现与解释 最大子数组和问题旨在从一个整数数组中找出具有最大和的连续子数组。该问题的经典解法采用动态规划思想,其核心逻辑在于逐个位置计算以当前元素结尾的最大子数组和,并记录全局最大值。 动态规划状态转移方程如下: `maxSubArray(A, i) = maxSubArray(A, i - 1) > 0 ? maxSubArray(A, i - 1) : 0 + A[i]` 该公式表示如果前一个位置的最大子数组和大于零,则将其加入当前位置的数值形成新的候选值;否则仅保留当前位置的数值作为起始点。通过这种方式,可以在线性时间内完成整个数组的遍历并求出最大子数组和[^1]。 #### Java 实现示例 ```java class Solution { public int maxSubArray(int[] A) { int n = A.length; int[] dp = new int[n]; // dp[i] 表示以 A[i] 结尾的最大子数组和 dp[0] = A[0]; int max = dp[0]; for (int i = 1; i < n; i++) { dp[i] = A[i] + (dp[i - 1] > 0 ? dp[i - 1] : 0); max = Math.max(max, dp[i]); } return max; } } ``` 上述代码使用了一个长度为 `n` 的数组 `dp` 来存储每个位置上的最大子数组和,最终返回全局最大值 `max`。该方法的时间复杂度为 O(n),空间复杂度也为 O(n)。 为了进一步优化空间复杂度,可以仅维护前一个状态的值,从而将空间复杂度降低至 O(1): #### 空间优化版本(O(1)) ```java class Solution { public int maxSubArray(int[] nums) { if (nums == null || nums.length == 0) { return 0; } int prev = nums[0]; int result = prev; for (int i = 1; i < nums.length; ++i) { prev = Math.max(nums[i], prev + nums[i]); result = Math.max(result, prev); } return result; } } ``` 在该实现中,变量 `prev` 记录以当前元素结尾的最大子数组和,而 `result` 跟踪整个过程中的最大值。这样避免了额外数组的使用,使空间效率更优[^2]。 --- ### 相关应用场景 该算法不仅适用于最大子数组问题本身,还可推广至股票买卖利润最大化等场景。例如,在给定每日股价数组中寻找最大收益时,可以通过构造价格差分数组并应用最大子数组和算法来解决[^3]。 --- ### 示例:C++ 实现(异常安全与空间优化) 以下是一个 C++ 版本的实现,结合了内存管理优化和异常处理机制,确保程序在面对大规模输入时具备更好的鲁棒性: ```cpp #include <iostream> #include <vector> #include <algorithm> int maxSubArraySum(const std::vector<int>& nums) { if (nums.empty()) return 0; int prev = nums[0]; int result = prev; for (size_t i = 1; i < nums.size(); ++i) { prev = std::max(nums[i], prev + nums[i]); result = std::max(result, prev); } return result; } int main() { try { std::vector<int> nums; nums.reserve(1 << 20); // 预留百万级元素的空间 for (int i = 0; i < 1000000; ++i) { nums.push_back(i % 100 - 50); // [-50, 49] } int maxSum = maxSubArraySum(nums); std::cout << "Maximum subarray sum: " << maxSum << std::endl; } catch (const std::bad_alloc& e) { std::cerr << "Memory allocation failed: " << e.what() << std::endl; } catch (...) { std::cerr << "An unexpected error occurred." << std::endl; } return 0; } ``` 此实现通过 `reserve()` 提前分配足够容量,减少频繁扩容带来的性能开销,并通过 `try-catch` 捕获可能的内存分配异常,提高程序稳定性[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值