53. Maximum Subarray

Problem statement:

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.

Solution:

The problem wants the max sum of a subarray. The basic idea is to drop the array if its sum is negative.

There two variables: one is the current sum, another is the max sum. The idea is similar with 300. Longest Increasing Subsequence.

Time complexity is O(n).

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int cur_sum = 0;
        int max_sum = INT_MIN;
        for(auto num : nums){
            if(cur_sum < 0){
                // cur_sum < 0, drop off it and make cur_sum = num
                cur_sum = num;
            } else {
                // if cur_sum >= 0, add num to cur_sum
                cur_sum += num;
            }
            // update the max sum for each element
            max_sum = max(max_sum, cur_sum);
        }
        return max_sum;
    }
};
时间: 2024-08-15 00:55:14

53. Maximum Subarray的相关文章

41. leetcode 53. Maximum Subarray

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. 思路:这个题还挺经典

LeetCode练题——53. Maximum Subarray

1.题目 53. Maximum Subarray——Easy 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 th

[Leetcode][Python]53: Maximum Subarray

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 53: Maximum Subarrayhttps://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 t

LeetCode 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. click to show more practice. Mor

leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

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

LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习

Description 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. F

【Divide and Conquer】53.Maximum Subarray(easy)

#week2# #from leetcode# Description 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

53.Maximum Subarray(法1线性扫面法2分治法)

Find the contiguous subarray within an array (containing at least onenumber) 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 thelargest sum = 6. click to showmore practice. HideT

[leedcode 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. public class Solution { public i