[抄题]:

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. CF 1093G Multidimensional Queries——线段树(消去绝对值符号)

    题目:http://codeforces.com/contest/1093/problem/G 只好看看题解:https://codeforces.com/blog/entry/63877 主要是把绝 ...

  2. bzoj 1001 [BeiJing2006]狼抓兔子——最小割转最短路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 #include<cstdio> #include<cstring& ...

  3. MyEclipse自动生成Ant Build.xm

    关于MyEclipse 自动生成 Ant Build.xml 配置文件,生成的方法很隐蔽,是自己无意中找到的.选择你要生成Build.xml文件的项目,右键. Export-> General ...

  4. 如何在项目中使用requireJS

    本文主要讲讲如何在一个项目中合理的使用requireJS,来加载js代码,这里是个实际例子,讲解不多, 看详情请连接到:http://www.cnblogs.com/evaling/p/6722760 ...

  5. 编译openvpn在链接时报tuncfg错误

    1. 链接时产生tuncfg错误 init.o: In function `do_persist_tuntap': init.c:(.text+0x24d8): undefined reference ...

  6. Java并发-Runnable、Callable、Future、Future Task

    Runnable: Runnable的代码非常简单,他是一个接口,且接口中只有一个方法,run(),创建一个类实现他,把一些费时操作写在其中,然后使用某个线程去执行该Runnable实现类即可实现多线 ...

  7. Eclipse中设置JDK、${user}变量

    为eclipse设置jdk方法: 两个方法: 1.设置PATH路径-eclipse自动会查找! 2.在快捷方式中加上参数:-VM java虚拟机路径 Eclipse中设置${user}变量 在Ecli ...

  8. [Java][Web]利用 referer 防盗链

    String referer = request.getHeader("referer"); if(referer == null || !referer.startsWith(& ...

  9. 怎样优化CPU

    大家写好的代码,在浏览器上运行,总会有怎样才能让他效率更高,不卡顿...等问题,就本人而言,我觉得是以下这几个导致CPU 过高 1.不要直接监听scroll,等到鼠标滚动停止的时候再去触发事件2.控制 ...

  10. hadoop map任务Combiner被调用的源码逻辑简要分析

      从MapTask类中分析下去,看一下map任务是如何被调用并执行的.   入口方法是MapTask的run方法,看一下run方法的相关介绍:   org.apache.hadoop.mapred. ...