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

LeetCode --- 122. Best Time to Buy and Sell Stock II 解题报告 题目: 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 complete as many transactions as you like (i.e., b... 阅读详情

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

Problem 122: Best Time to Buy and Sell Stock II [Easy]

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

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.

Example 2:

Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.

Example 3:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

Constraints:

  • 1 <= prices.length <= 3 * 10 ^ 4
  • 0 <= prices[i] <= 10 ^ 4

来源:LeetCode

解题思路

本题只需遍历一遍数组,将给定的每日价格数组看作折线图,多次交易的最大利润应该是每一段上升线段差值的累计。
借用LeetCode官方题解的图来理解为何每一段上升都要计入:
第一种情况: 两段上升非严格递增

这一种情况容易有在valleyi买入在peakj卖出比两次交易利润更大的错觉,但其实永远有 A + B ≥ C A + B \geq C A+B≥C。因此valleyi ~ peaki、valleyj ~ peakj这两上升段的差值都应该计入总利润。

第二种情况: 两段上升严格递增

很明显有 A + B + C = D A + B + C = D A+B+C=D,因此累计每一段差值和直接计算最大差值的效果是一样的。

具体过程:

  • maxP变量。
  • 遍历数组,累计每一段上升线段,公式为:
    m a x P = m a x P + max ⁡ { 0 , p r i c e s [ i ] − p r i c e s [ i − 1 ] } maxP = maxP + \max\{0, prices[i] - prices[i-1]\} maxP=maxP+max{0,prices[i]−prices[i−1]}
    其中 p r i c e s [ i ] prices[i] prices[i]为当前价格。
  • 最后返回最大利润值maxP。

时间复杂度为 O ( n ) O(n) O(n),空间复杂度为 O ( 1 ) O(1) O(1)。

运行结果:
在这里插入图片描述

Solution (Java)

class Solution {
    public int maxProfit(int[] prices) {
        int N = prices.length;
        if(N < 2) return 0;
        int maxP = 0;
        for(int i = 1; i < N; i++){
            if(prices[i] > prices[i-1]){
                maxP += prices[i] - prices[i-1];
            }
        }
        return maxP;
    }
}
leetcode题目:Best Time to Buy and Sell Stock II 立即下载

相关推荐

全球尺度遥感云计算平台:Google Earth Engine

Google Earth Engine(GEE)是一个基于 Google 云服务基础设施的遥感大数据分析平台,它结合 Google 强大的云计算能力与 NASA、ESA、NOAA 等机构的空间数据,用于在全球尺度解决一些非常重要的社会问题:森林退化、粮食安全、灾害预警、水资源管理、气候监测以及环境保护等。 GEE 官方宣传视频(版权归 Google 所有) Spacefan 的视频 ·303播放 发展背景 2008 年,手握 40 多年 LandSat 数据的美国地质调查局(USG...

qq_41240287的博客 5026

Leetcode-122:买卖股票的最佳时机 II(超简单解法)

题目链接: https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目: 给定一个数组 prices ,其中prices[i] 是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 示例: 示例 1: 输入: prices = [7,1,5,3,6,4] 输出: 7 .

道纪书生的博客 233

动态数据脱敏技术分析

在当前国内信息安全热潮中,数据脱敏作为数据安全的重要一环得到了业界的认可与重视。早在2012年,数据脱敏首次作为一个单独的魔力象限由Gartner发布,Gartner在2014年又提出了:按照数据使用场景,将数据脱敏分为静态数据脱敏(Static data masking-SDM )与动态数据脱敏(Dynamic data masking-DDM )。 可能有人望文生义,认为动态数据脱敏一定比静态数据脱敏高级。非也非也,静态or动态,取决于脱敏的使用场景,主要是以使用场景为由来选择合适的数据脱敏的模式。 本

美创科技的博客 4687

【LeetCode 面试经典150题】122. Best Time to Buy and Sell Stock II 买卖股票的最佳时机II

在每一天,你可以决定买入和/或卖出股票。你在任何时候最多只能持有一股股票。然而,你可以在同一天买入后立即卖出。这段代码的目的是计算给定股票价格数组中可以实现的最大利润,允许多次买卖。找出并返回你能获得的最大利润。

qq_43513394的博客 542

【论文复现】自动化细胞核分割与特征分析

HoVer-Net是一种精心设计的多分支深度学习网络架构,专为处理复杂的多组织显微图像中的细胞核实例分割与精确分类任务而开发。该网络通过创新的策略,不仅有效解决了细胞核在密集聚集区域难以准确区分的问题,还实现了对每个独立细胞核类型的细致分类。此外,HoVer-Net还引入了专门的上采样分支,这一设计旨在对每个成功分割出的细胞核实例进行进一步的核类型分类。这一分支利用高级语义信息,结合精心设计的分类策略,能够准确判断每个细胞核的所属类型,为医学研究提供了宝贵的细胞水平信息。

2201_75366661的博客 1982

RK3588上从零部署Facenet人脸识别:PyTorch转ONNX再到RKNN的完整避坑指南

本文提供了在RK3588开发板上从零部署Facenet人脸识别模型的完整指南。详细介绍了从PyTorch模型导出、转换为ONNX格式,再通过RKNN-Toolkit2转换为RKNN格式的完整流程,并重点分享了环境配置、算子兼容性处理、量化调优及板端部署中的关键步骤与避坑经验。

river的博客 470

LeetCode122. Best Time to Buy and Sell Stock II题解

今天讨论一道让我想的过于复杂的题目:一道有关股票买卖的问题。题目描述:Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many

SYSU_LBY的博客 622

05 Transformer 到底解决了什么问题

Transformer 是现代大语言模型的底层骨架。本文不推公式,而是从 CNN、RNN/LSTM 的瓶颈讲起,解释 Transformer 如何通过自注意力同时解决长距离依赖和并行训练问题,并说明它为什么能支撑 GPT、BERT 和现代大模型的规模化。

老于的笔记的专栏 258

122. Best Time to Buy and Sell Stock II的C++解法

题目说得不是特别清楚,研究了好几遍,意思其实就是只有一支股票,卖了之后才能再买进,买了之后才能再卖出。但是不限制交易次数。 看起来特别复杂其实用贪心就可以解决。对于每一笔交易 假设是从i天买入 j天卖出 ,如果有j+1天的价格高于j天,所以肯定是从i到j+1利润更高,同样的如果有第i-1天价格比第i天更低,肯定是从i-1天到j天利润更高。画个曲线图更容易理解,只要在低谷买入,高峰卖出,收益就是最

MuseCP@YenTown 449

【笔记】1.1 拉伸力-伸长(延伸)曲线和应力-应变曲线

过了B点后,由于颈缩导致的截面半径迅速减小,导致真应力迅速上升,切线斜率明显提高。假设试样原长为ε,在力的作用下伸长了dε,那么根据应变的定义,瞬时的应变为dε/ε,总的应变为瞬时应变的累计,假设在瞬时的条件下,试样受载荷变化量为 ΔF ,变化的截面积为 ΔA ,而且ΔA→0,那么瞬时的。由于原始的截面积和原始的标距长度为常数,故得到的曲线长相和拉伸力-伸长(延伸)曲线没啥区别。因为用的都是原始的截面积和原始的标距长度,故称为公称应力和公称应变。如果拉伸力→应力,伸长长度→应变,就会得到应力-应变曲线。

m0_73676887的博客 5291

YOLO26涨点改进 |TGRS 2026 | 全网独家改进、特征融合创新篇 | 引入CMIFE跨模态交互特征提取模块,充分挖掘互补信息,适合多模态图像融合,目标检测,实例分割,遥感图像分类高效涨点

本文提出CMIFE模块用于改进YOLO26网络模型,通过跨模态交互特征提取实现多尺度特征的精细化融合。该模块采用双注意力机制(自注意力强化模态内一致性,交叉注意力实现跨模态互补信息筛选),有效解决了传统YOLO融合中的信息冗余和负迁移问题。实验表明,CMIFE模块在保持模型实时性的同时,显著提升了小目标检测、遮挡场景等复杂环境下的检测精度。文章详细介绍了模块原理、网络结构、核心代码实现方法,并提供了三种改进版YOLO26的配置文件方案(yolo26_CMIFE-1/2/3.yaml)。

Ai缝合怪、助力小伙伴高效发论文 151

122. Best Time to Buy and Sell Stock II

leetcode 122

张小张的博客 330

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

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

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

leetcode 123. Best Time to Buy and Sell Stock III

/二是第i天买入了第二只股票(需支付prices[i]),第i天能买入第二只股票的前提是第i-1天处于已经卖出第一只股票但还没买入第二只股票的状态(对应dp[i-1][2])//二是第i天第一次买入股票(需支付prices[i]),第i天能第一次买入股票的前提是前一天(第i-1天)处于没有买过股票的状态(对应状态dp[i-1][0])//二是第i天卖出了第一只股票(收入prices[i]),第i天能卖出第一只股票的前提是第i-1天处于持有第一只股票的状态(对应dp[i-1][1])

weixin_41554427的博客 1379

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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

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

聚沙成塔 538

LeetCode 122. Best Time to Buy and Sell Stock II 解题报告

LeetCode 122. Best Time to Buy and Sell Stock II 解题报告

骆小坑的填坑之旅 877

[LeetCode]122. Best Time to Buy and Sell Stock II

122. Best Time to Buy and Sell Stock II 一、题目 Problem Description: Say you have an array prices for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you li

KKKKKKOBE_24的博客 299

122. Best Time to Buy and Sell Stock II [JavaScript]

一、解题思路   相比较121. Best Time to Buy and Sell Stock,本道题允许多次交易,本质上就是买进和卖出两种操作的多次切换。   那么可以这样定义状态: hold[i]表示第i天所持有的最大价值 sell[i]表示第i天出售所产生的最大价值   这两种状态分别对应买进和卖出两种操作所产生的状态: 买进时,所持有的最大价值(hold[i])需要减去当前股...

descire 187

Leetcode 122 Best Time to Buy and Sell Stock II 不限次数买卖股票最大收益

假设你有一个数组,里面记录的是每一天的股票的价格。设计一个算法来计算最大收益。你可以完成任意次数的交易(即,买买股票多次)。但是你不能同时进行多次交易(即,在你买入股票之前必须卖掉手里的股票)。

(ง •̀_•́)ง 1209

Leetcode 122. Best Time to Buy and Sell Stock II

Leetcode 122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete...

fengran_mark的博客 189

leetcode 122. Best Time to Buy and Sell Stock II 详解 python3

一.问题描述 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 complete as many transactions as you like (i.e....

CSerwangjun的博客 500

【LeetCode-Java实现】122. Best Time to Buy and Sell Stock II

122. Best Time to Buy and Sell Stock II题目描述解题思路实现代码 题目描述 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may...

IT Crowd的博客 244

[LeetCode] 122. Best Time to Buy and Sell Stock II

原题链接: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 1. 题目描述 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the ...

Ber03的博客 230

LeetCode-122. Best Time to Buy and Sell Stock II [C++][Java]

On each day, you may decide to buy and/or sell the stock. You can only holdat most oneshare of the stock at any time. However, you can buy it then immediately sell it on thesame day. Find and returnthemaximumprofit you can achieve.

贫道绝缘子的博客 284

Leetcode 188 Best Time to Buy and Sell Stock IV 至多k次买卖股票最大收益

假设你有一个数组,里面记录的是每一天的股票的价格。设计一个算法来计算最大收益。你至多可以完成k次交易。你不能同时进行多次交易(即,在你买入股票之前必须卖掉手里的股票)。

(ง •̀_•́)ง 2025
上一篇: 【LeetCode】解题121:Best Time to Buy and Sell Stock
下一篇: 【LeetCode】解题123:Best Time to Buy and Sell Stock III(动态规划)
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值