Skip to content

Commit 38781ea

Browse files
Create Best time to buy and Sell Stock
1 parent 7f66386 commit 38781ea

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Best time to buy and Sell Stock

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
5+
int n=prices.size();
6+
int max_profit=0;
7+
int profit=0;
8+
int min=INT_MAX;
9+
10+
11+
for(int i=0;i<n;i++)
12+
{
13+
//for finding the minimum value
14+
15+
if(min>prices[i])
16+
{
17+
min=prices[i];
18+
19+
}
20+
//check profit
21+
profit=prices[i]-min;
22+
23+
//check if profit is greater then maximum profit or not
24+
if(max_profit<profit)
25+
{
26+
max_profit=profit;
27+
}
28+
}
29+
return max_profit;
30+
}
31+
};

0 commit comments

Comments
 (0)