714. 买卖股票的最佳时机含手续费
714. 买卖股票的最佳时机含手续费
- 原题链接:
- 完成情况:
- 解题思路:
- Explanation
- Summary
- 参考代码:
- _714买卖股票的最佳时机含手续费
- 错误经验吸取
原题链接:
714. 买卖股票的最佳时机含手续费
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/
完成情况:
解题思路:
Sure, let’s break down the code and explain it step by step.
The problem is to maximize the profit from buying and selling stocks given that there is a transaction fee each time you sell a stock. You can perform as many transactions as you like, but you must sell the stock before you can buy again.
Explanation
-
Initialization:
int n = prices.length; int [][] dp = new int[n][2]; dp[0][0] = 0; dp[0][1] = -prices[0];
n
is the length of the prices array.dp
is a 2D array wheredp[i][0]
represents the maximum profit at dayi
when you don’t have any stock, anddp[i][1]
represents the maximum profit at dayi
when you have one stock.- On day 0, if you don’t own a stock, the profit is 0 (
dp[0][0] = 0
). - On day 0, if you own a stock, the profit is
-prices[0]
because you bought the stock atprices[0]
(dp[0][1] = -prices[0]
).
-
DP Transition:
for (int i = 1; i < n; i++){dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i] - fee);dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] - prices[i]); }
- For each day
i
from 1 ton-1
, we update thedp
array:dp[i][0]
can be obtained by either:- Doing nothing on day
i
, hencedp[i][0] = dp[i-1][0]
. - Selling the stock on day
i
, hencedp[i][0] = dp[i-1][1] + prices[i] - fee
(selling the stock gives the price at dayi
, but we must subtract the transaction fee).
- Doing nothing on day
dp[i][1]
can be obtained by either:- Doing nothing on day
i
, hencedp[i][1] = dp[i-1][1]
. - Buying the stock on day
i
, hencedp[i][1] = dp[i-1][0] - prices[i]
(we subtract the price at dayi
from the profit since we are buying the stock).
- Doing nothing on day
- For each day
-
Result:
return dp[n-1][0];
- At the end of the loop,
dp[n-1][0]
will contain the maximum profit we can achieve on the last day if we don’t own any stock (which is the desired result since we want to end up with no stock to realize the profit).
- At the end of the loop,
Summary
- The algorithm uses dynamic programming to keep track of the maximum profit for each day, considering both states of holding a stock or not holding a stock.
- The transition equations consider the profit from both holding and selling a stock, incorporating the transaction fee.
- Finally, the algorithm returns the maximum profit achievable by the end of the last day when no stock is held.
参考代码:
_714买卖股票的最佳时机含手续费
package leetcode板块;public class _714买卖股票的最佳时机含手续费 {/**** @param prices* @param fee* @return*/public int maxProfit(int[] prices, int fee) {// 你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。// 返回获得利润的最大值。int n = prices.length;int [][] dp = new int[n][2];// 0 代表当前股票的空余情况, 1代表当前股票处于抛售的情况dp[0][0] = 0;dp[0][1] = -prices[0];// TODO 交易的过程中,引入卖出时的手续费for (int i = 1; i < n; i++){dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1] + prices[i] - fee);dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0] - prices[i]);}return dp[n-1][0];}
}
错误经验吸取
相关文章:

714. 买卖股票的最佳时机含手续费
714. 买卖股票的最佳时机含手续费 原题链接:完成情况:解题思路:ExplanationSummary 参考代码:_714买卖股票的最佳时机含手续费 错误经验吸取 原题链接: 714. 买卖股票的最佳时机含手续费 https://leetcode.cn/probl…...

Linux系统查看程序内存及CPU占用
文章目录 1.free命令2.top命令3.PS命令3.1 查看内存占用前10位:3.2 查看CPU占用前10位 参考文档 1.free命令 可以通过free命令查看物理内存占用情况 #单位KB free #单位MB free -m #单位GB free -h 2.top命令 输入top命令,会输出定时刷新的程序PID、内…...

数据结构7---图
一、定义 对于图的定义,我们需要明确几个注意的地方:一线性表中我们把数据元素叫元素,树中叫结点,在途中数据元素我们则称之为顶点(Vertex)。 对于图的定义,我们需要明确几个注意的地方: 线性表中我们把数据元素叫元素…...

Excel 如何复制单元格而不换行
1. 打开excle, sheet1右键单击>查看代码>插入>模块 输入代码 Sub CopyText() Updated by NirmalDim xAutoWrapper As ObjectSet xAutoWrapper New DataObject or GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")xAutoWrapper.SetText ActiveC…...

前端 CSS 经典:mix-blend-mode 属性
前言:这是一个混合属性,作用是将两个颜色混合生成一个新颜色。可以将视频和文字相融合,产生动态文字效果。 效果 实现代码 <!DOCTYPE html> <html lang"en"><head><meta charset"utf-8" />&l…...
OpenCV--滤波器(一)
低通滤波器 代码和笔记 代码和笔记 import cv2 import numpy as np""" 滤波器--用于图像处理的重要工具,它们可以根据图像中像素的邻域信息来修改像素值,以实现去噪、模糊、锐化、边缘检测等效果。低通滤波器(Low-pass Filte…...

MK的前端精华笔记
文章目录 MK的前端精华笔记第一阶段:前端基础入门1、(1)、(2)、 2、3、4、5、6、7、 第二阶段:组件化与移动WebAPP开发1、(1)、(2)、 2、3、4、5、6、7、 第三…...
低代码平台框架:开源选型、实践与应用深度解析
文章目录 1.1 低代码平台的重要性与应用背景2.1 表单建模2.2 流程设计2.3 报表(打印)可视化2.4 代码生成器2.5 系统管理2.6 前端UI开源选型3.1 如何选择合适的开源框架3.2 市场上的主要开源低代码平台对比3.3 开源项目的技术栈与优缺点分析 5.1 成功案例…...

深度学习500问——Chapter12:网络搭建及训练(3)
文章目录 12.3.5 Caffe有哪些接口 12.4 网络搭建有什么原则 12.4.1 新手原则 12.4.2 深度优先原则 12.4.3 卷积核size一般为奇数 12.4.4 卷积核不是越大越好 12.5 有哪些经典的网络模型值得我们去学习的 12.6 网络训练有哪些技巧 12.6.1 合适的数据集 12.6.2 合适的预…...

Android使用DevRing框架搭建数据库实体类以及使用
一、引用DevRing依赖 //导入DevRing依赖implementation com.ljy.ring:devring:1.1.8创建数据库表的依赖implementation org.greenrobot:greendao:3.2.2 // add libraryimplementation org.greenrobot:greendao-generator:3.0.0 二、修改工程目录下的.idea->gradle.xml文件&…...
高效BUG管理:定级、分类和处理流程
高效BUG管理:定级、状态跟踪与处理全流程 前言一、BUG的定义二、BUG的定级三、BUG的状态四、BUG的处理流程1. BUG报告2. BUG确认3. BUG修复4. BUG验证5. BUG关闭 五、常见问题与解决方案六、总结 前言 在测试工作中,BUG的定级和分类是一个重要环节&…...

服务器数据恢复—raid5热备盘同步失败导致阵列崩溃如何恢复数据?
服务器存储数据恢复环境&故障: 某品牌DS5300存储,包含一个存储机头和多个磁盘柜,组建了多组RAID5磁盘阵列。 某个磁盘柜中的一组RAID5阵列由15块数据盘和1块热备硬盘组建。该磁盘柜中的某块硬盘离线,热备盘自动替换并开始同步…...

Ubuntu iso 镜像下载 步骤截图说明
Ubuntu镜像下载,在这个网址: Enterprise Open Source and Linux | Ubuntu 步骤如下图所示: 1、登入网址 2、点击Get Ubuntu 3、点击Download Ubuntu Desktop 后续点击Downloadload 24.04 LTS直接下载就行 如果需要下载其它版本…...

git拉取gitee项目到本地
git安装等不做赘述。 根据需要选择不同操作 1.只是单纯拉取个项目,没有后续的追踪等操作 不需要使用git init初始化本地文件夹 新建一个文件夹用于存储项目,右键选择 git bash here 会出现命令行窗口 如果像我一样,只是拉取个项目作业&…...
力扣42.接雨水
力扣42.接雨水 前后缀数组 对于每个一个位置 求其前面最高高度pre_max[i] max(pre_max[i-1] , h[i])和后面最高高度suf_max[i] max(suf_max[i1] , h[i])当前i处的水容量 为min(pre_max[i] , suf_max[i]) - h[i] class Solution {public:int trap(vector<int>& …...
国产数据库与MYSQL兼容性?开发应该怎么选择?
国产数据库主要包括以下几种: TiDB:由 PingCAP 公司研发设计的开源分布式 HTAP (Hybrid Transactional and Analytical Processing) 数据库,兼容 MySQL,支持无限的水平扩展,具备强一致性和高可用等特性。 华为GaussDB…...
Spring框架中Bean的生命周期
Bean的生命周期通常指的是从创建到初始化,经过一系列的流程,最终销毁的过程。只不过,在Spring框架中,Bean的生命周期是由Spring IOC容器来管理的。在Spring中,我们定义Bean时,也可以自己指定初始化和销毁的…...
从零到一学FFmpeg:avformat_alloc_output_context2 函数详析与实战
文章目录 前言一、函数原型二、功能描述三、使用场景四、AVFormatContext 结构体五、代码实例 前言 avformat_alloc_output_context2 是FFmpeg库中的一个函数,用于为输出多媒体文件初始化一个AVFormatContext结构体。这个函数在开始输出音频、视频数据到文件之前被…...
Lua 绕过元表
Lua 绕过元表,直接访问 table 的字段。 绕过元表 rawset(table, index, value),在不触发元方法的情况下,设置 table[index] 的值为 value。 rawget(table, index),在不触发元方法的情况下,获取 table[index] 的值。…...
pip方法总结(极简快速掌握)
pip是Python的包管理工具,它允许用户从PyPI等源安装和管理额外的库和依赖。以下是关于pip使用方法的详细总结,同时附上代码演示: 一、pip的基本功能 安装包:使用pip install 包名命令可以安装指定的Python包。例如,要…...

边缘计算医疗风险自查APP开发方案
核心目标:在便携设备(智能手表/家用检测仪)部署轻量化疾病预测模型,实现低延迟、隐私安全的实时健康风险评估。 一、技术架构设计 #mermaid-svg-iuNaeeLK2YoFKfao {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg…...

渗透实战PortSwigger靶场-XSS Lab 14:大多数标签和属性被阻止
<script>标签被拦截 我们需要把全部可用的 tag 和 event 进行暴力破解 XSS cheat sheet: https://portswigger.net/web-security/cross-site-scripting/cheat-sheet 通过爆破发现body可以用 再把全部 events 放进去爆破 这些 event 全部可用 <body onres…...

dedecms 织梦自定义表单留言增加ajax验证码功能
增加ajax功能模块,用户不点击提交按钮,只要输入框失去焦点,就会提前提示验证码是否正确。 一,模板上增加验证码 <input name"vdcode"id"vdcode" placeholder"请输入验证码" type"text&quo…...

Mac软件卸载指南,简单易懂!
刚和Adobe分手,它却总在Library里给你写"回忆录"?卸载的Final Cut Pro像电子幽灵般阴魂不散?总是会有残留文件,别慌!这份Mac软件卸载指南,将用最硬核的方式教你"数字分手术"࿰…...
Swagger和OpenApi的前世今生
Swagger与OpenAPI的关系演进是API标准化进程中的重要篇章,二者共同塑造了现代RESTful API的开发范式。 本期就扒一扒其技术演进的关键节点与核心逻辑: 🔄 一、起源与初创期:Swagger的诞生(2010-2014) 核心…...
laravel8+vue3.0+element-plus搭建方法
创建 laravel8 项目 composer create-project --prefer-dist laravel/laravel laravel8 8.* 安装 laravel/ui composer require laravel/ui 修改 package.json 文件 "devDependencies": {"vue/compiler-sfc": "^3.0.7","axios": …...
Spring Security 认证流程——补充
一、认证流程概述 Spring Security 的认证流程基于 过滤器链(Filter Chain),核心组件包括 UsernamePasswordAuthenticationFilter、AuthenticationManager、UserDetailsService 等。整个流程可分为以下步骤: 用户提交登录请求拦…...

解析两阶段提交与三阶段提交的核心差异及MySQL实现方案
引言 在分布式系统的事务处理中,如何保障跨节点数据操作的一致性始终是核心挑战。经典的两阶段提交协议(2PC)通过准备阶段与提交阶段的协调机制,以同步决策模式确保事务原子性。其改进版本三阶段提交协议(3PC…...

Xcode 16 集成 cocoapods 报错
基于 Xcode 16 新建工程项目,集成 cocoapods 执行 pod init 报错 ### Error RuntimeError - PBXGroup attempted to initialize an object with unknown ISA PBXFileSystemSynchronizedRootGroup from attributes: {"isa">"PBXFileSystemSynchro…...

华为云Flexus+DeepSeek征文 | 基于Dify构建具备联网搜索能力的知识库问答助手
华为云FlexusDeepSeek征文 | 基于Dify构建具备联网搜索能力的知识库问答助手 一、构建知识库问答助手引言二、构建知识库问答助手环境2.1 基于FlexusX实例的Dify平台2.2 基于MaaS的模型API商用服务 三、构建知识库问答助手实战3.1 配置Dify环境3.2 创建知识库问答助手3.3 使用知…...