简历:The Google Resume: How to Prepare for a Career and Land a Job at Apple, Microsoft, Google, or any Top Tech Company
算法学习书籍:Introduction to Algorithms
编程珠玑:Programming Pearls (2nd Edition)
C++ 学习:The C++ Programming Language, 4th Edition
经典操作系统书籍,龙书:Operating System Concepts
创业:The Start-up of You: Adapt to the Future, Invest in Yourself, and Transform Your Career
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array
the contiguous subarray
[−2,1,−3,4,−1,2,1,−5,4]
,the contiguous subarray
[4,−1,2,1]
has the largest sum = 6
.
More practice:
» Solve this problem
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
The first way to do it is to iterate through the whole array and use two variables, sum and max to dynamically record the max subarray value.
Cases can be concluded below,
1. All elements in array are negative, we just need to find the biggest one.
2. Have both negative and positive values.
(1) discard negative ones, while we need to find the maximum subarrays, so it possible that the maximum subarray includes both negative and positive values. In this case, we use a sum to record.
We use max to represent that maximum value of subarray. Assume that currently we have max>0, now we go ahead and scan. If A[i]<0, discard, go ahead. But if A[i]+A[i+1]+...+A[i+k]>0, then we need add Ai..k to subarray, and replace max = max + A[i]+...+A[i+k]. Based on the analysis above, we can have the algorithm given below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxSubArray(int A[], int n) { | |
int sum = 0, max = INT_MIN; | |
for(int i=0; i<n; i++){ | |
sum+=A[i]; | |
if(sum>max) | |
max = sum; | |
if(sum<0) | |
sum = 0; | |
} | |
return max; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int maxSubArray(int A[], int n) { | |
2: // Start typing your C/C++ solution below | |
3: // DO NOT write int main() function | |
4: int maxV = INT_MIN; | |
5: return maxArray(A, 0, n-1, maxV); | |
6: } | |
7: int maxArray(int A[], int left, int right, int& maxV) | |
8: { | |
9: if(left>right) | |
10: return INT_MIN; | |
11: int mid = (left+right)/2; | |
12: int lmax = maxArray(A, left, mid -1, maxV); | |
13: int rmax = maxArray(A, mid + 1, right, maxV); | |
14: maxV = max(maxV, lmax); | |
15: maxV = max(maxV, rmax); | |
16: int sum = 0, mlmax = 0; | |
17: for(int i= mid -1; i>=left; i--) | |
18: { | |
19: sum += A[i]; | |
20: if(sum > mlmax) | |
21: mlmax = sum; | |
22: } | |
23: sum = 0; int mrmax = 0; | |
24: for(int i = mid +1; i<=right; i++) | |
25: { | |
26: sum += A[i]; | |
27: if(sum > mrmax) | |
28: mrmax = sum; | |
29: } | |
30: maxV = max(maxV, mlmax + mrmax + A[mid]); | |
31: return maxV; | |
32: } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int maxSubArray(int A[], int n) { | |
vector<int> rec(n, 0); | |
rec[0] = A[0]; | |
int max = rec[0]; | |
for(int i=1; i<n; i++){ | |
if(rec[i-1]+A[i] < A[i]) rec[i] = A[i]; | |
else | |
rec[i] = rec[i-1] + A[i]; | |
if(rec[i]>max) max = rec[i]; | |
} | |
return max; | |
} |
Divide and conquer的那个解法好像是O(nlogn)的复杂度啊,不是O(n)的。
ReplyDeletegoogle这道题竟然把你这blog搜出来了。。。
ReplyDelete