Best Time to Buy and Sell Stock III 解题思路

题目要求:

最多交易两次,并且只能买卖完之后再买。

总思路:

在数组中找一个适当的点i,使得profit之和最大。

思路:

1、从左往右扫描,left[i]记录包括i元素以内的左部的maxprofit,用Best Time to Buy and Sell Stock 1可得到。

2、从右往左扫描,right[i]记录i+1到尾部元素的maxprofit,将上面的low改成big即可

3、返回max(left[i],right[i])

时间: 2024-08-28 22:26:27

Best Time to Buy and Sell Stock III 解题思路的相关文章

LeetCode: Best Time to Buy and Sell Stock III 解题报告

Best Time to Buy and Sell Stock IIIQuestion SolutionSay 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 en

Best Time to Buy and Sell Stock III

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 maximum profit. You may complete at most two transactions. c++版本代码: class Solution { public: i

leetcode——Best Time to Buy and Sell Stock III 买卖股票最大收益(AC)

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,

【leetcode】Best Time to Buy and Sell Stock III

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 maximum profit. You may complete at most two transactions. Note:You may not engage in multiple

LeetCode 笔记23 Best Time to Buy and Sell Stock III

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 maximum profit. You may complete at most two transactions. 现在A股涨这么好,要是股票都提前知道价格就好了(@[email pro

123. Best Time to Buy and Sell Stock III

/* * 123. Best Time to Buy and Sell Stock III * 2016-5-21 by Mingyang * 根据题目要求,最多进行两次买卖股票,而且手中不能有2只股票,就是不能连续两次买入操作. * 所以,两次交易必须是分布在2各区间内,也就是动作为:买入卖出,买入卖出. * 进而,我们可以划分为2个区间[0,i]和[i,len-1],i可以取0~len-1. * 那么两次买卖的最大利润为:在两个区间的最大利益和的最大利润. * 一次划分的最大利益为:Prof

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

[题目] 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

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

LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法

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,