Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock 

Best Time to Buy and Sell Stock 1
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

solution:
dp[i] contains the smallest prices until i-day;
the state transition equation is:
if prices[i] < dp[i - 1] then dp[i] = prices[i];
if prices[i] >= dp[i - 1] then dp[i] = dp[i-1];

int maxProfit(vector<int>& prices) 
{
    int n = prices.size();
    if (n <= 0) return 0;
    int max_profit = 0;
    int *dp = new int[n]();
    //memset(dp,0,sizeof(int)*n);
    for (int i = 0; i < n; ++i)
    {
        if (i == 0 || prices[i] < dp[i - 1])
            dp[i] = prices[i];
        else
        {
            dp[i] = dp[i - 1];
            max_profit = max(max_profit, prices[i] - dp[i]);
        }
    }
    delete[]dp;
    return max_profit;
}

duo to the fact that dp[i] only relies on previous state, we have the simplied form:
int maxProfit(vector<int>& prices) 
{
    int n = prices.size();
    if (n <= 0) return 0;
    int max_profit = 0, smallest_price = 0;
    for (int i = 0; i < n; ++i)
    {
        if (i == 0 || prices[i] < smallest_price)
            smallest_price = prices[i];
        else
            max_profit = max(max_profit, prices[i] - smallest_price);
    }
    return max_profit;
}
 
Best Time to Buy and Sell Stock 
 2 

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

solution:

reference: https://leetcode.com/discuss/71354/share-my-thinking-process

2 possible transactions: buy and sell 

buy[i] means before day i what is the maxProfit for any sequence end with buy.

sell[i] means before day i what is the maxProfit for any sequence end with sell.

state transition equation is: 

buy[i] = max(sell[i-1]-price, buy[i-1]) 

sell[i] = max(buy[i-1]+price, sell[i-1])

int maxProfit(vector<int> &prices) 
{
    int buy(INT_MIN), sell(0), prev_sell(0), prev_buy;
    for (int price : prices) 
    {
        prev_buy = buy;
        prev_sell = sell;
        buy = max(prev_sell - price, buy);
        sell = max(prev_buy + price, sell);
    }
    return sell;
}
 another solution: greedy algorithm 
int maxProfit(vector<int>& prices) 
{
    int max_profit= 0;
    for(int i=1;i<prices.size();++i)
    {
        max_profit += max(0,prices[i]-prices[i-1]);
    }
    return max_profit;
}
 

Best Time to Buy and Sell Stock 3 

Say you have an array for which the ith element is the price of a given stock on day i. 

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:   You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).   After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day) 

solution:

reference: https://leetcode.com/discuss/71354/share-my-thinking-process 

state transition equation is: 

buy[i] = max(sell[i-2]-price, buy[i-1]) 

sell[i] = max(buy[i-1]+price, sell[i-1])

int maxProfit(vector<int> &prices) 
{
    int buy(INT_MIN), sell(0), prev_sell(0), prev_buy;
    for (int price : prices) 
    {
        prev_buy = buy;
        buy = max(prev_sell - price, buy);
        prev_sell = sell;
        sell = max(prev_buy + price, sell);
    }
    return sell;
}
 

Best Time to Buy and Sell Stock 4 

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most K transactions.

solution:

f[k, ii] represents the max profit up until prices[ii] (Note: NOT ending with prices[ii]) using at most k transactions.     

f[k, ii] = max(f[k, ii-1], prices[ii] - prices[jj] + f[k-1, jj]) { jj in range of [0, ii-1] }             

  = max(f[k, ii-1], prices[ii] + max(f[k-1, jj] - prices[jj]))   

f[0, ii] = 0; 0 times transation makes 0 profit

f[k, 0] = 0; if there is only one price data point you can't make any money no matter how many times you can trade 

int maxProfit(vector<int> &prices,int K) 
{
    if (prices.size() <= 1) return 0;
    int maxProf = 0;
    vector<vector<int>> f(K+1, vector<int>(prices.size(), 0));
    for (int kk = 1; kk <= K; kk++) 
    {
        int tmpMax = f[kk-1][0] - prices[0];
        for (int ii = 1; ii < prices.size(); ii++) 
        {
            f[kk][ii] = max(f[kk][ii-1], prices[ii] + tmpMax);
            tmpMax = max(tmpMax, f[kk-1][ii] - prices[ii]);
            maxProf = max(f[kk][ii], maxProf);
        }
    }
    return maxProf;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值