LeetCode- Best Time to Buy and Sell Stock

leetcode难题之Best time to buy and sell stock大合集 这次带来的是Best time to buy and sell stock大合集,在leetcode上这一合集共有6道题,难度从easy到hard不等,我们这次记录下这一合集的全部解法并记录下解题的思考想法。所有的题目都是计算不同计算规则下股票的最大利润。 一,Best Time to Buy and Sell Stock (一次交易) Say you have an array for w... 阅读详情

LeetCode- Best Time to Buy and Sell Stock


题目描述

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Example 1:

Input: [7, 1, 5, 3, 6, 4] Output: 5

max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be
larger than buying price)

Example 2:

Input: [7, 6, 4, 3, 1] Output: 0

In this case, no transaction is done, i.e. max profit = 0.

只能买卖至多一次,求最大利润。

算法理解

  1. 遍历数组,记录最小值和当前最大值。

    • 如果当前价格小于最小值,更新,
      当前价格减最小值,如果大于之前的最大值,更新最大值。
  2. ob大神视角(Kadane’s algorithm(Kadane算法))

[1, 7, 4, 11] =》 [0,6,-3, 7];(difference array of prices)

diff[0] = 0;

等价于一个经典问题。
Maximum of subarray(最大子序列和)

Maximum of subarray(最大子序列和)
找到一个一维数组内的连续的子数组的最大数字的总和。


Kadane’s algorithm
详情看我的博客页。

*maxCur = current maximum value

*maxSoFar = maximum value found so far

(有时间回来补充)

代码块

代码一

int maxProfit(vector<int> &prices) {
    int maxPro = 0;
    int minPrice = prices[0];
    for(int i = 1; i < prices.size(); i++){
        minPrice = min(minPrice, prices[i]);
        maxPro = max(maxPro, prices[i] - minPrice);
    }
    return maxPro;
}

代码二

public int maxProfit(int[] prices) {
        int maxCur = 0, maxSoFar = 0;
        for(int i = 1; i < prices.length; i++) {
            maxCur = Math.max(0, maxCur += prices[i] - prices[i-1]);
            maxSoFar = Math.max(maxCur, maxSoFar);
        }
        return maxSoFar;
    }
嵌入式音频架构 - AudioWeaver 整体概念 AudioWeaver 是DSP Concept公司推出的基于嵌入式平台的音频框架。由AWE Core embedded processing engine 和 Audio Weaver Designer IDE 组成。 Audio Weaver Designer 有三个版本,ST Edition, Standard Edtion 和 Pro Edtion。 Audio Weaver Designer界面: Audio Weaver 的整体使用架构: AWD算法流文件,AWS配置脚本文件,AWB嵌入式b 阅读详情

相关推荐

机器人开发路线

通过这个路线图,你可以在6个月内完成从原型到试产的全流程。建议初期采用敏捷开发模式,每两周迭代一个核心功能模块。在硬件集成遇到瓶颈时,推荐参考。

woody218的博客 744

leetcode best-time-to-buy-and-sell-stock-iii(Java)

leetcode题目 best-time-to-buy-and-sell-stock-iii – newcoder 28 买卖股票的最佳时机III – leetcode 123 题目描述 * Say you have an array for which the i th element * is the price of a given stock on day i. * * Des...

聚沙成塔 684

ThinkAdmin后台Getshell

通过文件包含或者图片马的方式Getshell。修改其可允许的文件类型,php不允许,可添加。系统管理->系统参数配置->本地服务器存储。

hot_milk1的博客 1863

leetcode之Best Time to Buy and Sell Stock III 问题

问题描述: Say you have an array for which the ith element is the price of a given stock on dayi. Design an algorithm to find the maximum profit. You may complete at mosttwo transactions.(发现这些问题变化了啥没?)

漫漫学习路 326

避坑指南:STM32CubeMX配置USB虚拟串口时Heap Size设置的那些坑

