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 ...
随机推荐
- 【MySQL】函数IFNULL、设置默认时间
MySql 函数 IFNUll用法说明 IFNULL(expr1,expr2) 如果 expr1 不是 NULL,IFNULL() 返回 expr1,否则它返回 expr2. IFNULL()返回一个 ...
- 【BZOJ 2595】【WC 2008】游览计划
http://www.lydsy.com/JudgeOnline/problem.php?id=2595 斯坦纳树的例题诶...我怎么做了好长时间_(:з」∠)_ 首先这是一棵树. 状压表示状态,\( ...
- android studio 提示翻译
1. you can import your settings from a previous version of Studio可以导入您的设置从先前版本的工作室 2. I want to impo ...
- volatile修饰符
Volatile 修饰的成员变量在每次被线程访问时,都强制从共享内存中重新读取该成员变量的值.而且,当成员变量发生变化时,会强制线程将变化值回写到共享内存.这样在任何时刻,两个不同的线程总是看到某个成 ...
- 二.持续集成之--WEB后台
1.系统管理-系统设置:把linux服务器加进去 2.General配置 3.源码管理: 4.构建触发器 5.构建环境 6.构建 7.构建后操作
- sass 安装、配置,css规则
http://blog.csdn.net/oyuemijindu/article/details/51036096 --sass 安装 一安装 1.ruby下载,可以到官网下载 ,注意如果是系统如果 ...
- linux安装nexus
一.下载nexus 最新地址在:https://www.sonatype.com/download-oss-sonatype 我下载的是:nexus-2.14.1-01-bundle.tar.gz,是 ...
- QEMU/KVM虚拟机安装配置
1.安装相关组件: [root@KVM ~]# yum install qemu-img qemu-kvm qemu-kvm-tools virt-manager virt-viewer virt-v ...
- python 之 logging
#coding=utf-8 import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename ...
- Java中创建对象的5种方式
作为Java开发者,我们每天创建很多对象,但我们通常使用依赖管理系统,比如Spring去创建对象.然而这里有很多创建对象的方法,我们会在这篇文章中学到. Java中有5种创建对象的方式,下面给出它们的 ...