Java实现 LeetCode 714 买卖股票的最佳时机含手续费(动态规划 || 迭代法)
714. 买卖股票的最佳时机含手续费
给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。
你可以无限次地完成交易,但是你每次交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。
返回获得利润的最大值。
示例 1:
输入: prices = [1, 3, 2, 8, 4, 9], fee = 2
输出: 8
解释: 能够达到的最大利润:
在此处买入 prices[0] = 1
在此处卖出 prices[3] = 8
在此处买入 prices[4] = 4
在此处卖出 prices[5] = 9
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
注意:
0 < prices.length <= 50000.
0 < prices[i] < 50000.
0 <= fee < 50000.
class Solution {
// public int maxProfit(int[] prices, int fee) {
// int size = prices.length;
// if (size <= 1){
// return 0;
// }
// int[][] dp = new int[size+1][2];
// dp[0][0] = 0; dp[0][1]= Integer.MIN_VALUE;
//dp【】【0】就是手里没有股票反之dp[][1]就是手里有股票
// for(int day = 0; day < size; day++){
// dp[day+1][0] = dp[day][0];
// if (day > 0 && dp[day][1] != Integer.MIN_VALUE){
// dp[day+1][0] = Math.max(dp[day+1][0], dp[day][1] + prices[day] - prices[day-1] - fee);
// }
// dp[day+1][1] = dp[day][0];
// if (day > 0 && dp[day][1] != Integer.MIN_VALUE){
// dp[day+1][1] = Math.max(dp[day+1][1], dp[day][1] + prices[day] - prices[day-1]);
// }
// }
// return dp[size][0];
// }
public int maxProfit(int[] prices, int fee) {
int length = prices.length;
int keepMax = -prices[0];
int notKeepMax = 0;
for (int i = 1; i < length; i++) {
int keepMaxYesterday = keepMax;
keepMax = Math.max(keepMax, notKeepMax - prices[i]);
notKeepMax = Math.max(notKeepMax, keepMaxYesterday + prices[i] - fee);
}
return notKeepMax;
}
}
Java实现 LeetCode 714 买卖股票的最佳时机含手续费(动态规划 || 迭代法)的更多相关文章
- LeetCode——714. 买卖股票的最佳时机含手续费.
给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 :非负整数 fee 代表了交易股票的手续费用. 你可以无限次地完成交易,但是你每次交易都需要付手续费.如果你已经购买了一个 ...
- leetcode 714. 买卖股票的最佳时机含手续费
继承leetcode123以及leetcode309的思路,,但应该也可以写成leetcode 152. 乘积最大子序列的形式 class Solution { public: int maxProf ...
- 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】714、买卖股票的最佳时机含手续费
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
- Java实现 LeetCode 188 买卖股票的最佳时机 IV
188. 买卖股票的最佳时机 IV 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 k 笔交易. 注意: 你不能同时参与多 ...
- Java实现 LeetCode 123 买卖股票的最佳时机 III(三)
123. 买卖股票的最佳时机 III 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与 ...
- Java实现 LeetCode 122 买卖股票的最佳时机 II
122. 买卖股票的最佳时机 II 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意: ...
- Java实现 LeetCode 121 买卖股票的最佳时机
121. 买卖股票的最佳时机 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不 ...
- [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 ...
随机推荐
- [CodeForces 344C Rational Resistance]YY,证明
题意:给若干个阻值为1的电阻,要得到阻值为a/b的电阻最少需要多少个. 思路:令a=mb+n,则a/b=m+n/b=m+1/(b/n),令f(a,b)表示得到a/b的电阻的答案,由f(a,b)=f(b ...
- git切换账号
有的时候我们有两个甚至多个git账号(公司的git账号和自己的github),为了不混淆提交,我们需要在提交之前查看自己的git账号必要时进行切换. 查看当前git用户名: git config us ...
- spring中<context:property-placeholder/>一个坑
<context:property-placeholder location="classpath:db.properties" ></context:prope ...
- python之logging基础入门
博客学习至:https://www.cnblogs.com/Nicholas0707/p/9021672.html#_label0 https://www.cnblogs.com/dream66/p/ ...
- Dotnet core使用JWT认证授权最佳实践(二)
最近,团队的小伙伴们在做项目时,需要用到JWT认证.遂根据自己的经验,整理成了这篇文章,用来帮助理清JWT认证的原理和代码编写操作. 第一部分:Dotnet core使用JWT认证授权最佳实践(一) ...
- Appium自动化(7) - 控件定位工具之Appium 的 Inspector
如果你还想从头学起Appium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1693896.html 前言 上一篇文章介绍了另一块控件定 ...
- 整理今天js留下的作业(点击换图片换首页背景图)
<div class="buttons"> <button id="katong">卡通</button> <b ...
- xshell密钥登录服务器
其实很简单 1 xshell 生成pub key . 在工具 -> 用户密钥管理. 生成 .另存为id_rsa_1024.pub 2.服务器上ssh-keygen 3.将生成的文件id_rsa_ ...
- MyEclipse安装后的配置
一.Window-->Preferences-->General --> Workspace --> UTF-8 作用:从此以后,你创建的任何项目编码都是UTF-8,一次解决所 ...
- mysql运维入门1:基础及备份还原
存储引擎 myisam 表强调的是性能 执行速度比innodb类型更快 不提供事务支持 如果执行大量的select操作,是首选 支持表锁,不支持行锁 innodb 提供事务支持.外键等高级数据库功能 ...