zoukankan      html  css  js  c++  java
  • LeetCode 买卖股票的最佳时机

    121. 买卖股票的最佳时机

    题目描述:

    解法一:

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int n=prices.size();
            if(n<2) return 0;
            int minp=prices[0];
            int res=0;
            for(int i=1;i<n;i++){
                if(prices[i]>minp){
                    res=max(res,prices[i]-minp);
                }
                else 
                    minp=prices[i];
            }
            return res;
        }
    };

    解法二(传统动态规划):

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int n=prices.size();
            if(n<2) return 0;
            int dp[n][2];
            for(int i=0;i<n;i++){
                if(i==0){
                    dp[i][0]=0;
                    dp[i][1]=-prices[i];
                }
                else{
                    dp[i][0]=max(dp[i-1][0],dp[i-1][1]+prices[i]);
                    dp[i][1]=max(dp[i-1][1],-prices[i]);
                }
            }
            return dp[n-1][0];
        }
    };

    解法三(改进动态规划):

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int dp_i0=0,dp_i1=INT_MIN;
            for(int i=0;i<prices.size();i++){
                dp_i0=max(dp_i0,dp_i1+prices[i]);
                dp_i1=max(dp_i1,-prices[i]);
            }
            return dp_i0;
        }
    };

    122. 买卖股票的最佳时机 II

    题目描述:

    解法一:

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int n=prices.size();
            int i=0,j;
            int res=0;
            while(i<n-1){
                while(i<n-1&&prices[i]>=prices[i+1]) i++;    //谷
                j=i;
                while(j<n-1&&prices[j]<=prices[j+1]) j++;     //峰
                res+=prices[j]-prices[i];
                i=j;
            }
            return res;
        }
    };
    
    
    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int n=prices.size();
            if(n<2) return 0;
            int maxval=0;
            int valley=prices[0],peak=prices[0];
            int i=0;
            while(i<n-1){
                while(i<n-1&&prices[i]>=prices[i+1])
                    i++;
                valley=prices[i];                          //谷
                while(i<n-1&&prices[i]<=prices[i+1])
                    i++;
                peak=prices[i];                            //峰
                maxval+=peak-valley;
            }
            return maxval;
        }
    };

    解法二:

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int n=prices.size();
            int res=0;
            for(int i=0;i<n-1;i++){
                if(prices[i]<prices[i+1]){
                    res+=prices[i+1]-prices[i];                //分段加
                }
            }
            return res;
        }
    };

    解法三(传统动态规划):

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int n=prices.size();
            if(n<2) return 0;
            int dp[n][2];
            for(int i=0;i<n;i++){
                if(i==0){
                    dp[i][0]=0;
                    dp[i][1]=-prices[i];
                }
                else{
                    dp[i][0]=max(dp[i-1][0],dp[i-1][1]+prices[i]);
                    dp[i][1]=max(dp[i-1][1],dp[i-1][0]-prices[i]);
                }
            }
            return dp[n-1][0];
        }
    };

    解法四(改进动态规划):

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int dp_i0=0,dp_i1=INT_MIN;
            for(int i=0;i<prices.size();i++){
                int temp=dp_i0;
                dp_i0=max(dp_i0,dp_i1+prices[i]);
                dp_i1=max(dp_i1,temp-prices[i]);
            }
            return dp_i0;
        }
    };

    123. 买卖股票的最佳时机 III

    题目描述:

    解法一(最初的动态规划):

    int maxProfit(vector<int>& prices) {
    	int n = prices.size();
    	vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(3, vector<int>(2, 0)));
    	dp[0][0][1] = dp[0][1][1] = dp[0][2][1] = INT_MIN;
    	for (int i = 1; i<n + 1; i++) {
    		for (int k = 1; k<3; k++) {
    			dp[i][k][0] = max(dp[i - 1][k][0], dp[i - 1][k][1] + prices[i-1]);
    			dp[i][k][1] = max(dp[i - 1][k][1], dp[i - 1][k-1][0] - prices[i-1]);
    		}
    	}
    	return dp[n][2][0];
    }

    解法二(进一步的动态规划):

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int n = prices.size();
            if(n<2) return 0;
            vector<vector<vector<int>>> dp(n, vector<vector<int>>(3, vector<int>(2, 0)));
            for (int i = 0; i<n; i++) {
                for (int k = 1; k<3; k++) {
                    if (i == 0) {
                        dp[i][k][0] = 0;
                        dp[i][k][1] = -prices[i];
                    }
                    else {
                        dp[i][k][0] = max(dp[i - 1][k][0], dp[i - 1][k][1] + prices[i]);
                        dp[i][k][1] = max(dp[i - 1][k][1], dp[i - 1][k - 1][0] - prices[i]);
                    }
                }
            }
            return dp[n - 1][2][0];
        }
    };
    
    
    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int n=prices.size();
            if(n<2) return 0;
            int dp[n][3][2];
            memset(dp,0,sizeof(dp));
            for(int i=0;i<n;i++){
                for(int k=1;k<3;k++){
                    if(i==0){
                        dp[i][k][0]=0;
                        dp[i][k][1]=-prices[i];
                    }
                    else{
                        dp[i][k][0]=max(dp[i-1][k][0],dp[i-1][k][1]+prices[i]);
                        dp[i][k][1]=max(dp[i-1][k][1],dp[i-1][k-1][0]-prices[i]);
                    }
                }
            }
            return dp[n-1][2][0];
        }
    };

    解法三(较优的动态规划):

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int dp_i10=0,dp_i20=0,dp_i11=INT_MIN,dp_i21=INT_MIN;
            for(int i=0;i<prices.size();i++){
                dp_i20=max(dp_i20,dp_i21+prices[i]);
                dp_i21=max(dp_i21,dp_i10-prices[i]);
                dp_i10=max(dp_i10,dp_i11+prices[i]);
                dp_i11=max(dp_i11,-prices[i]);
            }
            return dp_i20;
        }
    };

    188. 买卖股票的最佳时机 IV

    解法一:

    class Solution {
    public:
        int helper(vector<int>& prices){
            int res=0;
            for(int i=1;i<prices.size();i++){
                if(prices[i]>prices[i-1])
                    res+=prices[i]-prices[i-1];
            }
            return res;
        }
        int maxProfit(int k, vector<int>& prices) {
            int n=prices.size();
            if(n<2) return 0;
            if(k>n/2) return helper(prices);
            int dp[n][k+1][2];
            memset(dp,0,sizeof(dp));
            for(int i=0;i<n;i++){
                for(int f=1;f<=k;f++){
                    if(i==0){
                        dp[i][f][0]=0;
                        dp[i][f][1]=-prices[i];
                    }
                    else{
                        dp[i][f][0]=max(dp[i-1][f][0],dp[i-1][f][1]+prices[i]);
                        dp[i][f][1]=max(dp[i-1][f][1],dp[i-1][f-1][0]-prices[i]);
                    }
                }
            }
            return dp[n-1][k][0];
        }
    };

    解法二:

    class Solution {
    public:
        int helper(vector<int>& prices){
            int dp_i0=0,dp_i1=INT_MIN;
            for(int i=0;i<prices.size();i++){
                int temp=dp_i0;
                dp_i0=max(dp_i0,dp_i1+prices[i]);
                dp_i1=max(dp_i1,temp-prices[i]);
            }
            return dp_i0;
        }
        int maxProfit(int k, vector<int>& prices) {
            int n=prices.size();
            if(n<2) return 0;
            if(k>n/2) return helper(prices);
            int dp[n][k+1][2];
            memset(dp,0,sizeof(dp));
            for(int i=0;i<n;i++){
                for(int m=1;m<=k;m++){
                    if(i==0){
                        dp[i][m][0]=0;
                        dp[i][m][1]=-prices[i];
                    }
                    else{
                        dp[i][m][0]=max(dp[i-1][m][0],dp[i-1][m][1]+prices[i]);
                        dp[i][m][1]=max(dp[i-1][m][1],dp[i-1][m-1][0]-prices[i]);
                    }
                }
            }
            return dp[n-1][k][0];
        }
    };

    309. 最佳买卖股票时机含冷冻期

    题目描述:

    解法一:

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int n=prices.size();
            if(n<2) return 0;
            int dp_i0=0,dp_i1=INT_MIN;
            int dp[n];
            for(int i=0;i<n;i++){
                if(i<2){
                    dp_i0=max(dp_i0,dp_i1+prices[i]);
                    dp_i1=max(dp_i1,-prices[i]);
                    dp[i]=dp_i0;
                }
                else{
                    dp_i0=max(dp_i0,dp_i1+prices[i]);
                    dp_i1=max(dp_i1,dp[i-2]-prices[i]);
                    dp[i]=dp_i0;;
                }
            }
            return dp_i0;
        }
    };

    解法二:

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int dp_i0=0,dp_i1=INT_MIN;
            for(int i=0;i<prices.size();i++){
                int temp=dp_i0; 
                dp_i0=max(dp_i0,dp_i1+prices[i]);
                dp_i1=max(dp_i1,temp-prices[i]);
            }
            return dp_i0;
        }

    714. 买卖股票的最佳时机含手续费

    题目描述:

    解法:

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int dp_i0=0,dp_i1=INT_MIN;
            for(int i=0;i<prices.size();i++){
                int temp=dp_i0; 
                dp_i0=max(dp_i0,dp_i1+prices[i]);
                dp_i1=max(dp_i1,temp-prices[i]);
            }
            return dp_i0;
        }
    };

    参考:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/solution/yi-ge-fang-fa-tuan-mie-6-dao-gu-piao-wen-ti-by-l-3/

    讲的很好,感谢!

  • 相关阅读:
    青春如同奔流的江河,一去不回来不及道别
    关于RESOURCE_SEMAPHORE等待类型
    sql server 2008 The fulltext filter daemon host (FDHost) process has stopped abnormally.
    [转]SQL Server 2008 R2 Pricing
    64bit sql server 2008 sp1 使用lock pages in memory 具体操作
    [转]Fun with Locked Pages, AWE, Task Manager, and the Working Set…
    关掉和开启win7的Windows Search服务
    How to recreate the msdb database in SQL Server 2005
    Kimberly L. Tripp的sp_helpindex2 for sql server 2005/2008,并修复了for sqk2k8的一个bug
    表无索引为什么sp_spaceused 中的index_size不为0
  • 原文地址:https://www.cnblogs.com/oneDongHua/p/14263989.html
Copyright © 2011-2022 走看看