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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. 【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 ...

  6. 【LEETCODE】36、121题,Best Time to Buy and Sell Stock

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. hdu ACM Steps Section 1 花式A+B 输入输出格式

    acm与oi很大的一个不同就是在输入格式上.oi往往是单组数据,而acm往往是多组数据,而且题目对数据格式往往各有要求,这8道a+b(吐槽..)涉及到了大量的常用的输入输出格式.https://wen ...

  2. BZOJ1058:[ZJOI2007]报表统计——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1058 https://www.luogu.org/problemnew/show/P1110#su ...

  3. BZOJ2553 [BeiJing2011]禁忌 【AC自动机 + dp + 矩乘优化】

    题目链接 BZOJ2553 题解 话说在前,此题卡精度,最好开long double 先建\(AC\)自动机 求期望,逆着求,设\(f[i][j]\)为长度为\(i\)的串,当前匹配AC自动机\(j\ ...

  4. train loss与test loss结果分析(接利用caffe的solverstate断点训练)

    train loss 不断下降,test loss不断下降,说明网络仍在学习; train loss 不断下降,test loss趋于不变,说明网络过拟合; train loss 趋于不变,test ...

  5. Android之简易音乐播发器

    布局主要代码之ListView: <span style="font-size:14px;"> <ListView android:id="@+id/m ...

  6. Cppcheck代码分析上

    1.检查点 1.自动变量检查: 返回自动变量(局部变量)指针: 2.越界检查:数组越界返回自动变量(局部变量)指针: 3.类检查:构造函数初始化: 4.内存泄露检查:  5.空指针检查: 6.废弃函数 ...

  7. asp.net 权限管理系统

    asp.net webform ,基于组织机构.角色的权限管理系统. 网上找的,挺好.随拿来分享. https://bitbucket.org/zzhi/asp.net

  8. jQuery拖拽 & 弹出层

    了解更多请查看 官网 和 API iDrag & iDialog 介绍 特点: iDialog.js依赖于jquery编写的简单易用的对话框,同时还可以通过添加css3,改变对话框的展现动画. ...

  9. Spring Cloud的基本认识和使用Spring Cloud的基本教程(山东数漫江湖)

    认识Spring Cloud Spring Cloud简单来说就是一个微服务相关的框架,至于什么是微服务,简单来说就是一个整体项目由多个单独运行的小项目构成,每个小项目负责一个或多个功能,每个小项目有 ...

  10. 基于 python imageai 对象检测 目标检测 识别 视频

    1.视频连接如下: http://www.iqiyi.com/w_19s6vownit.html