SoFunction
Updated on 2025-03-05

C++ implements LeetCode (53. Maximum subarray)

[LeetCode] 53. Maximum Subarray Maximum subarray

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

This problem requires finding the sum of the largest subarray, and two methods are used to solve it, namely the solution of O(n), and the dividing and conquer Approach. The time complexity of this solution is O(nlgn). Let’s first look at the solution of O(n) and define two variables res and curSum. Res saves the final result to be returned, that is, the sum of the largest subarrays. The initial value of curSum is 0. Each time one traversal is num, compare the larger values ​​in curSum + num and num to store them into curSum, and then store the larger values ​​in res and curSum into res, and so on until the complete array is traversed, you can get the value of the largest subarray to exist in res. The code is as follows:

C++ Solution 1:

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int res = INT_MIN, curSum = 0;
        for (int num : nums) {
            curSum = max(curSum + num, num);
            res = max(res, curSum);
        }
        return res;
    }
};

Java Solution 1:

public class Solution {
    public int maxSubArray(int[] nums) {
        int res = Integer.MIN_VALUE, curSum = 0;
        for (int num : nums) {
            curSum = (curSum + num, num);
            res = (res, curSum);
        }
        return res;
    }
}

The question also requires us to use the Divide and Conquer Approach method to solve it. The idea of ​​this Divide and Conquer Approach is similar to the binary search method. It is necessary to divide the array into two, find the sum of the largest subarrays on the left and right respectively, and then scan it from the middle to the left and right respectively, and compare the maximum value to the maximum value obtained on the left and right sides to take the largest one. The code is as follows:

C++ Solution 2:

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        if (()) return 0;
        return helper(nums, 0, (int)() - 1);
    }
    int helper(vector<int>& nums, int left, int right) {
        if (left >= right) return nums[left];
        int mid = left + (right - left) / 2;
        int lmax = helper(nums, left, mid - 1);
        int rmax = helper(nums, mid + 1, right);
        int mmax = nums[mid], t = mmax;
        for (int i = mid - 1; i >= left; --i) {
            t += nums[i];
            mmax = max(mmax, t);
        }
        t = mmax;
        for (int i = mid + 1; i <= right; ++i) {
            t += nums[i];
            mmax = max(mmax, t);
        }
        return max(mmax, max(lmax, rmax));
    }
};

Java Solution 2:

public class Solution {
    public int maxSubArray(int[] nums) {
        if ( == 0) return 0;
        return helper(nums, 0,  - 1);
    }
    public int helper(int[] nums, int left, int right) {
        if (left >= right) return nums[left];
        int mid = left + (right - left) / 2;
        int lmax = helper(nums, left, mid - 1);
        int rmax = helper(nums, mid + 1, right);
        int mmax = nums[mid], t = mmax;
        for (int i = mid - 1; i >= left; --i) {
            t += nums[i];
            mmax = (mmax, t);
        }
        t = mmax;
        for (int i = mid + 1; i <= right; ++i) {
            t += nums[i];
            mmax = (mmax, t);
        }
        return (mmax, (lmax, rmax));
    }
}

This is the end of this article about C++ implementing LeetCode (53. Maximum Subarray). For more related contents of C++ implementing the largest subarray, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!