-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStock_buy_and_sell.cpp
70 lines (54 loc) · 1.33 KB
/
Stock_buy_and_sell.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
//Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
vector<vector<int>> maxProfit(vector<int> prices, int n)
{
int i = 0;
if(n == 1)
return {{}};
vector<vector<int>> res;
int buy, sell;
while(i < n - 1){
while(i < n - 1 && prices[i+1] <= prices[i])
++i;
if(i == n - 1) return res;
buy = i++;
while ((i < n) && (prices[i] >= prices[i - 1]))
i++;
sell = i - 1;
res.push_back({buy, sell});
}
return res;
}
vector<vector<int> > stockBuySell(vector<int> A, int n){
return maxProfit(A, n);
}
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<int> A(n);
for (int i=0; i<n; ++i){
cin>>A[i];
}
Solution ob;
vector<vector<int> > ans = ob.stockBuySell(A, n);
if(ans.size()==0)
cout<<"No Profit";
else{
for (int i=0; i<ans.size(); ++i){
cout<<"("<<ans[i][0]<<" "<<ans[i][1]<<") ";
}
}cout<<endl;
}
return 0;
}
// } Driver Code Ends