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 ...
随机推荐
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- GDB调试汇编堆栈过程分析
GDB调试汇编堆栈过程分析 分析过程 这是我的C源文件:click here 使用gcc - g example.c -o example -m32指令在64位的机器上产生32位汇编,然后使用gdb ...
- CF719E(线段树+矩阵快速幂)
题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...
- Docker命令学习
今天更换腾讯云系统的时候发现了多了个CoreOS,据说是专门运行docker的轻量系统,顺便学习一下docker命令. 1. docker version 显示 Docker 版本信息. 2. doc ...
- 我们常用,却容易忽视——CSS的BFC(Block formatting contexts)
BFC——一个我们容易忽视掉的布局神器 今天给大家说说BFC这个概念,在说概念前,先给大家看个例子: 首先,定义三个div块元素 效果: 我们发现,块级元素的排列顺序是从上往下,一块接着一块,在w ...
- js中类型识别的方法
第一种方法typeof typeof是一种运算符,它的值有以下几种 <!DOCTYPE html> <html lang="en"> <head> ...
- vuex(1.0版本写法)
Vuex 是一个专门为 Vue.js 应用所设计的集中式状态管理架构. 官方文档:http://vuex.vuejs.org/zh-cn/ 2.0和1.0都能在此找到 每一个 Vuex 应用的核心就 ...
- bzoj4621: Tc605
应要求写一下这个题的题解. 我的DP很奥(奇)妙(怪),不过跟标算还是殊途同归的(反正怎么做都行……) 先讲一下奥妙的性质吧. 首先,在最终序列中,每个数最多出现一段,并且,对于出现的数,每段数两两之 ...
- c++中的lambda特性
来源: http://www.jb51.net/article/56147.htm cocos2dx3.0测试: 1.有参数a,b printf("lambda=%lf", []( ...