053._maximum_subarray.md 2.8 KB
Newer Older
K
KEQI HUANG 已提交
1
### 53. Maximum Subarray
K
KEQI HUANG 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

题目:
<https://leetcode.com/problems/maximum-subarray/>


难度:
Medium


思路一:

O(N^2)

从i开始,计算i到n,存比较大的sum,会超时

```
class Solution(object):
	def maxSubArray(self, nums):
	    """
	    :type nums: List[int]
	    :rtype: int
	    """
	    n = len(nums)
	    m = float('-inf')
	    for i in range(n):
	    	s = 0
	    	for j in range(i,n):
	    		s = s + nums[j]
	    		m = max(m,s)
	    return m 
```

思路二:

动归

ms(i) = max(ms[i-1]+ a[i],a[i])

到i处的最大值两个可能,一个是加上a[i],另一个从a[i]起头,重新开始。可以AC

K
KEQI HUANG 已提交
42
```python
K
KEQI HUANG 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
class Solution(object):
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums)
        maxSum = [nums[0] for i in range(n)]
        for i in range(1,n):
        	maxSum[i] = max(maxSum[i-1] + nums[i], nums[i])
        return max(maxSum)
```


思路三:


Kadane’s Algorithm wikipedia可以查到,然后一般的是负的可以还回0,这里需要稍作修改,参考

<http://algorithms.tutorialhorizon.com/kadanes-algorithm-maximum-subarray-problem/>


```
start:
    max_so_far = a[0]
    max_ending_here = a[0]

loop i= 1 to n
  (i) max_end_here = Max(arrA[i], max_end_here+a[i]);
  (ii) max_so_far = Max(max_so_far,max_end_here);

return max_so_far

```

AC代码:

K
KEQI HUANG 已提交
80
```python
K
KEQI HUANG 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
class Solution(object):
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums)
        maxSum , maxEnd = nums[0], nums[0]
        
        for i in range(1,n):
        	maxEnd = max(nums[i],maxEnd + nums[i])
        	maxSum = max(maxEnd,maxSum)
        return maxSum
```


思路四:



参见clrs 第71页,用divide and conquer,有伪码


最大的subarray sum有三个可能,左半段或者右半段,或者跨越左右半段,

速度比较慢,AC代码,复杂度O(NlogN)

```
class Solution(object):
	def maxSubArray(self, nums):
		"""
		:type nums: List[int]
		:rtype: int
		"""
		def find_max_crossing_subarray(nums, low, mid, high):
			left_sum = float('-inf')
			sum = 0
			for i in xrange(mid,low-1,-1):
				sum = sum + nums[i]
				if sum > left_sum:
					left_sum = sum

			right_sum = float('-inf')
			sum = 0
			for j in range(mid+1,high+1):
				sum = sum + nums[j]
				if sum > right_sum:
					right_sum = sum

			return left_sum + right_sum

		def find_max_subarray(nums,low,high):
			if low == high: 
				return nums[low]
			else:
				mid = (low + high) / 2
				left_sum = find_max_subarray(nums, low, mid)
				right_sum = find_max_subarray(nums,mid+1,high)
				cross_sum = find_max_crossing_subarray(nums,low,mid,high)
				# print left_sum, right_sum, cross_sum
				# print mid, low, high
				return max(left_sum, right_sum, cross_sum)

		return find_max_subarray(nums, 0, len(nums)-1)

K
KEQI HUANG 已提交
146
```