Leetcode714.Best Time to Buy and Sell Stock with Transaction Feed动态规划

原创 2018年04月16日 20:31:30

虽然不是自己想出来的,还是做个总结吧,一个比较简单的动态规划。题目大意就是,选择合适的时间买卖股票让利润最大。

这个题可以用动态规划解决。每一天结束,两个状态,要么有股票,要么没有股票。所以我们用两个变量,cash表示当天结束手里没有股票的最大利润,hold表示当天结束手上有股票最大利润。那么初值就是cash=0;hold=-prices[0];

那么状态是如何转移的呢?

如果当天结束手上没股票,cash也是由由两种情况推出

    i.今天什么也没干,依然是上次的cash。

    ii. 今天把股票卖出来,cash=hold+prices[i]-fee(别忘了每卖一次股票都有手续费)

这两种情况取较大值就可以。

反之,如果当天结束手上有股票,分析方法一模一样。

上代码了,详见AC代码:

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

Leetcode算法学习日志-714 Best Time to Buy and Sell Stock with Transaction Fee

Leetcode 714 Best Time to Buy and Sell Stock with Transaction Fee 题目原文 Your are given an array o...
  • Zarlove
  • Zarlove
  • 2017-10-23 21:16:05
  • 1801

weekly contest 55 Best Time to Buy and Sell Stock with Transaction Fee

题目Your are given an array of integers prices, for which the i-th element is the price of a given sto...
  • weixin_38739799
  • weixin_38739799
  • 2017-10-23 14:21:20
  • 582

LeetCode题解系列--714. Best Time to Buy and Sell Stock with Transaction Fee

描述Your are given an array of integers prices, for which the i-th element is the price of a given sto...
  • bowen_wu_sysu
  • bowen_wu_sysu
  • 2017-12-13 16:39:53
  • 131

leetcode【121+122+123 best time to buy and sell stock】【python】

我们先拿出来前三道题,因为他们都是array中的题目。这是leetcode种经典的一系列题,涉及到动态规划和贪心算法。按照我的理解,贪心是满足当前条件的最优值我们就将它最为最优解,也就是大家说的局部最...
  • u014251967
  • u014251967
  • 2016-09-12 21:04:35
  • 1149

714. Best Time to Buy and Sell Stock with Transaction Fee

Your are given an array of integers prices, for which the i-th element is the price of a given stock...
  • zjucor
  • zjucor
  • 2017-10-23 10:12:46
  • 827

【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求...
  • ljiabin
  • ljiabin
  • 2015-04-06 11:10:34
  • 22523

LWC 55:714. Best Time to Buy and Sell Stock with Transaction Fee

LWC 55:714. Best Time to Buy and Sell Stock with Transaction Fee传送门:714. Best Time to Buy and Sell S...
  • u014688145
  • u014688145
  • 2017-10-23 12:03:39
  • 1239

【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Say you have a...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-14 06:16:32
  • 7948

Best Time to Buy and Sell Stock -- LeetCode

原题链接: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/  这道题求进行一次交易能得到的最大利润。如果用brute ...
  • linhuanmars
  • linhuanmars
  • 2014-04-09 05:33:32
  • 17658

【LeetCode】【Python题解】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...
  • u011613729
  • u011613729
  • 2014-07-24 10:11:44
  • 3572
收藏助手
不良信息举报
您举报文章:Leetcode714.Best Time to Buy and Sell Stock with Transaction Feed动态规划
举报原因:
原因补充:

(最多只允许输入30个字)