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

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... 阅读详情

1 解题思想

嗯,是的,已经第三题了,数组代表什么我不想再复述一次,自己看:
Leetcode 122. Best Time to Buy and Sell Stock II 股票买卖2 解题报告
Leetcode 121. Best Time to Buy and Sell Stock 股票买卖 解题报告

这道题的难点或者说同点在于:
只允许买卖两次

这道题本质上用的是第一题的方法,我也看了下Discuss,用的也基本一样。

所谓用第一套题是什么意思,是什么动态规划思想?其实就是用两次题目1中的方式,但是第二次买卖需要加上当前第一次利润的状态?

不信 你看代码就知道

2 原题

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 at most two transactions.

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

3 AC解

public class Solution {
    public int maxProfit(int[] prices) {
        /**
         * 同Discuss的主流经典高效解法,相当于第一题的加强版
         * 初始状态
         * */
        int buy1 = Integer.MIN_VALUE,sell1 = 0;
        int buy2 = Integer.MIN_VALUE,sell2 = 0;
        for(int price : prices){
            // 第一次购买
            if ( buy1 < -price) buy1 = -price;
            // 第一次卖出去的可能的最大利润
            if ( sell1 < buy1 + price ) sell1 = buy1 + price;
            //注意第二次的利润已经包含第一次的了
            //同理,第二次购买,注意这里是
            if ( buy2 < sell1 - price) buy2 = sell1 - price;
            //最后一次
            if ( sell2 < buy2 + price ) sell2 = buy2 + price;
        }
        return sell2;
    }
}

3 AC解

public class Solution {
    public int maxProfit(int[] prices) {
        /**
         * 同Discuss的主流经典高效解法,相当于第一题的加强版
         * 初始状态
         * */
        int buy1 = Integer.MIN_VALUE,sell1 = 0;
        int buy2 = Integer.MIN_VALUE,sell2 = 0;
        for(int price : prices){
            // 第一次购买,始终买当前所有里面最便宜的那一次,第一次买,利润肯定为负,但是要找出最少的那一个
             if ( buy1 < -price) buy1 = -price;
            // 第一次卖出去的可能的最大利润,到这里都原理同第一题
            if ( sell1 < buy1 + price ) sell1 = buy1 + price;
            //注意第二次的利润已经包含第一次的了,这也是唯一一个不一样的难点
            //同理,第二次购买,但是注意这里有个状态的加成 sell1
            if ( buy2 < sell1 - price) buy2 = sell1 - price;
            //最后一次
             if ( sell2 < buy2 + price ) sell2 = buy2 + price;
        }
        return sell2;
    }
}
104报文解析工具 可选择包含报文的文本文件或日志文件进行解析,可选择按条数范围或时间范围,输出结果可以在报文解析显示区,也可以输出到文件。可以对文本内容的报文文件进行高级分析,包括遥信遥测等通讯数据的具体信息,通讯帧的统计,通讯次数的统计,各种错误的分析,报文的定位功能,传输的文件具体内容等。输入完成后点击工具栏的解析报文按钮或使用右键菜单的解析就可以对报文进行解析。在报文输入区输入待解析报文并解析后,解析结果就显示在报文解析显示区和报文定位展示区及报文定位条目区。当有多条报文解析后,可以通过报文列表树具体定位某条报文。 阅读详情

相关推荐

VMware报错系列:虚拟机配置文件(.vmx)损坏或丢失---修复

虚拟机配置文件(.vmx)损坏或丢失---修复

weixin_54626591的博客 6968

Leetcode 122. Best Time to Buy and Sell Stock II 股票买卖2 解题报告

1 解题思想首先请看下第一个问题Leetcode 121. Best Time to Buy and Sell Stock 股票买卖 解题报告 题目给定的数组还是一样的含义,依旧代表股票的价格,但是现在不同的是,现在你可以任意次数的买卖(当然还是要先买后卖才行,不能买两次再卖等)这道题目我的解法很naive,在第i天,只要第i+1天的售价高于第i天,那么我就买,然后i+1天卖了。关于其中原

MebiuW的专栏 1143

模糊控制温度_模糊PID温度_模糊pid_模糊控制_模糊温度_nearest9eu_

