add q3
This commit is contained in:
20
3-53-maximum_subarray.cpp
Normal file
20
3-53-maximum_subarray.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <bits/stdc++.h>
|
||||
#include <math.h>
|
||||
#include <algorithm>
|
||||
|
||||
int maxSubArray(int* nums, int numsSize) {
|
||||
int curmax = -1000000;
|
||||
int all_max = -1000000;
|
||||
for (int i = 0; i < numsSize; i++) {
|
||||
curmax = nums[i] > curmax + nums[i] ? nums[i] : curmax + nums[i];
|
||||
all_max = all_max > curmax ? all_max : curmax;
|
||||
// printf("%d %d\n", curmax, all_max);
|
||||
}
|
||||
return all_max;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int nums[] = {-3, -2, -2, -3};
|
||||
printf("%d\n", maxSubArray(nums, sizeof(nums) / sizeof(int)));
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user