本文深入剖析了使用STM32CubeMX配置USB虚拟串口时,因Heap Size设置不当导致设备无法识别的常见问题。文章从故障现象出发,详细解释了堆内存不足如何影响USB枚举过程,并提供了从内存布局分析、动态监控到系统化调试的完整解决方案,帮助开发者从根本上避免这一配置陷阱。

ttt77的博客 599

遥感卫星影像AI数据集格式规范详解、技术原理与数据生产全流程技术手册

本技术手册旨在系统性地梳理遥感卫星影像AI数据集的各类格式标准(涵盖VOC、COCO、YOLO及遥感专用DOTA格式等),深入剖析其背后的技术原理,并详细阐述数据生产的全过程,涵盖预处理、标注、质量控制及数据增强等关键环节。无论您是刚入门的算法工程师,还是资深的数据生产管理者,本手册都将为您提供详实的参考与指引。

panshengwu的博客 1126

Leetcode Best Time to Buy and Sell Stock python 问题解析

Leetcode 121题 Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i....

润泽的博客 286

玩转openclaw+企业微信

OpenClaw横空出世:AI从问答到执行的范式革命 OpenClaw作为2026年AI领域的重大突破,实现了从对话机器人到智能执行体的进化。其核心价值在于"自主执行"而非简单交互,通过六层解耦架构(表现层、网关层、运行时层、技能层、工具层、记忆层)构建完整执行闭环。关键技术包括Model Context Protocol实现多模型兼容,以及事件驱动架构确保实时响应。安装过程支持多平台,通过Node.js环境即可快速部署,并支持对接主流大模型。OpenClaw标志着AI开始真正&quot

西木风落 1591

【LeetCode】解题122:Best Time to Buy and Sell Stock II

LeetCode解题 122:Best Time to Buy and Sell Stock II Problem 122: Best Time to Buy and Sell Stock II [Easy]解题思路Solution (Java) Problem 122: Best Time to Buy and Sell Stock II [Easy] Say you have an array...

Fayedily的博客 367

Mac用户必看:PD虚拟机网络初始化失败的3分钟修复指南(附详细截图)

本文针对Mac用户在Parallels Desktop(PD虚拟机)启动时遇到的“网络初始化失败”问题,提供了快速诊断与修复指南。核心原因是macOS系统更新后权限未正确授予。文章通过详细的三步修复法(关闭进程、修改配置文件、重新授权重启),帮助用户在3分钟内恢复虚拟机网络连接,并附有排查清单与预防建议。

weixin_29231027的博客 537

SigFit实战:从FEA到光学仿真的热力耦合分析全流程解析

在光机系统与芯片封装等精密工程领域,热与力的耦合效应是影响产品性能与可靠性的核心物理问题。有限元分析(FEA)是预测结构在热、力载荷下变形与应力的基础工具,但其结果通常无法直接用于评估光学性能。这需要一座桥梁,将物理场数据转化为光学设计语言。其核心原理在于数据映射与面形表征技术,例如通过泽尼克多项式拟合,将离散的节点位移分解为离焦、像散等标准光学像差。这项技术的工程价值在于,它实现了多物理场仿真的闭环,使工程师能在设计阶段精准预测热变形或应力对波前误差、调制传递函数等关键光学指标的影响。其典型应用场景包括高

weixin_28630185的博客 337

【LeetCode】解题123:Best Time to Buy and Sell Stock III(动态规划)

LeetCode解题 123:Best Time to Buy and Sell Stock III (动态规划)Problem 123: Best Time to Buy and Sell Stock III [Hard]解题思路I. O(2n)时间+O(3n)空间II. O(2n)时间+O(n)空间III. O(n)时间+O(4)空间Solution (Java) Problem 123: B...

Fayedily的博客 491

Devexpress16.2.6破解工具

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

leetcode 122 Best Time to Buy and Sell Stock II 详细解答

leetcode 122 Best Time to Buy and Sell Stock II 详细解答 解答1 假设有一个图像代表数组: > https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/solution/ 最后需要求得的答案就是,每一段的递增区间的差值。所以得到代码如下: 解法2 上面解法比较hack, 不推荐。 就股票买卖来说,买入股票自身的价格就会 -Price,卖出股票自身的价格就是 +Price. 利

