[LeetCode] 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.

click to show more practice.

More practice:

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

这道题让我们求最大子数组之和,并且要我们用两种方法来解,分别是O(n)的解法,还有用分治法Divide and Conquer Approach,这个解法的时间复杂度是O(nlgn),那我们就先来看O(n)的解法,定义两个变量res和tmp,其中res保存最终要返回的结果,即最大的子数组之和,tmp是个临时变量,初始值为数组的第一个数,每遍历一个数字A[i],比较tmp + A[i]和A[i]中的较大值存入tmp,然后再把res和tmp中的较大值存入res,以此类推直到遍历完整个数组,可得到最大子数组的值存在res中,代码如下:

解法一

// O(n) solution
class Solution {
public:
    int maxSubArray(int A[], int n) {
        int res = A[0], tmp = A[0];
        for (int i = 1; i < n; ++i) {
            tmp = max(tmp + A[i], A[i]);
            res = max(res, tmp);
        }
        return res;
    }
};

题目还要求我们用分治法Divide and Conquer Approach来解,这个分治法的思想就类似于二分搜索法,我们需要把数组一分为二,分别找出左边和右边的最大子数组之和,然后还要从中间开始向左右分别扫描,求出的最大值分别和左右两边得出的最大值相比较取最大的那一个,代码如下:

解法二

// Divide and conquer approach
class Solution {
public:
    int maxSubArray(int A[], int n) {
        return getMaxSubArray(A, 0, n - 1);
    }
    int getMaxSubArray(int A[], int left, int right) {
        if (left >= right) return A[left];
        int mid = (left + right) / 2;
        int lmax = getMaxSubArray(A, left, mid - 1);
        int rmax = getMaxSubArray(A, mid + 1, right);
        int mmax = A[mid], tmp = A[mid];
        for (int i = mid - 1; i >= left; --i) {
            tmp += A[i];
            mmax = max(mmax, tmp);
        }
        tmp = mmax;
        for (int i = mid + 1; i <= right; ++i) {
            tmp += A[i];
            mmax = max(mmax, tmp);
        }
        return max(mmax, max(lmax, rmax));
    }
};
时间: 2024-10-14 03:47:16

[LeetCode] Maximum Subarray 最大子数组的相关文章

lintcode 中等题:maximum subarray最大子数组II

题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 您在真实的面试中是否遇到过这个题? Yes 样例 给出数组[1, 3, -1, 2, -1, 2],这两个子数组分别为[1, 3]和[2, -1, 2]或者[1, 3, -1, 2]和[2],它们的最大和都是7 注意 子数组最少包含一个数 挑战 要求时间复杂度为O(n) 解题 最大子数组I 这个题目是求一个数组中一个最大子数组的和,而本题目是求数组中的前

【LeetCode每天一题】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

LeetCode 70 _ Maximum Subarray 最大子数组 (Easy)

Description: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer. Example 1: Input: 2 Output: 2

[LintCode] Maximum Subarray 最大子数组

Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarray should contain at least one number. Have you met this question in a real interview? Yes Example Given the array [−2,2,−3,4,−1,2,1,−5,3], the contigu

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. 这道题很经典,<算法导论>上有相关讨论,但是没有

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. 方法一:贪心算法greedy [一句话思路]: 每次

LeetCode——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. 原题链接: https://oj.leetcode.com/p

LeetCode: Maximum Subarray [052]

[题目] 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. [题意] 给定一个数组,找出和最大的子数组,返回这个

[leetcode]Maximum Subarray @ Python

原题地址:https://oj.leetcode.com/problems/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,