作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/

题目描述

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee.

You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)

Return the maximum profit you can make.

Example 1:
Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
Output: 8
Explanation: The maximum profit can be achieved by:
Buying at prices[0] = 1
Selling at prices[3] = 8
Buying at prices[4] = 4
Selling at prices[5] = 9
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

Note:

  • 0 < prices.length <= 50000.
  • 0 < prices[i] < 50000.
  • 0 <= fee < 50000.

题目大意

给出一系列的股票交易价格,每次股票交易会有交易fee,求买卖股票能获得的最大的收益。

解题方法

动态规划

可以使用dp的方法去做。该dp使用了两个数字,cash和hold。解释如下:

  1. cash 该天结束手里没有股票的情况下,已经获得的最大收益
  2. hold 该天结束手里股票的情况下,已经获得的最大收益

所以转移状态分析如下:

cash 更新的策略是:既然今天结束之后手里没有股票,那么可能是今天没买(保持昨天的状态),也可能是今天把股票卖出了

hold 更新的策略是:今天今天结束之后手里有股票,那么可能是今天没卖(保持昨天的状态),也可能是今天买了股票

class Solution:
def maxProfit(self, prices, fee):
"""
:type prices: List[int]
:type fee: int
:rtype: int
"""
cash = 0
hold = -prices[0]
for i in range(1, len(prices)):
cash = max(cash, hold + prices[i] - fee)
hold = max(hold, cash - prices[i])
return cash

使用C++代码如下:

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

优化空间复杂度到O(1)的代码如下:

class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
const int N = prices.size();
// max profit if today don't have stock
int cash = 0;
// max profit if today have stock
int hold = -prices[0];
for (int i = 1; i < N; ++i) {
cash = max(cash, prices[i] + hold - fee);
hold = max(hold, cash - prices[i]);
}
return cash;
}
};

日期

2018 年 4 月 10 日 —— 风很大,很舒服~~
2018 年 12 月 5 日 —— 周三啦!
2019 年 2 月 22 日 —— 这周结束了

【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)的更多相关文章

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

  2. Week 7 - 714. Best Time to Buy and Sell Stock with Transaction Fee & 718. Maximum Length of Repeated Subarray

    714. Best Time to Buy and Sell Stock with Transaction Fee - Medium Your are given an array of intege ...

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

    问题 给定一个数组,第i个元素表示第i天股票的价格,可执行多次"买一次卖一次",每次执行完(卖出后)需要小费,求最大利润 Input: prices = [1, 3, 2, 8, ...

  4. 【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 ...

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

  6. Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee)

    Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee) 股票问题: 1 ...

  7. [LeetCode] 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 ...

  8. LeetCode Best Time to Buy and Sell Stock with Transaction Fee

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/descripti ...

  9. [Swift]LeetCode714. 买卖股票的最佳时机含手续费 | 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 ...

随机推荐

  1. Excel—在Excel中利用宏定义实现MD5对字符串(如:手机号)或者文件加密

    下载宏文件[md5宏] 加载宏 试验md5加密 可能遇到的问题 解决办法 下载宏文件[md5宏] 下载附件,解压,得md5宏.xla md5宏.zip 加载宏 依次打开[文件]-[选项]-[自定义功能 ...

  2. 使用clion阅读eos源码

    配置mingw 安装clion 从github克隆源码 使用clion open打开 在cmake上使用boost: sudo apt-get install libboost-all-dev

  3. Android 高级UI组件(一)GridView与ListView

    1.GridView 1.GridView学习 GridView和ListView都是比较常用的多控件布局,而GridView更是实现九宫图的首选 main.xml: <?xml version ...

  4. 【Linux】【Basis】【RHEL】KickStart for RHEL6.8

    1. 概念: 自动安装的脚本,这篇文章以RHEL6.8为例 kickstart for RHEL6.8官方教程:https://access.redhat.com/documentation/en-U ...

  5. java关键字volatile内存语义详细分析

    volatile变量自身具有下列特性. 1.可见性.对一个volatile变量的读,总是能看到(任意线程)对这个volatile变量最后的写 入. · 2.原子性:对任意单个volatile变量的读/ ...

  6. Jenkins凭证管理

    目录 一.简介 二.管理凭证 三.常用凭证 保密文本 账号密码 保密文件 账号秘钥 四.优雅使用凭证 保密文本 账号密码 保密文件 五.凭证插件 集成HashiCorp Vault pipeline ...

  7. react-hook简单使用

    一.函数式组件创建 function HelloComponent(props, /* context */) { return <div>Hello {props.name}</d ...

  8. linux下编译php扩展

    1 在pecl.php.net搜索你需要的php扩展 2 在解压后的扩展目录运行phpize 3 执行编译./configure --with-php-config=/usr/local/php/bi ...

  9. 【教程】OBS直播推流教程(Windows & macOS)

    OBS Open Broadcaster Software | OBS (obsproject.com) Windows直播推流教程 Windows下OBS直播推流非常简单,本教程将会介绍,具体步骤如 ...

  10. 资源分配单位(Project)

    <Project2016 企业项目管理实践>张会斌 董方好 编著 那些分配了资源的任务,其中的资源是有数量单位的,默认工时单位是100%,材料单位是1. 比如某吃货,为了完成吃米饭这一任务 ...