[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

基本设定是:卖会赚钱,买会赔钱

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/Most-consistent-ways-of-dealing-with-the-series-of-stock-problems

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int maxProfit(int[] prices, int fee) {
//ini: T_ik0, T_ik(1, T_ik0_old
int T_ik0 = 0, T_ik1 = Integer.MIN_VALUE; //for loop
for (int price : prices) {
int T_ik0_old = T_ik0;
T_ik0 = Math.max(T_ik0, T_ik1 + price);
T_ik1 = Math.max(T_ik1, T_ik0_old - price - fee);
} return T_ik0;
}
}

714. Best Time to Buy and Sell Stock with Transaction Fee有交易费的买卖股票的更多相关文章

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

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

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

  3. [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 ...

  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. 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

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

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

随机推荐

  1. Oracle安装-------实例化EM 配置文件时出错问题 ( 转 )

    命令行中执行: C:\Users\Administrator>emca -repos drop   EMCA 开始于 2013-4-23 20:13:58 EM Configuration As ...

  2. bzoj 1927 [Sdoi2010]星际竞速——网络流

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1927 每个点拆点保证只经过一次. 主要是如果经过了这个点,这个点应该向汇点流过去表示经过了它 ...

  3. bzoj 4010 [HNOI2015]菜肴制作——贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4010 和 bzoj 2535 差不多.因为当前怎么决策与该点后面连的点的标号情况有关,所以按 ...

  4. ( 转)mappingResource属性和mappingDirectoryLocations属性的使用

    在Spring的applicationContext.xml中配置映射文件,通常是在这个Bean实例中进行的,若配置的映射文件较少时,可以用sessionFactory的所属类LocalSession ...

  5. Request.UrlReferrer详解

    使用前需要进行判断: if (Request != null && Request.UrlReferrer != null && Request.UrlReferrer ...

  6. 搭建基于hyperledger fabric的联盟社区(四) --chaincode开发

    前几章已经分别把三台虚拟机环境和配置文件准备好了,在启动fabric网络之前我们要准备好写好的chaincode.chaincode的开发一般是使用GO或者JAVA,而我选择的是GO语言.先分析一下官 ...

  7. JEECG获取当前登录人的值

    TSUser user = ResourceUtil.getSessionUserName(); mv.addObject("fbillerid", user.getUserNam ...

  8. 64位windows系统安装javaee6.0不成功解决方案

    64位windows系统安装javaee6.0不成功解决方案 (2013-01-19 14:59:51) 转载▼ 标签: 杂谈   could not find the required versio ...

  9. TCP 、UDP、IP包的最大长度

    1.概述 首先要看TCP/IP协议,涉及到四层:链路层,网络层,传输层,应用层. 其中以太网(Ethernet)的数据帧在链路层 IP包在网络层 TCP或UDP包在传输层 TCP或UDP中的数据(Da ...

  10. 保证service不被杀死的方法

    Service设置成START_STICKY kill 后会被重启(等待5秒左右),重传Intent,保持与重启前一样 提升service优先级 在AndroidManifest.xml文件中对于in ...