# Buy & Sell : interview question

Here is the list one should be aware of

Below is the direct consise readable solution. To read & understand in depth I would prefer reading this blog post on leetcode.

```//Buy and Sell Stock:1
int maxProfit(vector<int>& prices) {
int sell = 0;
for (int price : prices) {
// the maximum profit if only one transaction is allowed
sell = max(sell, price - buy);
}
return sell;
}
//Method 1:
int maxProfit2(vector<int>& prices) {
int maxprofit = 0;
for (int i = 1; i < prices.size(); i++) {
if (prices[i] > prices[i - 1])
maxprofit += prices[i] - prices[i - 1];
}
return maxprofit;
}
//Method 2:
int maxProfit2(vector<int>& prices) {
int sell = 0;
for (int price : prices) {
buy = min(buy, price-sell); //Use the previous profit for reinvetsment and minimize expenditure
sell = max(sell, price - buy);
}
return sell;
}

//Method 1:
int maxProfit3(int* prices, int pricesSize){
int len = pricesSize;
if(len == 0)
return 0;
int min_so_far = prices;
int max_so_far = prices[len-1];
int max_profit = 0;
int maxProf[len];
memset(maxProf,0,len*sizeof(int));

for(int i=1;i<len; i++){
int profit = (prices[i] - min_so_far) ;
max_profit = max_profit > profit ? max_profit : profit;
maxProf[i] = max_profit;
min_so_far = min_so_far < prices[i] ? min_so_far : prices[i];

}

for(int i = len-2;i>0;i--){
int profit = (max_so_far - prices[i])+maxProf[i];
max_profit = max_profit > profit ? max_profit : profit;
max_so_far = max_so_far > prices[i] ? max_so_far : prices[i];
}

return max_profit;
}

//Method 2:
int maxProfit3(vector<int>& prices) {
int sell1 = 0, sell2 = 0;
for (int price : prices) {
// the maximum profit if only one transaction is allowed
sell1 = max(sell1, price - buy1);
// re-invest the gained profit in the second transaction
sell2 = max(sell2, price - buy2);
}
return sell2;
}

int maxProfit4(int k, vector<int>& prices) {

if (k >= prices.size() / 2) { // if k >= n/2, then you can make maximum number of transactions
int profit = 0;
for (int i = 1; i < prices.size(); i++)
if (prices[i] > prices[i - 1]) profit += prices[i] - prices[i - 1];
return profit;
}
vector<int> sell(k + 1,0);

for (int price : prices) {
for (int i = 1; i <= k; i++) {
}
}
return sell[k];
}

//Buy and sell stock with cooldown period
int maxProfit(vector<int>& prices) {
for (int price : prices) {
prev_sell = sell;
}
return sell;
}

//Buy and Sell with transaction fee

int maxProfit(vector<int>& prices, int fee) {
for (int price : prices) {
}
return sell;
} 