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