Lintcode.44 minimum sub-array, lintcode.44 Array

Source: Internet
Author: User
Tags lintcode

Lintcode.44 minimum sub-array, lintcode.44 Array

Minimum Child Array

 
  • Description
  • Notes
  • Data
  • Evaluation

Given an integer array, find a child array with the smallest sum. Returns the smallest sum of values.

Notes

The sub-array contains at least one number.

Have you ever encountered this question during a real interview? Yes, which company asked you this question? Airbnb Amazon LinkedIn Cryptic Studios Dropbox Apple Epic Systems TinyCo Yelp Hedvig Zenefits Uber Snapchat Yahoo Microsoft Bloomberg Facebook Google Twitter Thank you for your feedback. Example

Returns an array.[1,-1,-2, 1], Return-3

TagLintCode Copyright All Rights Reserved greedy sub-array Brute Force
class Solution {public:    /*     * @param nums: a list of integers     * @return: A integer indicate the sum of minimum subarray     */    int minSubArray(vector<int> &nums) {        // write your code here        int s=nums.size();        int res=nums[0];        for(int i=0;i<s;i++)        {            int cn=nums[i];            if(cn<res)                res=cn;            for(int j=i+1;j<s;j++)            {                cn+=nums[j];                if(cn<=res)                    res=cn;            }        }        return res;    }};

Greedy

 

class Solution {public:    /*     * @param nums: a list of integers     * @return: A integer indicate the sum of minimum subarray     */    int minSubArray(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;    }};

  

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.