Topic 1 The most kid array
Describe:
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? YesSample Example
Given array [1,-1,-2, 1], return-3
labelLintcode All rights reserved sub-array greedy arrayTopic 2 Maximum sub-arraysDescription:
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? YesSample 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 Enumeration method Array Lintcode All rights reserved sub-arrays LinkedInTopic Analysis:Topic 1 To find the smallest sub-array and, the topic does not write requirements algorithm complexity, you can consider the Python array slicing function, only get the sub-array, and then sum, get the minimum valueby dynamic programming, get Nums[i:j] sub-array and, Algorithm complexity O (N*LOGN)
def minsubarray (nums): max = min = sum (nums) maxlist = minlist = nums n = Len (nums) +1 for I in range (n+1): For J in Range (i+1,n+1): sonlist = nums[i:j] sum = SUM (sonlist) if sum >= max: max = Sum maxli st = sonlist elif sum < min: min = sum minlist = sonlist print maxlist,sum (maxlist) print min List, sum (minlist)
Topic two requires algorithm complexity O (n), the appellate dynamic programming algorithm does not meet the requirements, using the greedy algorithm:
Class solution: "" " @param nums:a List of integers @return: An integer denote the sum of maximum subarray
"" " def maxsubarray (self, nums): # Write your code here n = len (nums) maxsum = SUM (nums) cursum = 0
for I in range (n): # starts with Sum, if current and greater than Maxsum, assigns to maxsum cursum + = nums[i] if cursum > maxsum: Maxsum = cursum # front and if it is less than 0, then the next element value is definitely less than the next element value # So if the previous value is less than 0, discard the preceding sum, and proceed from the next one to continue the sum if cursum < 0: cursum = 0 return maxsum
Similarly, the smallest sub-array can also use the greedy algorithm, the disadvantage is not to get to the smallest substring, can only get its and.
Class solution: "" " @param nums:a List of integers @return: A integer denote the sum of minimum subarray "" " def minsubarray (self, nums): # Write your code here n = len (nums) minsum = SUM (nums) cursum = 0
for I in range (n): # starts with Sum, if current and less than Minsum, assigns minsum cursum + = nums[i] if cursum < minsum: Minsum = cursum # front and if it is already greater than 0, then adding the next element value is definitely greater than the next element value # So if the previous value is greater than 0, discard the preceding sum, and proceed from the next one to continue the sum if cursum > 0: cursum = 0 return minsum
Lintcode Python Simple-level topic minimum Sub-array and maximum subarray and