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

Bybit面经 这段总结描述了求职者在Bybit面试过程中的经历和相关问题,包括面试内容、求职者向面试官提问的问题以及关于Bybit的信息 阅读详情

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.

还是买卖股票的问题,不同于122题,加了一个条件,就是可以无限次买卖一支股票,但是每次卖的时候要有手续费,在有手续费的情况下考虑最大收益

思路:
因为每次卖出要手续费,所以不能看到有收益就交易了

这里需要考虑较小点买入,较大点卖出,还有手续费

参考309的with cool down的思路dp。
dp的话本来是定义两个数组,但是为了节省space,只定义两个变量

定义一个hold,即较小点买入股票,持有股票的profit
一个cash,较大点卖出股票后转化的cash流,卖出的时候要减去手续费

持有的情况,hold[0] = -prices[0],因为要拿prices[0]的cash去买,所以收益就是-prices[0],后面再持续更新
cash[0]=0,刚开始买卖同一价格不仅没有profit,还会倒贴手续费,所以选择不交易,即0

hold:
买入股票的话相当于hold[i] = cash - prices[i]
不买的话保留hold[i - 1]
取两者较大的

cash:
卖出的话相当于hold[i] + prices[i] - fee
不交易的话就保留cash[i - 1]
取两者较大的

最后返回cash

    public int maxProfit(int[] prices, int fee) {
        if(prices ==null || prices.length == 0) {
            return 0;
        }
        
        int cash = 0;
        int hold = -prices[0];
        
        for (int i = 1; i < prices.length; i++) {
            cash = Math.max(cash, hold + prices[i] - fee);
            hold = Math.max(hold, cash - prices[i]);
        }
        
        return cash;
    }
深度学习部署工程师--8(docker+TensorRT部署到推理+yolo人体检测)下 本文介绍了YOLOv5模型通过TensorRT加速部署的流程。首先需要检查磁盘空间,确保有30GB可用。文章对比了不同推理方式的性能:int8量化+TensorRT可达200FPS,纯TensorRT为166FPS,CUDA为147FPS,CPU为93FPS。重点讲解了将YOLOv5模型导出为ONNX格式的详细步骤,包括修改Detect类的forward方法以移除decode运算,使用git patch批量修改代码,安装必要的依赖库,以及通过onnx-graphsurgeon工具修改ONNX图结构。最终目标 阅读详情

相关推荐

从叶片之舞到数据之眼:净空雷达如何重塑风电安全与效率的边界

净空雷达通过激光或毫米波技术实现风机叶片与塔筒间距离的毫米级实时监测,极大提升了风电场在极端天气下的安全性与运营效率。这项技术不仅预防扫塔事故,还通过数据可视化与智能分析赋能叶片健康管理和发电优化,成为风电数字化运维的核心基础设施。

xk7n92h3p5m的博客 1171

LeetCode //C - 714. Best Time to Buy and Sell Stock with Transaction Fee

【代码】LeetCode //C - 714. Best Time to Buy and Sell Stock with Transaction Fee。

Made in Code 1014

运行 ui5 serve 命令行报错的处理方法

如果大家运行 ui5 serve 命令行,报如下的错误消息: ui5 不是内部或外部命令,也不是可运行的程序: 只需要执行命令行,全局安装 ui5 cli(command line interface)这个命令行工具即可: npm install --global @ui5/cli 这个 ui5 cli 的入口执行文件,实际安装在了我 Node.js 的安装目录下: 更多Jerry的原创文章,尽在:“汪子熙”: ...

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1368

[leetcode] 714. Best Time to Buy and Sell Stock with Transaction Fee

Description 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 comple...

Learning from the mistakes 317

uniapp - 实现 App 端 “半屏幕“ 连续扫码功能,页面只有半个屏幕是扫码框并且可持续连续扫码获取数据(不使用官方 uni.scanCode API,因为无法连续扫码!)详细示例源码插件教程

uniapp app端实现半屏幕扫码,uniapp 扫一扫半个屏幕,uniapp 扫一扫,uniapp持续扫码实现,uniapp怎么自定义扫码和连续扫码,uni-app App端半屏连续扫码,uniapp 扫一扫半个屏幕,uniapp自定义扫码页面样式,实现连续扫码功能(uniapp),uniapp - 实现 App 端 "半屏幕" 连续扫码功能,页面只有半个屏幕是扫码框并且可持续连续扫码获取数据(不使用官方 uni.scanCode API,因为无法连续扫码!)详细示例源码插件教程。

✅ 精选推荐文章 1万+

深度学习实战12(进阶版)-利用Dewarp实现文本扭曲矫正

大家好,我是微学A1, 我们在生活中会看到一些拍摄扭曲的图片,我们在通过OCR识别的时候,因为扭曲的厉害,而无法识别,我们需要对图片进行处理。

微学AI的博客 3170

【leetcode笔记】Python实现 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 m...

Jack_Kuo的博客 561

恶劣天气下的分割鲁棒性:雨雪雾干扰消除

