lincode.41 Maximum sub-array

Source: Internet
Author: User

Maximum sub-array

    • Describe
    • Notes
    • Data
    • Evaluation

Given an array of integers, find a sub-array with the maximum and return its maximum and.

Precautions

A sub-array contains at least one number

Have you ever encountered this problem in a real interview? YesWhich company is asking you this question?AirbnbAmazonlinkedin cryptic Studios Span class= "BTN Btn-xs btn-white Company" >dropbox apple epic Systems tinyco yelp hedvig zenefits uber snapchat yahoo microsoft bloomberg Thank you for your feedback Sample Example

Given [?2,2,?3,4,?1,2,1,?5,3] arrays, the sub-array that meets the requirements is [4,?1,2,1] , its maximum and6

challenges

Requires a time complexity of O (n)

labelGreedy LinkedIn array Lintcode All rights reserved Sub-Array enumeration method
Class Solution {public:    /     * * @param nums:a List of integers     * @return: A integer indicate the sum of Max Sub Array     *    /int Maxsubarray (vector<int> &nums) {        //write your code here        int S=nums.size ();        int res=nums[0];        int cn=0;        for (int i =0;i<s;i++) {            cn+=nums[i];            if (RES<CN)                RES=CN;            if (cn<0)                cn=0;        }        return res;    }};

  

lincode.41 Maximum sub-array

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.