模糊控制pid温度控制,有需要的可以下载看看,可以用于大家参考学习,一起进步

【力扣解题报告】:(数组)买卖股票的最佳时机

最近由于基金回调严重,一个星期亏掉1w,刷题股票,希望股市红红火火,而不是青山绿水 给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。 返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。 解题思路1:通过双重循环遍历出所有的差值,取最大值,时间复杂度,时间复杂度为O(n^2),空间复杂度:O(1)。 clas

张不帅 521

Leetcode 123 Best Time to Buy and Sell Stock III 至多两次买卖股票最大收益

假设你有一个数组,里面记录的是每一天的股票的价格。 设计一个算法来计算最大收益。你至多可以完成两次交易

(ง •̀_•́)ง 6534

股票问题(二)

一、 Best Time to Buy and Sell Stock IISay 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 as many ...

Jae_Wang的博客 846

LeetCode 123.买卖股票的最佳时机 III

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

spring_3_shine的博客 4991

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

LeetCodeBest Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求最大的收益。分析:动态规划法。从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求。**代码:**O(n)时

跳出温水的青蛙 2万+

LeetCode 123. Best Time to Buy and Sell Stock III动态规划

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

da_kao_la的博客 341

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

LeetCode 123Best Time to Buy and Sell Stock III 三维DP完全解析

当 j=2 时,j+1=3>K,就没有「还能卖 3 次」这个状态,所以不能通过「卖出」到达 dp[i][2][0],只能从「昨天不持股、还能卖 2 次」继承。这 4 个状态可以理解为把三维 dp[i][j][k] 中真正会被用到的那几类状态抽出来,省去了显式的 i 维和 j 维,从而把空间压缩到 O(1)。注意:只有在 j+1≤2 时,第二项才有效,否则不存在 dp[i-1][j+1],这时直接视为「不可能」,不会取。先用三维 dp[i][j][k] 把「时间 / 交易次数 / 持仓状态」都写清楚。

六六哥的博客 1192

[LeetCode] 123. Best Time to Buy and Sell Stock III

原题链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 1. 题目简介 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...

Ber03的博客 290

LeetCode 123. Best Time to Buy and Sell Stock III股票买卖

原题网址:https://leetcode.com/problems/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 day i. Design an algorithm to find the

James Pan 710

LeetCode 股票买卖系列 best time to buy and sell stock

LeetCode 股票买卖系列 best time to buy and sell stock

andyL_05的博客 534

LeetCode Best Time to Buy and Sell Stock III

原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 本题是Best Time to Buy and Sell Stock的进阶版。思路也很相似,仍旧是维护一个全局最优,一个局部最优。 这里最多可以进行k次交易,让后本题把k变成2即可。全局最优global是到达第i天可以最多进行j次交易的利润是多少

Dylan_Java_NYC的博客 619

LeetCode123. Best Time to Buy and Sell Stock III 解题报告(Python)

给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。 注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 示例 1: 输入: [3,3,5,0,0,3,1,4] 输出: 6 解释: 在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。 随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候

L141210113的专栏 658

123 - Best Time to Buy and Sell Stock III

123 - Best Time to Buy and Sell Stock III

uso的博客 193

leetcode 123. Best Time to Buy and Sell Stock III 股票最多两次买入、两次卖出的最大利润

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 数组的最大2子段和,允许在一天内买入又卖出,相当于不交易。因为题目规定的是最多交易两次,而不是必须交易两次。 class Solution { public: int maxProfit(vector<int>& prices)...

rock4you 308

LeetCodeBest Time to Buy and Sell Stock总结

LeetCode买卖股票问题如下: 121. Best Time to Buy and Sell Stock 122. Best Time to Buy and Sell Stock II 123. Best Time to Buy and Sell Stock III 188. Best Time to Buy and Sell Stock IV 309. Best Time to...

天行健,君子以自强不息 427
上一篇: Leetcode 122. Best Time to Buy and Sell Stock II 股票买卖2 解题报告
下一篇: Leetcode 415. Add Strings 字符串加法 解题报告
学术状态抽奖器
博客等级 码龄14年 249粉丝 270原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值