恶劣天气下的分割鲁棒性:雨雪雾干扰消除​ ,人工智能,计算机视觉,大模型,AI,本文围绕恶劣天气下语义分割鲁棒性及雨雪雾干扰消除展开,分析了雨雪雾对图像和分割任务的影响,介绍了基于图像增强、深度学习等现有解决方法,以及针对不同干扰的具体方案,还探讨了提高分割鲁棒性的策略、实验评估情况,指出当前面临复杂天气、数据缺乏、实时性等挑战,并展望了端到端模型、无监督学习等未来发展趋势,为该领域研究提供了全面参考。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 17万+

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

题目原址 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/ 题目描述 给定一个数组和一个变量,其中数组中的元素表示当天的股票价格,变量表示每次交易需要支付的固定费用。不限制股票的购买和售出的次数,但是要求在购买新的股票之前要将手中的股票全部售出。 ...

想当厨子的程序媛 536

十八.EtherCAT开发之ECM-XF主站与 AX58200从站的FOE固件升级

AX58200 (新塘M480与AX58100)是 SPI连接,工作在SPI模式。FoE(File Access over EtherCAT)可实现EtherCAT节点之间的文件传输。TWINCAT 与AX58200 之间的FOE 正常运行,说明从站FOE没有问题,这个是重大的前提ECM-XF主站 SPI模式下,FOE不通,所以先调试USB模式,排除MCU代码问题。

AIoT深度探索者 | 嵌入式系统架构师 深耕物联网(IoT)与人工智能(AI)融合技术,聚焦​​边缘计算、图像开发、低功耗通信协议​​,致力于智能硬件,关注我获取最新技术文章信息与海量资源~ 2304

Calibre 3Dstack --每日一个命令day27【overlap】(3-27)

本文介绍了Calibre3Dstack中overlap命令的使用方法。该命令用于检查两个已放置层之间的重叠量是否符合要求,支持设置检查名称、层类型、堆叠范围、检查方向等参数。关键参数包括:-constraint指定重叠约束条件,-exact控制百分比精度,-by_area切换为面积检查模式,-intersection报告相交区域。检查结果可通过RVE选项定制显示方式,适用于3D堆叠设计中的层间重叠验证场景。

u013620135的博客 120

LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee (Java版; Medium)

welcome to my blog LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee (Java版; Medium) 题目描述 Your are given an array of integers prices, for which the i-th element is the price of a give...

littlehaes的博客 191

2026年全国青少年信息素养大赛复赛真题(算法应用主题赛C++初中组:带解析)(7月4日试卷一)【文末附答案和解析】

2026年全国青少年信息素养大赛复赛真题(算法应用主题赛C++初中组:带解析)(7月4日试卷一)【文末附答案和解析】

王老师青少年编程 1789

【Leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee

题目地址: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 以数组AAA的形式给出每天的股票价格,只有买入之后才能卖出,并且每次卖出的时候会收取一笔交易费。问能得到的最大利润。 思路是状态机。有两个状态,即手里有股票和手里无股票,以下简称“有货”和“无货”。在无货的时候,下一天可以继续无货,则cash变化为000,下一天也可以有货,则cash变化为负的下一天股票价格(买了股票cash减少)

数学、算法爱好者的博客 248

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LeetCode-714. Best Time to Buy and Sell Stock with Transaction Fee [C++][Java]

You are given an arraypriceswhereprices[i]is the price of a given stock on theithday, and an integerfeerepresenting a transaction fee. Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay

贫道绝缘子的博客 216

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee

原题链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ Buy and Sell Stock专题相关题目 121. Best Time to Buy and Sell Stock 122. Best Time to Buy and Sell Stock II 123. Best...

Ber03的博客 249

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

[动态规划] leetcode 714 Best Time to Buy and Sell Stock with Transaction Fee

problem:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 维护两个状态,一个是当前持有股票状态,一个是当前不持有股票状态,两者分别计算最大值。 在第i天,如果当天不持有股票,那么可能是昨天也没有持有股票,也有可能是当天卖出了股票...

ayzejx8547408的博客 258

【LeetCode】解题714:Best Time to Buy and Sell Stock with Transaction Fee(动态规划)

LeetCode解题 714:Best Time to Buy and Sell Stock with Transaction Fee (动态规划)Problem 714: Best Time to Buy and Sell Stock with Transaction Fee [Medium]解题思路Solution (Java) Problem 714: Best Time to Buy an...

Fayedily的博客 341

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

Description 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 c...

zy2317878的博客 618

LeetCode 题解: 714. Best Time to Buy and Sell Stock with Transaction Fee

You 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

qq_23666815的博客 577

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

题目 链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ Level: Medium Discription: Your are given an array of integers prices, for which the i-th element is the pric...

weixin_30588827的博客 152

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

DescriptionYour 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

csdn_cinderella的博客 513

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

题目描述 题目难度:Medium 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 c...

tkzc_csk的博客 248

【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 on day i; and a non-negative integer fee representing a transaction fee. You may complete as many ...

ghscarecrow的博客 105

LeetCode 714 Best Time to Buy and Sell Stock with Transaction Fee

LeetCode 714

bkjs626的博客 329
上一篇: leetcode 659. Split Array into Consecutive Subsequences
下一篇: leetcode 738.Monotone Increasing Digits
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址