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. BZOJ 1342: [Baltic2007]Sound静音问题 | 单调队列维护的好题

    题目: 给n个数字,一段合法区间[l,l+m-1]要求max-min<=c 输出所有合法区间的左端点,如果没有输出NONE 题解: 单调队列同时维护最大值和最小值 #include<cst ...

  2. JQuery选择符的理解与应用

    JQuery强大的选择符可以让我们获得页面中任何元素进行操作,并且使用简单方便,可读性强.本章内容根据本人在开发中常用到的选择符作为例子来进行讲解,如有更多常用的简单的例子可回复提供,参与讨论,一起学 ...

  3. noip模拟赛 保留道路

    [问题描述] 很久很久以前有一个国家,这个国家有N个城市,城市由1,2,3,…,N标号,城市间有M条双向道路,每条道路都有两个属性g和s,两个城市间可能有多条道路,并且可能存在将某一城市与其自身连接起 ...

  4. HDU4081:Qin Shi Huang's National Road System (任意两点间的最小瓶颈路)

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  5. Coconuts HDU - 5925 二维离散化 自闭了

    TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, ...

  6. bzoj 4900 [CTSC2017]密钥 模拟+乱搞

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4900 #include<cstring> #include<cmath&g ...

  7. CodeVS 1017 DP

    1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描 ...

  8. 大道至简第一章伪代码读后感o(╯□╰)o

    import.java.io*; import class duhougan; public static void main(Striing arges[]){ system.out.println ...

  9. [技巧篇]02.关于MyBatis存取图片到MySQL数据Blob字段

  10. 【JSP EL】EL表达式获取当前时间(两种方式)

    第一种方式: //先在代码段定义<% long date = new Date().getTime(); request.setAttribute("date", date) ...