-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp~
More file actions
executable file
·216 lines (188 loc) · 4.78 KB
/
solution.cpp~
File metadata and controls
executable file
·216 lines (188 loc) · 4.78 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "solution.h"
int Solution::maxSubArray(vector<int>& nums)
{
vector<int> sums(nums.size() + 1);
vector<int> valley; //the valleys of sums
//To traverse sums, nums ans poles
vector<int>::const_iterator n_it;
vector<int>::iterator s_it;
vector<int>::iterator valley_it;
//discrete integration, count non-positive and obtain nums maximum
int num_max = *nums.begin();
sums[0] = 0;
for(n_it = nums.cbegin(), s_it = sums.begin() + 1;
s_it != sums.end();
n_it++, s_it++)
{
if(*n_it > num_max)
num_max = *n_it;
*s_it = *(s_it - 1) + *n_it;
}
#ifdef _DEBUG
cout << "nums_max = " << num_max << endl;
cout << "Sums = ";
for(const auto ele : sums)
cout << ele << " ";
cout << endl;
#endif
//If all elements of nums are non-positive
if(num_max <= 0)
return num_max;
//The difference between a peak and a valley is called a fall
int fall;
int fall_max = 0;
int current_sum;
//Generate valleys
if(*sums.begin() <= *(sums.begin() + 1))
valley.push_back(*(sums.begin()));
for(s_it = sums.begin() + 1; s_it != sums.end() - 1; s_it++)
{
current_sum = *s_it;
//A valley
if(current_sum <= *(s_it + 1) &&
current_sum < *(s_it - 1))
valley.push_back(current_sum);
//A peak
else if(current_sum >= *(s_it + 1) &&
current_sum > *(s_it - 1))
{
for(valley_it = valley.begin(); valley_it != valley.end();
valley_it++)
{
fall = current_sum - *valley_it;
fall_max = fall > fall_max ? fall : fall_max;
}
}
}
#ifdef _DEBUG
cout << "valley:" << endl;
if(valley.empty())
cout<<"<empty>"<<endl;
else
{
for(auto ele : valley)
cout << ele << " ";
cout<< endl;
}
#endif
if(*(sums.end() - 1) > *(sums.end() - 2))
{
current_sum = *(sums.end() - 1);
for(valley_it = valley.begin(); valley_it != valley.end();
valley_it++)
{
fall = current_sum - *valley_it;
fall_max = fall > fall_max ? fall : fall_max;
}
}
return fall_max;
}
/*int Solution::maxSubArray(vector<int>& nums)
{
vector<int> sums(nums.size() + 1);
vector<int> valley; //the valleys of sums
//To traverse sums, nums ans poles
vector<int>::const_iterator n_it;
vector<int>::iterator s_it;
vector<int>::iterator valley_it;
//discrete integration, count non-positive and obtain nums maximum
int num_max = *nums.begin();
sums[0] = 0;
for(n_it = nums.cbegin(), s_it = sums.begin() + 1;
s_it != sums.end();
n_it++, s_it++)
{
if(*n_it > num_max)
num_max = *n_it;
*s_it = *(s_it - 1) + *n_it;
}
#ifdef _DEBUG
cout << "nums_max = " << num_max << endl;
cout << "Sums = ";
for(const auto ele : sums)
cout << ele << " ";
cout << endl;
#endif
//If all elements of nums are non-positive
if(num_max <= 0)
return num_max;
//The difference between a peak and a valley is called a fall
int fall;
int fall_max = 0;
int current_sum;
int prev_valley_size = 0;
int prev_peak = 0;
//Generate valleys
if(*sums.begin() <= *(sums.begin() + 1))
valley.push_back(*(sums.begin()));
for(s_it = sums.begin() + 1; s_it != sums.end() - 1; s_it++)
{
current_sum = *s_it;
//A valley
if(current_sum <= *(s_it + 1) &&
current_sum < *(s_it - 1))
{
#ifdef _DEBUG
cout << "\tValley: " << current_sum << endl;
#endif
valley.push_back(current_sum);
}
//A peak
else if(current_sum >= *(s_it + 1) &&
current_sum > *(s_it - 1))
{
#ifdef _DEBUG
cout << "\tPeak: " << current_sum << endl;
#endif
//size of valley has not changed
if(prev_valley_size == valley.size())
{
if(current_sum > prev_peak)
{
fall_max += current_sum - prev_peak;
prev_peak = current_sum;
}
}
//size of valley has changed
else
{
prev_valley_size = valley.size();
for(valley_it = valley.begin(); valley_it != valley.end();
valley_it++)
{
fall = current_sum - *valley_it;
if(fall > fall_max)
{
fall_max = fall;
prev_peak = current_sum;
}
#ifdef _DEBUG
cout << " fall_max: " << fall_max << endl;
#endif
}
}
}
}
#ifdef _DEBUG
cout << "valley:" << endl;
if(valley.empty())
cout<<"<empty>"<<endl;
else
{
for(auto ele : valley)
cout << ele << " ";
cout<< endl;
}
#endif
if(*(sums.end() - 1) > *(sums.end() - 2))
{
current_sum = *(sums.end() - 1);
for(valley_it = valley.begin(); valley_it != valley.end();
valley_it++)
{
fall = current_sum - *valley_it;
fall_max = fall > fall_max ? fall : fall_max;
}
}
return fall_max;
}*/