zoukankan      html  css  js  c++  java
  • [leetcode]股票题型123

     122. Best Time to Buy and Sell Stock II

    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 (i.e., buy one and sell one share of the stock multiple times).

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

    分析:主要是寻找数组中的递减子数组,及递增子数组(或者可以认为是极值点),然后求和即可。

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int size = prices.size();
            if (size <= 1)
                return 0;
            int index = 0;
            int profit = 0;
            while(index < size)
            {
                int buy,sell;
                while(index+1 < size && prices[index] > prices[index+1]) index++;
                buy = index;
                while(index+1 < size && prices[index]< prices[index+1]) index++;
                sell = index;
                profit += prices[sell] - prices[buy];
                index++;
            }
            return profit;       
        }
    };

    123. Best Time to Buy and Sell Stock III

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

    动态规划preprofit保存i之前交易获得的收益postprofit保存i之后交易获得的收益

    只能进行两次交易,在第0-i天,我进行一次交易,在第i天后我进行一次交易。 

    • preprofit保存,从开始那天到第i天,如果我进行交易的最大值,从max{preprofit[i-1],price[i]-curmin}获得。

      从前往后扫:因为我要获得arr[0,..i](i++)包含i的最大收益:

              1.要找到0-i的最小值,curmin需要更新

              2.最大收益即之前的preprofit和当前price-curmin作对比。

    • postprofit保存,从第i天到最后一天,如果我进行交易的最大值,从max{postprofit[i-1],curmax-postprofit[i]}获得。

      从后往前扫描:因为我要获得arr[i+1……n](i++)的最大收益

              1.要找到i+1~n的最大值,如果不是从后往前扫,而是从前往后扫,curmax保存的是0-i的最大值。而我们要找的是i+1~n的最大值。

              2.最大收益即之后postprofit和当前的curmax-price做对比。

    整个算法是这样的:是把整个数组分两段Max( MaxProfitOn[0,i] + MaxProfitOn[i+1, n-1] ),分别找最大,看看他们的和在如何分的时候才能达到全局最大。

      

    最后遍历preprofit和postprofit数组,max{preprofit[i]+postprofit[i]}既是最大的可能收益。

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int size=prices.size();
            if (size<2)
                return 0;
            int curmin=prices[0],curmax=prices[size-1];
            vector<int> preProfit(size,0);
            vector<int> postProfit(size,0);
            for(int i=1;i<size;i++){
                curmin=min(curmin,prices[i]);
                preProfit[i]=max(prices[i]-curmin,preProfit[i-1]);
            }
            for (int j = size-2;j>=0; --j){
                curmax=max(curmax,prices[j]);
                postProfit[j]=max(postProfit[j+1],curmax-prices[j]);
    
            }
            int retProfit=0;
            for(int m=0;m<size;m++){
                if(retProfit<(preProfit[m]+postProfit[m]))
                    retProfit=preProfit[m]+postProfit[m];
            }
            return retProfit;
        }
    };
  • 相关阅读:
    进度条
    html5 表单新增事件
    html5 表单的新增type属性
    html5 表单的新增元素
    html5 语义化标签
    jq 手风琴案例
    codeforces 702D D. Road to Post Office(数学)
    codeforces 702C C. Cellular Network(水题)
    codeforces 702B B. Powers of Two(水题)
    codeforces 702A A. Maximum Increase(水题)
  • 原文地址:https://www.cnblogs.com/LUO77/p/5631768.html
Copyright © 2011-2022 走看看