-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMaxSubarraySum.cpp
More file actions
38 lines (33 loc) · 814 Bytes
/
Copy pathMaxSubarraySum.cpp
File metadata and controls
38 lines (33 loc) · 814 Bytes
1
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
#include <bits/stdc++.h>
using namespace std;
// O(N) time and O(1) Space
int maxSubArray(vector<int>& nums) {
// INT_MIN is negative infinity
int maxSoFar = INT_MIN;
int maxEndingHere = 0;
for (int i = 0; i < nums.size(); i++) {
maxEndingHere += nums[i];
maxSoFar = max(maxSoFar, maxEndingHere);
maxEndingHere = max(maxEndingHere, 0);
}
return maxSoFar;
}
// Naive approach O(N^2) time and O(1) Space
int naive(vector<int>& nums){
int sum=INT_MIN;
for(int i = 0; i<nums.size();i++){
int temp = 0;
for(int j =i+1;j<nums.size();j++){
temp += nums[j];
if(sum < temp){
sum =temp;
}
}
}
return sum;
}
int main(){
vector<int> arr ={-2,1,-3,4,-1,2,1,-5,4};
cout<<maxSubArray(arr);
return 0;
}