Skip to content

Commit 366a00c

Browse files
committed
53 Maximum Subarray
1 parent 86fca52 commit 366a00c

File tree

5 files changed

+85
-1
lines changed

5 files changed

+85
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
+ [47 Permutations II(全排列)](algorithms/Permutations2)
3535
+ [49 Anagrams(字符串,回文构词)](algorithms/Anagrams)
3636
+ [50 Pow(x, n)(二分,浮点数比较,INT\_MIN绝对值)](algorithms/Pow)
37+
+ [53 Maximum Subarray(DP, 最大连续之和)](algorithms/MaximumSubarray)
3738
+ [58 Length of Last Word](algorithms/LengthofLastWord)
3839
+ [60 Permutation Sequence(康托展开)](algorithms/PermutationSequence)
3940
+ [64 Minimum Path Sum(DP,空间压缩)](algorithms/MinimumPathSum)
@@ -89,7 +90,7 @@
8990
+ [148 Sort List(归并排序)](algorithms/SortList)
9091
+ [150 Evaluate Reverse Polish Notation(栈的应用)](algorithms/EvaluateReversePolishNotation)
9192
+ [151 Reverse Words in a String](algorithms/ReverseWordsinaString)
92-
+ [152 Maximum Product Subarray(DP)](algorithms/MaximumProductSubarray)
93+
+ [152 Maximum Product Subarray(DP,最大连续之积)](algorithms/MaximumProductSubarray)
9394
+ [153 Find Minimum in Rotated Sorted Array(二分)](algorithms/FindMinimuminRotatedSortedArray)
9495
+ [154 Find Minimum in Rotated Sorted Array II(二分)](algorithms/FindMinimuminRotatedSortedArray2)
9596
+ [155 Min Stack(数据结构设计、栈的使用)](algorithms/MinStack)

algorithms/MaximumProductSubarray/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ int maxProduct(vector<int>& nums) {
4040
return max;
4141
}
4242
```
43+
44+
## 扩展
45+
46+
[Maximum Subarray](../MaximumSubarray): 最大连续之和

algorithms/MaximumSubarray/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Maximum Subarray
2+
3+
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
4+
5+
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
6+
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
7+
8+
click to show more practice.
9+
More practice:
10+
11+
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
12+
13+
## Solution
14+
15+
最大连续之和经典题
16+
17+
```c
18+
int maxSubArray(int *a, int n)
19+
{
20+
if (a == NULL || n <= 0)
21+
return 0;
22+
int max = a[0], sum = a[0];
23+
for (int i = 1; i < n; ++i) {
24+
if (sum < 0)
25+
sum = a[i];
26+
else
27+
sum += a[i];
28+
max = MAX(max, sum);
29+
}
30+
return max;
31+
}
32+
```
33+
## 扩展
34+
35+
[Maximum Product Subarray](../MaximumProductSubarray): 最大连续之积

algorithms/MaximumSubarray/in.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
9
2+
-2 1 -3 4 -1 2 1 -5 4
3+
5
4+
-1 -2 -3 -4 -5
5+
5
6+
1 -8 2 3 4

algorithms/MaximumSubarray/solve.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
static inline int MAX(int a, int b)
5+
{
6+
return a > b ? a : b;
7+
}
8+
int maxSubArray(int *a, int n)
9+
{
10+
if (a == NULL || n <= 0)
11+
return 0;
12+
int max = a[0], sum = a[0];
13+
for (int i = 1; i < n; ++i) {
14+
if (sum < 0)
15+
sum = a[i];
16+
else
17+
sum += a[i];
18+
max = MAX(max, sum);
19+
}
20+
return max;
21+
}
22+
void print(int *a, int n)
23+
{
24+
for (int i = 0; i < n; ++i)
25+
printf("%d ", a[i]);
26+
printf("\n");
27+
}
28+
int main(int argc, char **argv)
29+
{
30+
int a[20], n;
31+
memset(a, 0, sizeof(a));
32+
while (scanf("%d", &n) != EOF) {
33+
for (int i = 0; i < n; ++i)
34+
scanf("%d", &a[i]);
35+
printf("%d\n", maxSubArray(a, n));
36+
}
37+
return 0;
38+
}

0 commit comments

Comments
 (0)