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

新英格兰10机39节点matlab建模,ieee10机39节点系统数据 【实例简介】ieee10机39节点系统数据,主要是BPA数据!【实例截图】【核心代码】ne_039_bus└── ne_039_bus├── 039readme.doc├── bpa│ ├── 039bpa.dat│ ├── 039bpa.swi│ ├── LFerr.LIS│ ├── OneNote 目录.onetoc2│ └── STERR.LIS├── eurostag│... 阅读详情

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+BC。因此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[i1]}
    其中 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 --- 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... 阅读详情

相关推荐

PCL Super4PCS算法实现点云粗配准(版本一)【2025最新版】

Win11系统下实现[Super4PCS: Fast Global Pointcloud Registration via Smart Indexing一文中的配准算法

点云侠的博客 3343

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 .

道纪书生的博客 228

leetcode题目:Best Time to Buy and Sell Stock II

leetcode题目:Best Time to Buy and Sell Stock II

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的博客 619

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

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

qq_43513394的博客 538

122. Best Time to Buy and Sell Stock II

leetcode 122

张小张的博客 329

leetcode 122. Best Time to Buy and Sell Stock II(买卖股票的最佳时机 II)--Java题解

题目描述 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。  示例 输入:[7, 1, 5, 3, 6, 4] 输出: 7 解释:在第2天买入,在第3天卖出,利润为4,在第4天买入,第5天卖出,利润为3,总利...

leyao的博客 245

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的博客 185

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的博客 1374

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

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

骆小坑的填坑之旅 872

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

聚沙成塔 531

[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的博客 293

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 185

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的博客 188

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

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

(ง •̀_•́)ง 1204

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的博客 495

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的博客 238

[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的博客 227

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.

贫道绝缘子的博客 282

Leetcode 123. Best Time to Buy and Sell Stock III 股票买卖3 解题报告

1 解题思想嗯,是的,已经第三题了,数组代表什么我不想再复述一次,自己看: Leetcode 122. Best Time to Buy and Sell Stock II 股票买卖2 解题报告 Leetcode 121. Best Time to Buy and Sell Stock 股票买卖 解题报告这道题的难点或者说同点在于: 只允许买卖两次这道题本质上用的是第一题的方法,我也看了下Di

MebiuW的专栏 3448

likeshop_家政系统开源版源码.rar

78962likeshop_家政系统开源版源码最新版likeshop上门家政服务源码,基于likeadmin-php开发的上门预约系统,提供全部前后台无加密源代码,拥有强大的地图定位、在线预约、系统派单、后台派单、下单支付、核销订单等功能模块,用户端和师傅端完美融合,前端Vue,uniapp测试环境:MySQL5.7,PHP8.0,框架Thinkphp,版本号2.1.1有小程序和H5端,商品服务价格可设置多规格,首页diy设置,随时随地都能接单;可自定义预约时间段,无需排队,降低时间成本;全方位满足用户需求支付支持微信,支付宝官方支付地图定位接口对接的腾讯地图储存支持本地和oss短信对接阿里云和腾讯云师傅端支持保证金和每日限单功能支持设置指定城市开放接单,适合本地运营搭建教程:·1.系统环境:MySQL5.7,PHP8.0(安装扩展fileinfo)·2.新建站点,选择新建数据库·3.把源码里面的server文件夹里面的文件打包上传到网站根目录解压4.运行目录设置为Public,伪静态设置为Thinkphp5.访问首页在线安装关闭移动端右下角调试图标教程根目录.env文件,把1改成false即可

上一篇: 【LeetCode】解题121:Best Time to Buy and Sell Stock
下一篇: 【LeetCode】解题123:Best Time to Buy and Sell Stock III(动态规划)
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值