【LeetCode】053. 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.

题解:

  由于是连续子序列这个限制,所以如果k+1这个元素之前的和是小于0的,那么对于增大k+1这个元素从而去组成最大子序列是没有贡献的,所以可以把sum置0处理。

本质上是DP问题。,只是要先求出sum。

Solution 1 ()

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int res = INT_MIN, sum = 0;
        for (int n : nums) {
        /*  if(sum < 0) sum = 0;
            sum += A[i];
            res = max(res, sum); */
            sum = max(sum + n, n);
            res = max(res, sum);
        }
        return cur;
    }
};

Solution 2 (TLE)

class Solution {
public:
    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], tmp = mmax;
        for(int i=mid-1; i>=left; --i) {
            tmp += nums[i];
            mmax = max(tmp, mmax);
        }
        tmp = mmax;
        for(int i=mid+1; i<=right; ++i) {
            tmp += nums[i];
            mmax = max(tmp, mmax);
        }
        return max(mmax, max(lmax, rmax));
    }
    int maxSubArray(vector<int>& nums) {
        if(nums.size() == 0) return 0;
        return helper(nums,0, nums.size()-1);
    }
};
时间: 2024-10-27 13:06:16

【LeetCode】053. Maximum Subarray的相关文章

【leetcode】998. Maximum Binary Tree II

题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree. Just as in the previous problem, the given tree was constructed from an list A (root = Construct(A)) recursively with

【LeetCode】152. Maximum Product Subarray

题目: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 题解: 先暴力解,遍历所有组合,更新最大值.很显然得超时. Solution

【Leetcode】Minimum Size Subarray Sum

题目链接:https://leetcode.com/problems/minimum-size-subarray-sum/ 题目: Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the

【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays

题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k, and we want to maximize the sum of all 3*k entries. Return the result as a list of indices representing the star

【LeetCode】104 - Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Solution: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val;

【leetcode】Minimum Size Subarray Sum(middle)

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

【leetcode】1224. Maximum Equal Frequency

题目如下: Given an array nums of positive integers, return the longest possible length of an array prefix of nums, such that it is possible to remove exactly one element from this prefix so that every number that has appeared in it will have the same num

【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. More practice: If you have figu

【LeetCode】 Maximum Depth of Binary Tree

Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root