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 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有交易费的买卖股票的更多相关文章
- 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 ...
- 714. Best Time to Buy and Sell Stock with Transaction Fee
问题 给定一个数组,第i个元素表示第i天股票的价格,可执行多次"买一次卖一次",每次执行完(卖出后)需要小费,求最大利润 Input: prices = [1, 3, 2, 8, ...
- [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 ...
- 【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 ...
- 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- SharePoint2013切换帐户登录菜单显示
SharePoint2013帐户姓名显示的地方没有切换帐户的菜单,这个功能对于终端用户是可有可无的,但对于sharepoint管理员或sharepoint开发人员来讲,却是一个很重要的菜单,由于经常要 ...
- SVN checkout 出的项目在PHPstorm中打开没有subversion(SVN)选项按钮怎么办?
对于svn add 命令的批量操作,为了操作简便还是习惯在IDE中完成,有时候新checkout出的项目,在PHPstorm中右键菜单中没有 Subversion 按钮,操作如下: 点击VCS按钮,然 ...
- JAVA-Unit05: 视图、序列、索引 、 约束
Unit05: 视图.序列.索引 . 约束 视图 数据库对象之一 视图在SQL语句中体现的角色与表相同, 但它并非一张真实存在的表,它对应的 是一个查询语句的结果集. 创建一个查看10号部门员工信息的 ...
- DomHelper
public class DomHelper { public static ArrayList<Person> queryXML(Context context) { ArrayList ...
- NFS的安装以及windows/linux挂载linux网络文件系统NFS
1.创建linux的NFS服务端安装centos6.4,关闭防火墙/etc/init.d/iptables status yum install nfs-utils rpcbind [root@lin ...
- 【游记】noip2017酱油记
2017-10-14:初赛 12:00:比赛时间14:30-16:30,由于比赛地点在二附中,我12点就坐上了地铁(无力吐槽,周六中午人还那么多,站了一路). 13:45:到了efz,所有人都被拦在了 ...
- linux 利用nethogs查看某进程的网卡流量
一.nethogs介绍 分享一个linux 下检测系统进程占用带宽情况的检查.来自github上的开源工具. 它不依赖内核中的模块.当我们的服务器网络异常时,可以通过运行nethogs程序来检测是那个 ...
- python学习笔记(六):常用模块
一.模块.包 什么是模块? 模块实质上就是一个python文件,它是用来组织代码的,意思就是说把python代码写到里面,文件名就是模块的名称,test.py test就是模块名称. 什么是包? 包, ...
- RESTful基础知识
RESTful简介 互联网软件的架构原则,定名为REST,即Representational State Transfer的缩写.翻译是"表现层状态转化". 如果一个架构符合RES ...
- 为什么要使用叶脊(leaf-spine)拓扑网络?
三层网络设计的结构发展已经很成熟,但leaf-spine (leaf叶节点,spine脊节点)结构越来越热门,网络设计师们应该如何进行选择呢? 尽管三层网络结构应用广泛而且技术成熟,但随着技术的发展, ...