LeetCode123: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, you must sell the stock before you buy again).
解题思路:
话说这题同前两题难度瞬间就拉开好多,哎,编程能力还是不行啊,如果不是谷歌各路大神解题报告http://blog.csdn.net/pickless/article/details/12034365,真心想不出来。
这题实际上用到了DP和分段的思想。
首先,根据题意,要求至少买卖两次(就因为有这限制,使得题目难度突然就增加了),所以,我们可以进行分段。
寻找一个点i,将原来的price[0..n-1]分割为price[0..i]和price[i..n-1],分别求两段的最大profit,可知分段就是使得买卖至少进行两次。
下面求price[0..i]和price[i..n-1]两段的最大profit时,利用了DP思想。
对于点i+1,求price[0..i+1]的最大profit时,很多工作是重复的,在求price[0..i]的最大profit中已经做过了。
类似于Best Time to Buy and Sell Stock,可以在O(1)的时间从price[0..i]推出price[0..i+1]的最大profit。
但是如何从price[i..n-1]推出price[i+1..n-1]?反过来思考,我们可以用O(1)的时间由price[i+1..n-1]推出price[i..n-1]。
最终算法:
数组l[i]记录了price[0..i]的最大profit,
数组r[i]记录了price[i..n]的最大profit。
已知l[i],求l[i+1]是简单的,同样已知r[i],求r[i-1]也很容易。
最后,我们再用O(n)的时间找出最大的l[i]+r[i],即为题目所求。
实现代码:
#include <iostream>
#include <vector>
using namespace std; /**
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). */ class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty())
return 0;
int n = prices.size();
int *l = new int[n];
int *r = new int[n];
l[0] = 0;
int lmin = prices[0];
for(int i = 1; i < n; i++)
{
lmin = min(prices[i],lmin);
l[i] = max(l[i-1], prices[i] - lmin);
} r[n-1] = 0;
int rmax = prices[n-1];
for(int i = n - 2; i >= 0; i--)
{
rmax = max(rmax, prices[i]);
r[i] = max(r[i+1], rmax - prices[i]);
} int maxprofit = 0;
for(int i = 0; i < n; i++)
{
maxprofit = max(maxprofit, l[i] + r[i]);
}
delete l;
delete r;
return maxprofit; }
}; int main(void)
{
int arr[] = {2,4,5,1,7,10};
int n = sizeof(arr) / sizeof(arr[0]);
vector<int> stock(arr, arr+n);
Solution solution;
int max = solution.maxProfit(stock);
cout<<max<<endl;
return 0;
}
LeetCode123:Best Time to Buy and Sell Stock III的更多相关文章
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- 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 ...
- Best Time to Buy and Sell Stock | & || & III
Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...
- 【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 ...
- 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 al ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- 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 ...
- [leetcode]123. 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 al ...
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
随机推荐
- 2015 年最受 Linux 爱好者欢迎的软硬件大盘点
Linux 爱好者都喜欢用哪些硬件,哪些发行版呢?近日 OpenBenchmarking.org 做了一个 2015 年度数据的统计和梳理,Linux Story 特意整理了一下,分享给大家. 转载于 ...
- WebApi系列~在WebApi中实现Cors访问
回到目录 说在前 Cors是个比较热的技术,这在蒋金楠的博客里也有体现,Cors简单来说就是“跨域资源访问”的意思,这种访问我们指的是Ajax实现的异步访问,形象点说就是,一个A网站公开一些接口方法, ...
- Composer使用
是什么 如果你知道yum.apt-get.npm.bower等命令中的一种或者多种,那么,你也能很快知道composer是什么了.没错,它就是PHP里快速安装类库的.平时,我们安装一个PHP类库,需要 ...
- JS中的宽高(基础知识很重要)
IE中:document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度document.docu ...
- Atitti 数据库事务处理 attilax总结
Atitti 数据库事务处理 attilax总结 1.1. 为什么要传递Connection?1 1.2. 两种事务处理方式,一种是编程式事务处理;一种是声明...2 1.3. 事务隔离级别 2 1. ...
- iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍
UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...
- ASP.NET MVC3 模板页的使用
占位符的使用: 下面是一个模板页 _Layout.cshtml <!DOCTYPE html> <html> <head> @RenderSection(" ...
- 每天一个linux命令(43):killall命令
Linux系统中的killall命令用于杀死指定名字的进程(kill processes by name).我们可以使用kill命令杀死指定进程PID的进程,如果要找到我们需要杀死的进程,我们还需要在 ...
- java.util.Scanner简单应用
import java.util.Scanner; import java.io.*; public class FileScannerTest{ public static void main(St ...
- Spark入门实战系列--4.Spark运行架构
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 1. Spark运行架构 1.1 术语定义 lApplication:Spark Appli ...