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

    class Solution {
    public:
        int maxProfit(vector<int> &prices) {
            int len = prices.size();
            if (len < 1) return 0;
            int minv = prices[0];
            int maxv = prices[0];
            int mprofit = 0;
            for (int i=1; i<len; i++) {
                int cur = prices[i];
                if (cur < minv) {
                    minv = cur;
                    maxv = cur;
                }
                if (cur > maxv) {
                    maxv = cur;
                    if (maxv - minv > mprofit) mprofit = maxv - minv;
                }
            }
            
            return mprofit;        
        }
    };

    这个解法应该算是贪婪吧,还可以把后缀序列的最大值求出来,然后在扫一边每个元素就可以知道如果当前买入,在后面的最高价是多少,从而求出最高利润,如果是负值则不进行交易,利润为0,下面给出dp版本的代码

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

    两者都是O(n)时间,只不过后者用了额外空间

    第二轮:

    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.

    class Solution {
    public:
        int maxProfit(vector<int> &prices) {
            int len = prices.size();
            if (len < 2) return 0;
            
            vector<int> H(len, 0);
            H[len-1] = prices[len-1];
            for (int i=len-2; i>=0; i--) {
                H[i] = max(H[i+1], prices[i]);
            }
            
            int maxp = 0;
            for (int i=0; i<len; i++) {
                maxp= max(maxp, H[i] - prices[i]);
            }
            return maxp;
        }
    };
    class Solution {
    public:
        int maxProfit(vector<int> &prices) {
            int len = prices.size();
            if (len < 2) return 0;
            int maxv = prices[0];
            int minv = prices[0];
            
            int maxp = 0;
            
            for (int i=1; i<len; i++) {
                if (prices[i] < minv) {
                    minv = maxv = prices[i];
                }
                maxv = max(maxv, prices[i]);
                maxp = max(maxp, maxv - minv);
            }
            return maxp;
        }
    };
  • 相关阅读:
    使用pwn_deploy_chroot部署国赛pwn比赛题目
    《Java程序设计》第十一章 JDBC与MySQL数据库
    使用commons.cli实现MyCP
    2018-2019-2 20175211 实验二《Java面向对象程序设计》实验报告
    结对编程练习_四则运算(第二周)
    20175211 2018-2019-2 《Java程序设计》第六周学习总结
    20175211 2017-2018-2 《Java程序设计》第六周学习记录(2)
    海思Hi35xx平台调试笔记
    ffmpeg,rtmpdump和nginx rtmp实现录屏,直播和录制
    文件传输(xmodem协议)
  • 原文地址:https://www.cnblogs.com/lailailai/p/3738386.html
Copyright © 2011-2022 走看看