(LeetCode OJ) Maximum Subarray [53]

Source: Internet
Author: User

(LeetCode OJ) Maximum Subarray [53]

53. Maximum Subarray

My Submissions QuestionTotal Accepted: 89899 Total Submissions: 253014 Difficulty: Medium

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.

Subscribe to see which companies asked this question

Hide Tags Divide and Conquer Array Dynamic Programming Show Similar Problems
// Typical dynamic planning problem: // train of thought first: // obviously, nums [I] is added in the continuous accumulation process. If sum is less than 0, so it indicates the side effects of the previous sums calculation. You need to re-find the starting value and start accumulating. // you need to re-start accumulating class Solution {public: int maxSubArray (vector
 
  
& Nums) {int maxSum = nums [0]; // The maximum value int subSum = nums [0]; for (int I = 1; I <nums. size (); I ++) {subSum = subSum <= 0? Nums [I]: subSum + nums [I]; if (subSum> maxSum) maxSum = subSum ;}return maxSum ;}};
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.