The least-boy array
- Describe
- Notes
- Data
- Evaluation
Given an array of integers, find a sub-array with the minimum and. Returns its minimum 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 array [1,-1,-2, 1], return-3
labelLintcode Copyright Greedy subarray Array violence
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 (); C7/>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; }};
lintcode.44 Minimum Sub-array