[抄题]:

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. java根据特定密钥对字符串进行加解密

    package com.test; import java.io.IOException; import java.security.SecureRandom; import javax.crypto ...

  2. mybatis 面试

    1.接口绑定有几种实现方式,分别是怎么实现的? 接口绑定有两种实现方式,一种是通过注解绑定,就是在接口的方法上面加上 @Select@Update等注解里面包含Sql语句来绑定, 另外一种就是通过xm ...

  3. 【转】JMeter-Java Sampler编写范例

    今天,听段念的培训,学了一下Jmeter写Java请求的过程. 根据某博文,修改其中代码的bug后,贴在下面吧:另外公司一同学Raylupas在半年前也写过关于JMeter写Java Sampler的 ...

  4. 1102 Invert a Binary Tree

    题意:给定一个二叉树,要求输出翻转后的二叉树的层序序列和中序序列. 思路:不用真的翻转,只需要在输出时先访问右结点再访问左结点即可. 代码: #include <cstdio> #incl ...

  5. 02:Sysbench基准压测(oltp_update_index.lua、oltp_update_non_index.lua)my.cnf

    目录 Sysbench 基准压测 my.cnf 一.Sysench测试前准备 1.1.压测环境 二.进行OLTP_update测试 2.1.安装压测工具sysbench 2.2.执行压测 三.执行结果 ...

  6. [置顶] ubuntu版本很老,apt-get update更新失败时(W: Failed to fetch ...)------如何创建新的sources.list

    在说这个解决方案之前,我先说下,目前遇到的问题: 我使用 sudo apt-get update 之后,更新失败.具体原因如下: W: Failed to fetch http://cn.archiv ...

  7. springmvc 使用ajx上传文件 不设置form enctype

    最近在做一个小项目 碰到这个问题 解决方案如下 1.js代码如下 获取当前form 转换为formdata ajax提交到后台 var form = $("#importForm" ...

  8. apache DOCUMENT_ROOT

    问题描述:本地页面错误,+1上正常 本地及+1apache配置 <VirtualHost *:> ServerAdmin webmaster@dummy-host.example.com ...

  9. Tkinter Colors(颜色)

          Tkinter Colors: Tkinter的代表与字符串的颜色.一般有两种方式来指定Tkinter的颜色 Tkinter的代表与字符串的颜色.一般有两种方式来指定Tkinter的颜色: ...

  10. 3、数据类型一:strings

    题外: 学习过程参考三份资料:<Redis入门指南>.<Redis实战>.http://redis.io 后面的学习笔记中会引入它们的内容或代码,在这里统一说明,后面笔记中便不 ...