qq_22210659的博客 270

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

leetcode 121. Best Time to Buy and Sell Stock

leetcode-121 Best Time to Buy and Sell Stock (Easy) Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transacti...

Night_Thief的博客 166

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

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

LeetCode解题 309:Best Time to Buy and Sell Stock with Cooldown (动态规划)Problem 309: Best Time to Buy and Sell Stock with Cooldown [Medium]解题思路Solution (Java)修改过程 Problem 309: Best Time to Buy and Sell Sto...

Fayedily的博客 323

[刷题]leetcode #309 - Best Time to Buy and Sell Stock with Cooldown

题目 代码 class Solution { public int maxProfit(int[] prices) { if (prices.length <= 1) return 0; int l = prices.length; int a=0, b = -prices[0], c = 0; for (int i = 1; i < l; ++i){ int temp = a;

qq_27637285的博客 309

Best Time to Buy and Sell Stock - LeetCode

目录 题目链接 注意点 解法 小结 题目链接 Best Time to Buy and Sell Stock - LeetCode 注意点 在卖出之前必须要先购入 解法 解法一:遍历一遍,随时记录当前数字之前的最小的数字。将当前数字与当前最小数字相减查看收益。时间...

weixin_30335353的博客 91

Leetcode 121. Best Time to Buy and Sell Stock

Leetcode 121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e...

fengran_mark的博客 140

leetcode best-time-to-buy-and-sell-stock(Java)

leetcode题目 best-time-to-buy-and-sell-stock – newcoder 30 买卖股票的最佳时机 – leetcode 121 题目描述 * Say you have an array for which the i th element * is the price of a given stock on day i. * * If you were...

聚沙成塔 484

【leetcode】【medium】309. Best Time to Buy and Sell Stock with Cooldown​​​​​​​

309. Best Time to Buy and Sell Stock with Cooldown Say you have an array for which theithelement is the price of a given stock on dayi. Design an algorithm to find the maximum profit. You may com...

lemonade13的博客 146

LeetCode Best Time to Buy and Sell Stock III

给定每天的股票价格,如果最多允许两次交易,但手中最多只能持有一支股票,在再次买入的时候必须将之前的股票卖出,求能获取的最大利润。

DRFish 2552

Leetcode在线编程 Best Time to Buy and Sell Stock II

Leetcode在线编程 Best Time to Buy and Sell Stock II DP

你误入了一个弱渣的领域 613

LeetCode 122. Best Time to Buy and Sell Stock II

题目来源:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 问题描述 122.Best Time to Buy and Sell Stock II Easy Say you have an array for which theithelement is the price of a given stoc...

da_kao_la的博客 186

Best Time to Buy and Sell Stock III -- LeetCode

原题链接: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/  这道题是Best Time to Buy and Sell Stock的扩展,现在我们最多可以进行两次交易。我们仍然使用动态规划来完成,事实上可以解决非常通用的情况,也就是最多进行k次交易的情况。 这里我们先解释最多可以进行k次交易的算法,

Code Ganker 4万+

LeetCode - Best Time To Buy and Sell Stock Series (I, II, III, IV, with Cooldown)

LeetCode - Best Time to Buy and Sell Stock 系列 解题报告本文记录笔者在解LeetCode上的Best Time to Buy and Sell Stock系列时的一些想法。该系列题目共有五道,它们背景相似,但因为条件限制不同,解决思路也不太相同。I Best Time to Buy and Sell Stock https://leet

Gregzeng的博客 687

LeetCode123—Best Time to Buy and Sell Stock III

LeetCode123—Best Time to Buy and Sell Stock III

上善若水 1320
上一篇: LeetCode -Best Time to Buy and Sell Stock with Cooldown
下一篇: LeetCode- kadane algorithm
Coder_zyq
博客等级 码龄10年 3粉丝 23原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值