forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buysellstock.cpp
72 lines (64 loc) · 1.62 KB
/
Buysellstock.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
class Solution {
public:
int maxProfit(vector<int>& prices) {
// int max = prices[0] ;
int min = prices[0] ;
int c = 0;
int total_profit;
if(prices.size() > 1)
// {
// for (int i = 0 ; i < prices.size()-1; i++)
// {
// if (prices[i] < min)
// {
// min = prices[i];
// c++;
// }
// }
// cout << c <<" "<< min ;
int max = prices[0] ;
for (int j = 0 ; j < prices.size() ; j++)
{
if (prices[j] > max)
{
max = prices[j];
}
}
int lastOcc(int prices[],int n,int max){
if (n==0){
return -1
}
int subindex = lastOcc(prices + 1,n-1,max)
if(subindex==-1){
if(arr[0]==key){
return 0;
}
else{
return -1;
}
}
else{
return subIndex + 1;
}
}
m=lastocc()
{
for (int i = 0 ; i < m; i++)
{
if (prices[i] < min)
{
min = prices[i];
c++;
}
}
cout << c <<" "<< min ;
if ( max > min)
total_profit = max - min;
return total_profit;
}
else
{
return 0 ;
}
}
};