zoukankan      html  css  js  c++  java
  • LeetCode: Best Time to Buy and Sell Stock

    LeetCode: Best Time to Buy and Sell Stock

    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.

    地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/

    算法:动态规划搞定。用dp[i-1]来存储前面 i 天的最大收入。对于第 i+1 天的收入可以这样计算:如果该天股票的价钱低于前一天的价格,则我们不会选择在这一天卖出,所以收入应该跟第 i 天一样,如果该天股票价钱高于前一天,那么最大的收入应该是以下两种情况的最大值:1)不在该天卖出,则收入跟前一天保持不变;2)在该天卖出,则在前面的天数里找一天价格最低的买入,然后在该天卖出。代码:

     1 class Solution {
     2 public:
     3     int maxProfit(vector<int> &prices) {
     4         if(prices.empty())    return 0;
     5         int n = prices.size();
     6         vector<int> dp(n);
     7         dp[0] = 0;
     8         for(int i = 1; i < n; ++i){
     9             if(prices[i] <= prices[i-1]){
    10                 dp[i] = dp[i-1];
    11             }else{
    12                 int max_profit = dp[i-1];
    13                 int min_prices = 0x7FFFFFFF;
    14                 for(int j = 0; j < i; ++j){
    15                     if(min_prices > prices[j]){
    16                         min_prices = prices[j];
    17                     }
    18                 }
    19                 if(max_profit < prices[i] - min_prices){
    20                     max_profit = prices[i] - min_prices;
    21                 }
    22                 dp[i] = max_profit;
    23             }
    24         }
    25         return dp[n-1];
    26     }
    27 };

    第二题:

    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).

    地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

    算法:这一题与上一题不同点在于,这一题可以买卖多次,而上一题只能买卖一次。同样也可以用动态规划解决,用dp[i]表示前面 i 天的收入(注意,上一题是用dp[i-1]表示),则第i+1天的收入可以这样计算:如果该天股票的价钱低于前一天的价格,则我们不会选择在这一天卖出,所以收入应该跟第 i 天一样,如果该天股票价钱高于前一天,那么最大的收入应该是以下两种情况的最大值:1)不再该天卖出,则收入跟前一天一样;2)在该天卖出,则在该天的前面找所有股票价格比该天低的天数,比如j,然后收入为dp[j-1]加上第i+1天价钱减去第j天价格,从所有满足条件的j里选收入最大的。代码:

     1 class Solution {
     2 public:
     3     int maxProfit(vector<int> &prices) {
     4         if(prices.empty())    return 0;
     5         int n = prices.size();
     6         vector<int> dp(n+1);
     7         dp[0] = 0;
     8         dp[1] = 0;
     9         for(int i = 2; i <= n; ++i){
    10             if(prices[i-1] <= prices[i-2]){
    11                 dp[i] = dp[i-1];
    12                 continue;
    13             }
    14             int max_profit = dp[i-1];
    15             for(int j = 1; j < i; ++j){
    16                 if(prices[j-1] < prices[i-1] && max_profit < dp[j-1] + prices[i-1] - prices[j-1]){
    17                     max_profit = dp[j-1] + prices[i-1] - prices[j-1];
    18                 }
    19             }
    20             dp[i] = max_profit;
    21         }
    22         return dp[n];
    23     }
    24 };

    第三题:

    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 two transactions.

    Note:
    You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

    地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/

    算法:这道题的难度更大,因为可以买卖的次数被限制在两次。这里用两个数组来存储子问题,其中dp1[i]表示在前i天最多只买卖一次的收入,dp2[i]表示在前i天最多只买卖两次的收入。其中dp1的更新跟第一题一样,dp2的更新类似与第二题,但在该天有卖出的情况下,更新dp2的数组应该采用dp1,因为在该天有卖出的话就表示前面只能买卖一次。看代码吧:

     1 class Solution {
     2 public:
     3     int maxProfit(vector<int> &prices) {
     4         if(prices.empty())  return 0;
     5         int n = prices.size();
     6         vector<int> dp1(n+1);
     7         vector<int> dp2(n+1);
     8         dp1[0] = dp1[1] = 0;
     9         dp2[0] = dp2[1] = 0;
    10         for(int i = 2; i <= n; ++i){
    11             if(prices[i-1] <= prices[i-2]){
    12                 dp1[i] = dp1[i-1];
    13                 dp2[i] = dp2[i-1];
    14             }else{
    15                 int max_profit = dp1[i-1];
    16                 int min_prices = 0x7FFFFFFF;
    17                 for(int j = 1; j < i; ++j){
    18                     if(min_prices > prices[j-1]){
    19                         min_prices = prices[j-1];
    20                     }
    21                 }
    22                 if(max_profit < prices[i-1] - min_prices){
    23                     max_profit = prices[i-1] - min_prices;
    24                 }
    25                 dp1[i] = max_profit;
    26                 max_profit = dp2[i-1];
    27                 for(int j = 1; j < i; ++j){
    28                     if(prices[i-1] > prices[j-1] && max_profit < dp1[j-1] + prices[i-1] - prices[j-1]){
    29                         max_profit = dp1[j-1] + prices[i-1] - prices[j-1];
    30                     }
    31                 }
    32                 dp2[i] = max_profit;
    33             }
    34         }
    35         return dp2[n];
    36     }
    37 };
  • 相关阅读:
    长沙Uber司机奖励政策(8月24日到8月30日)
    苏州Uber优步司机奖励政策(8月31日至9月6日)
    武汉Uber优步司机奖励政策(8月31日~9月6日)
    青岛Uber司机奖励政策(8月31号~9月6号)
    成都Uber优步司机奖励政策(8月31日~9月6日)
    成都UBER优步司机第六组奖励政策
    敏捷软件开发--敏捷开发原则
    UML--对象的介绍
    ip 子网掩码 网关 DNS
    敏捷软件开发--计划
  • 原文地址:https://www.cnblogs.com/boostable/p/leetcode_best_time_to_buy_and_sell_stock.html
Copyright © 2011-2022 走看看