-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC_MaximumSubarray.cpp
More file actions
executable file
·50 lines (46 loc) · 1.05 KB
/
LC_MaximumSubarray.cpp
File metadata and controls
executable file
·50 lines (46 loc) · 1.05 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lli long long int
#define vi vector<int>
#define forinput for(int i = 0; i<n ; i++){cin>>arr[i];}
#define foroutput for(int i = 0; i<n; i++){cout<<arr[i]<<" ";}
#define pb push_back
#define pob pop_back
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max_i = INT_MIN;
int count = 0;
for(auto x : nums){
count+=x;
max_i = max(count, max_i);
if(count < 0)
count = 0;
}
return max_i;
//Second Way..Both approaches are same, just upper is short a little
// if(nums.size()<=1){
// return nums.front();
// }
// int sum = 0;
// int cnt_1 = INT_MIN;
// for(auto val : nums){
// sum+=val;
// cnt_1 = max(sum, cnt_1);
// if(sum < 0)
// sum = 0;
// }
// return cnt_1;
}
};
int main(){
Solution S;
int input;
vi nums;
while(cin>>input){
nums.pb(input);
}
cout<<S.maxSubArray(nums)<<endl;
return 0;
}