leetcode 121 122 123 . Best Time to Buy and Sell Stock
121题目描述:
解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想。
class Solution {
public:
int maxProfit(vector<int>& prices) { if(prices.size() == 0)
return 0; int min_prices = prices[0];
int max_profit = 0; for(int i = 1 ; i < prices.size(); i++ ){
if( prices[i] < min_prices)
min_prices = prices[i];
else{
max_profit = (prices[i] - min_prices) > max_profit ? prices[i] - min_prices : max_profit;
}
}
return max_profit;
}
};
122 可以重复的进行买入卖出,但是卖出时间必须在买入时间之后
思路: 划分最小的时间粒度进行买卖,即相邻两天,相邻两天只要买入卖出有收益则进行买卖,否则不进行买卖
class Solution {
public:
int maxProfit(vector<int>& prices) {
int max_profit = ;
for(int i = ; i < prices.size() ; i++){
max_profit += prices[i] - prices[i-] > ? prices[i] -prices[i-] : ;
}
return max_profit; }
};
123 最多进行两次买卖后的最大收益
以第i天对n个时间进行划分两半 分别计算两部分的最大收益 再进行加和 但是时间复杂度为 O(n2) 超时!!!!
public class Solution {
public int maxProfit(int[] prices) {
int ans = ;
for(int m = ; m<prices.length; m++){
int tmp = maxProfitOnce(prices, , m) + maxProfitOnce(prices, m, prices.length-);
if(tmp > ans) ans = tmp;
}
return ans;
} public int maxProfitOnce(int[] prices,int start, int end){
if(start >= end) return ;
int low = prices[start];
int ans = ;
for(int i=start+; i<=end; i++){
if(prices[i] < low) low = prices[start];
else if(prices[i] - low > ans) ans = prices[i] - low;
}
return ans;
} }
利用网上动态规划的思想来做
我们定义local[i][j]为在到达第i天时最多可进行j次交易并且最后一次交易在最后一天卖出的最大利润,此为局部最优。然后我们定义global[i][j]为在到达第i天时最多可进行j次交易的最大利润,此为全局最优。它们的递推式为: 维护了两个递推变量
local[i][j]=max(global[i-1][j-1]+max(diff,0),local[i-1][j]+diff) 局部最优中的local[i-1][j] + diff 可以看为在第i-1天卖出后又买入在第i天又卖出的操作,所有和总的买卖次数还是j次,所以diff无论正负都得加入。
global[i][j]=max(local[i][j],global[i-1][j]),
其中局部最优值是比较前一天并少交易一次的全局最优加上大于0的差值,和前一天的局部最优加上差值中取较大值,而全局最优比较局部最优和前一天的全局最优。
class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.empty()) return ;
int n = prices.size(), g[n][] = {}, l[n][] = {};
for (int i = ; i < prices.size(); ++i) {
int diff = prices[i] - prices[i - ];
for (int j = ; j <= ; ++j) {
l[i][j] = max(g[i - ][j - ] + max(diff, ), l[i - ][j] + diff);
g[i][j] = max(l[i][j], g[i - ][j]);
}
}
return g[n - ][];
}
};
leetcode 121 122 123 . Best Time to Buy and Sell Stock的更多相关文章
- LeetCode(122) 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 ...
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...
- 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 ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 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】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】36、121题,Best Time to Buy and Sell Stock
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- LeetCode OJ 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 ...
- [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 ...
- 123. Best Time to Buy and Sell Stock III ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- [洛谷P1341]无序字母对
题目大意:给一张无向图,找一条字典序最小的欧拉路径 题解:若图不连通或有两个以上的奇数点,则没有欧拉路径,可以$dfs$,在回溯时把这个节点加入答案 卡点:没有在回溯时加入答案,导致出现了欧拉路径没走 ...
- JavaScript中的valueOf与toString方法
基本上,所有JS数据类型都拥有valueOf和toString这两个方法,null除外.它们俩解决javascript值运算与显示的问题. JavaScript 的 valueOf() 方法 valu ...
- LVM分区
使用LVM对磁盘进行初始化 pvcreate /dev/vdd 创建卷组 vgcreate vg /dev/vdd 备注:vg是卷组的名称,可改变. 查看卷组的详细信息 vgdisplay 下图是我执 ...
- 使用snmp4j实现Snmp功能(二)
相关链接:Snmp学习笔记使用snmp4j实现Snmp功能(一)使用snmp4j实现Snmp功能(二)使用snmp4j实现Snmp功能(三) 前一篇文章讲了如何用snmp4j实现set和get的功能, ...
- Java日期时间实用工具类
Java日期时间实用工具类 1.Date (java.util.Date) Date(); 以当前时间构造一个Date对象 Date(long); 构造函数 ...
- HDU1394 逆序数
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- X210(s5pv210)中断系统
1.SoC对中断的实现机制:异常向量表 (1)异常向量表是CPU中某些特定地址的特定定义.当中断发生的时候,中断要想办法通知CPU去处理中断,怎么做到?这就要靠异常向量表.(2)在CPU设计时,就事先 ...
- [技巧篇]04.使用PowerDesigner逆向生成
- sun.security.x509.CertAndKeyGen;找不到
导入已有项目编译时出错,报: import sun.security.x509.CertAndKeyGen;找不到 而这个包属于sun公司的jar包.不是项目本身的问题,而是开发环境的问题. 最后原因 ...
- 【C++对象模型】第一章 关于对象
1.C/C++区别 C++较之C的最大区别,无疑在于面向对象,C程序中程序性地使用全局数据.而C++采用ADT(abstract data tpye)或class hierarchy的数据封装.类相较 ...