-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP122.cpp
More file actions
32 lines (30 loc) · 754 Bytes
/
P122.cpp
File metadata and controls
32 lines (30 loc) · 754 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
#include "header.h"
#include <iostream>
#include <vector>
class Solution {
public:
int maxProfit(vector<int>& prices) {
int _max, _min, ans =0;
int i=0;
while (i<prices.size()) {
int j=i;
while (j+1<prices.size() && prices[j+1]>prices[j]) j++;
if (j>i) {
ans += prices[j]-prices[i];
}
i=j+1;
}
return ans;
}
};
int main() {
vector<int> a = {1,2,3,4,5};
cout << Solution().maxProfit(a) << endl;
a = {7,1,5,3,6,4};
cout << Solution().maxProfit(a) << endl;
a = {7,6,4,3,1};
cout << Solution().maxProfit(a) << endl;
a = {7,1,10,10,100,50,200};
cout << Solution().maxProfit(a) << endl;
return 0;
}