Leetcode(W8):121. Best Time to Buy and Sell Stock(动态规划)

来源:互联网 发布:中国网络日报。 编辑:程序博客网 时间:2024/05/16 15:21

leetcode中Best Time to Buy and Sell Stock买卖股票的最佳时间总共有5道类型题。


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

题意:

给定一个数组,下标代表第几天,元素值即为当天可买进或卖出的股票价格,现在设定我们最多只能进行一次交易(即一次买一次卖,后面的题目会在这个条件做各种变化),谋取最大利益然后输出这个最大利益。

举例:

Input: [7, 1, 5, 3, 6, 4]
Output: 5
第二天的1块钱买进,第五天6块钱卖出,赚5。
例2:
Input: [7, 6, 4, 3, 1]
Output: 0
股票一路下跌,所以只能当天买当天卖,赚0。

思路:

最暴力的版本就是直接枚举所有可能的情况分析了,即两重循环,第i天买进,第j天卖出,然后i从第一个元素遍历到最后一个元素,j从i遍历到最后一个元素,每次计算array[j]-array[i]的值,最后挑一个最大的保存。
然而不难发现这种算法有很多多余的计算,比如如果你已经算过买入价格为3的卖出情况了,那么今天买入价格为5的还有必要算吗,所以算法其实能剪枝的地方还有很多。
这里要讲的是第二种方法,用动态规划,还是遍历数组,但是这次只需要一重循环,我们假设在第i天能赚到的最大利益为a[i-1],那么只需要计算出i到最后一天时的a[i]即可,很明显a[0]为0,a[1]为max(a[0],第二天作为卖出-(前两天的最小值)作为卖出),a[2]为max(a[1],第三天作为卖出-(前三天的最小值)作为卖出)……在后面的文章中会谈到一种算法,将这里所说的“第X天作为卖出-(前X天的最小值)作为卖出”作为一个新的数组,这样能方便计算当题目条件为“能进行K次交易”时的情景。

操作:

在一重循环中每次都先找出个前X天的最小值curmin,然后计算array[i]-curmin的值,每次取计算的最大值作为最终结果即可。

2. 代码

暴力法

class Solution {public:    int maxProfit(vector<int>& prices) {        int result = 0;        for (int i = 0; i < prices.size(); i++) {            for (int j = i+1; j < prices.size(); j++) {                int temp = prices[j] - prices[i];                if (temp > result) {                    result = temp;                }            }        }        return result;    }};

动态规划法

class Solution {public:    int maxProfit(vector<int>& prices) {        if (prices.size() < 2) return 0;        int curmin = prices[0];        int result = 0;        for (int i = 0; i < prices.size(); i++) {            curmin = min(curmin, prices[i]);            result = max(result, prices[i] - curmin);        }        return result;    } };
阅读全文
0 0
原创粉丝点击