-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMaxNonNegativeSubArray.cpp
95 lines (76 loc) · 1.89 KB
/
MaxNonNegativeSubArray.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// MaxNonNegativeSubArray.cpp
// C++ TESTING SITE
//
// Created by DEEPTI SHARMA on 19/12/17.
// Copyright © 2017 . All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
class Solution{
public :
static vector<int>* maxset(vector<int> &A) {
int i =0;
int j=0;
int sum=0;
int sumMax=0;
int s1=0,s2=0;
int len=A.size();
vector<int>* max= new vector<int>(len);
vector<int>* maxSet= new vector<int>(len);
for ( i=0; i<len;i++ )
{
if( A[i]>0 )
{
sum+=A[i];
max->push_back(i);
}
else
{
if(sumMax<sum){
sumMax=sum;
sum=0;
maxSet->clear();
maxSet=max;
max->clear();
}
else if(sum==sumMax){
sum=0;
s1=maxSet->size();
s2=max->size();
if(s1<s2){
maxSet->clear();
maxSet=max;
max->clear();
}
max->clear();
}
else{
sum=0;
max->clear();
}
}
}
return maxSet;
}
};
int main()
{
vector<int> A;
vector<int>* res;
Solution S;
A.push_back(1);
A.push_back(2);
A.push_back(3);
A.push_back(-3);
A.push_back(1);
A.push_back(12);
res=S.maxset(A);
int sz;
sz=res->size();
for(int i=0;i<sz;i++){
cout << res->at(i) << " " ;
}
return 1;
}