您的位置:首页 > 其它

【LeetCode】 best-time-to-buy-and-sell-stock-i ii iii iv

2017-08-19 21:20 471 查看
best-time-to-buy-and-sell-stock-i

Say you have an array for which the i th 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.

题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求最大的收益。

分析:动态规划法。从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求。

代码:时间O(n),空间O(1)。

int maxProfit(vector<int> &prices) {
          if (prices.size()<2) return 0;
          if (prices.size() == 2) prices[0] - prices[1]<0 ? 0 : prices[0] - prices[1];
          int MAX = 0, min = prices[0], size = prices.size();
          for (int i = 1; i < size; i++){
           
        if (prices[i]>min)
           
                  MAX = prices[i] - min>MAX ? prices[i] - min : MAX;
           
        else
           
                  min = prices[i];
          }
          return MAX <= 0 ? 0 : MAX;
}

best-time-to-buy-and-sell-stock-ii

Say you have an array for which the i th 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).

题目:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。交易次数不限,但一次只能交易一支股票,也就是说手上最多只能持有一支股票,求最大收益。

分析:贪心法。从前向后遍历数组,只要当天的价格高于前一天的价格,就算入收益。

代码:时间O(n),空间O(1)。
int maxProfit_ii(vector<int> &prices) {
          //本题由于允许多次交易(每次必须先卖出再买进),所以不好用爆搜
          //分析可知,要想利益最大化,就应该每次在波谷买进,波峰卖出,这样利益最大,操作次数最少
          //应该是使用动态规划来做可能比较简洁,个人觉得。
          //当股票高于前一天时,可获利润,累加所有的利润即可获得最大值
          int len = prices.size();
          vector<int> change(len, 0);
          int maxPro = 0;
          for (int i = 1; i<len; i++){//所有递增子区间
          
         change[i] = prices[i] - prices[i - 1];//记录所有长和跌的情况
        
          if (change[i]>0)maxPro += change[i];//累加所有长幅,即为最大收益
          }
          //波峰减波谷等于波峰到波谷的元素依次相减的和,写出公式就知道是抵消的。 a, b, c, d, e ; e - a = e - d + d - c + c - b + b - a
          return maxPro;
}

best-time-to-buy-and-sell-stock-iii

Say you have an array for which the i th 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).

题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。最多交易两次,手上最多只能持有一支股票,求最大收益。

分析:动态规划法。以第i天为分界线,计算第i天之前进行一次交易的最大收益preProfit[i],和第i天之后进行一次交易的最大收益postProfit[i],最后遍历一遍,max{preProfit[i] + postProfit[i]} (0≤i≤n-1)就是最大收益。第i天之前和第i天之后进行一次的最大收益求法同Best Time to Buy and Sell Stock I。

代码:时间O(n),空间O(n)。

int maxProfit_iii(vector<int> &prices) {
          int len = prices.size();
          int firstBuy = INT_MAX;
          int firstSell = 0;
          int secondBuy = INT_MAX;
          int secondSell = 0;
          for (int i = 0; i<len; i++)
          {
                    firstBuy
= min(firstBuy, prices[i]);
                    firstSell
= max(firstSell, prices[i] - firstBuy);
               
    secondBuy = min(secondBuy, prices[i] - firstSell);
               
    secondSell = max(secondSell, prices[i] - secondBuy);
          }
          return secondSell;
 }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: