Best Time to Buy and Sell Stock1,2,3,4
找到最低值和最高值
int maxProfit(vector<int>& prices) {
if(prices.size()<)return ;
int profit=;
int cur_min=prices[];
for(int i=;i<prices.size();i++)
{
profit=max(profit,prices[i]-cur_min);//记录最大利润
cur_min=min(cur_min,prices[i]);//保留购买最小值
}
return profit;
}
2、
计算差分序列,大于0加入
int maxProfit(vector<int>& prices) {
if(prices.size()<)return ;
int profit=;
int diff=;
for(int i=;i<prices.size();i++)
{
diff=prices[i]-prices[i-];
if(diff>)profit+=diff;
}
return profit;
}
3、
把交易分成两次,分别完成,最后将利润相加求最大。
public int maxProfit(int[] prices) {
if (prices.length < ) return ; int n = prices.length;
int[] preProfit = new int[n];
int[] postProfit = new int[n]; int curMin = prices[];
for (int i = ; i < n; i++) {
curMin = Math.min(curMin, prices[i]);
preProfit[i] = Math.max(preProfit[i - ], prices[i] - curMin);
} int curMax = prices[n - ];
for (int i = n - ; i >= ; i--) {
curMax = Math.max(curMax, prices[i]);
postProfit[i] = Math.max(postProfit[i + ], curMax - prices[i]);
} int maxProfit = ;
for (int i = ; i < n; i++) {
maxProfit = Math.max(maxProfit, preProfit[i] + postProfit[i]);
} return maxProfit;
}
4、
Description: 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 k transactions. Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
public int maxProfit(int k, int[] prices) {
if (prices.length < ) return ; int days = prices.length;
if (k >= days) return maxProfit2(prices); int[][] local = new int[days][k + ];
int[][] global = new int[days][k + ]; for (int i = ; i < days ; i++) {
int diff = prices[i] - prices[i - ]; for (int j = ; j <= k; j++) {
local[i][j] = Math.max(global[i - ][j - ], local[i - ][j] + diff);
global[i][j] = Math.max(global[i - ][j], local[i][j]);
}
} return global[days - ][k];
} public int maxProfit2(int[] prices) {
int maxProfit = ; for (int i = ; i < prices.length; i++) {
if (prices[i] > prices[i - ]) {
maxProfit += prices[i] - prices[i - ];
}
} return maxProfit;
}
参考:http://liangjiabin.com/blog/2015/04/leetcode-best-time-to-buy-and-sell-stock.html
可以交易k次,没看懂,感觉自己好笨。。。
Best Time to Buy and Sell Stock1,2,3,4的更多相关文章
- CF867E: Buy Low Sell High(贪心, STL) (hdu6438)
Description 有nn个城市,第ii个城市商品价格为aiai,从11城市出发依次经过这nn个城市到达n n城市,在每个城市可以把手头商品出售也可以至多买一个商品,问最大收益. Input 第 ...
- Lintcode393 Best Time to Buy and Sell Stock IV solution 题解
[题目描述] Say you have an array for which the i th element is the price of a given stock on day i. Desi ...
- leetcode 【 Best Time to Buy and Sell Stock II 】python 实现
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [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 al ...
- [LeetCode] 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 al ...
- [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LintCode] 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 al ...
随机推荐
- LISA介绍及其使用方法
LISA是ARM公司开发的一款开源工具.在内核开发过程中,苦于无法针对修改内容进行一些量化或者可视化结果的测量,而无感.LISA对于模型调优,回归测试都有较强的支持. 什么是LISA? LISA是Li ...
- python基础补漏-06-内置模块
1> sys 模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的 ...
- Block 代码快
1.原理 1.1block类型 a.全局bock块 贯彻整个程序 b.栈块 存在于栈内存中,作用域中使用 c.堆块 自行管理其内存 注*:http://blog.parse.com/learn ...
- Android源码——AsynTask
AsyncTask<Params, Progress, Result>中三个参数为: Params 输入数据 Progress 过程数据 Result ...
- Hibernate Hql 总结(2)---laoyang
package com.etc.test; import java.util.Iterator; import java.util.List; import org.hibernate.Query; ...
- 深入研究HTTP协议以及部分应用
引言 工作了一段时间,都是在开发网页,自然和http打交道打得最多了,投身工作之后原来理解的东西又变得模糊,所以有必要深入探讨一下http协议的细节,也借此总结一下学习的成果. HTTP相关认识 对H ...
- ADB
adb logcat 命令详解 log过滤 http://blog.csdn.net/liao277218962/article/details/50129009 如何使用 adb logcat 查看 ...
- 【先定一个小目标】windows下安装RabbitMQ消息服务器
RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. 1:安装RabbitMQ 需要先安装Erlang语言开发包.下载地址 ...
- runtime-给系统已有类添加属性
在没有接触runtime之前,我们接触到的能给类进行扩展的方法有类目(category)和延展(extension)两种.类目(category)可以给系统已有类添加扩展方法但是不能添加属性,并且被添 ...
- Android中AIDL的理解与使用(二)——跨应用绑定Service并通信
跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...