-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbull sell stock.cpp
45 lines (31 loc) · 1.15 KB
/
bull sell stock.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
/*You are given an array prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.*/
class Solution {
public:
int maxProfit(vector<int>& prices) {
/*
*****this gave TLE as time complexitiy is of o(n^2)********
int n=prices.size();
int maxprofit=0;
int current_profit=0;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
current_profit=prices[j]-prices[i];
if(current_profit > maxprofit && current_profit >0)
maxprofit=current_profit;
}
}
return maxprofit;*/
int n= prices.size();
int buyAtPrice = INT_MAX;
int maxProfit = 0;
for(int i=0; i<n; i++){
buyAtPrice = min(buyAtPrice, prices[i]);
maxProfit = max(maxProfit, prices[i] - buyAtPrice);
}
return maxProfit;
}
};