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. 用Matlab对数据进行线性拟合算法

    http://www.cnblogs.com/softlin/p/5965939.html 挖坑

  2. Redis、Memcache

    ★ Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sor ...

  3. 【HASH】【UVA 10125】 Sumset

    传送门 Description 给定一个整数集合S,求一个最大的d,满足a+b+c=d,其中a,b,c,d∈S Input 多组数据,每组数据包括: 第一行一个整数n,代表元素个数 下面n行每行一个整 ...

  4. IE的CSS渲染跟其它浏览器有什么不同

    由于IE系浏览器对标准的支持不够好,导致Web开发中经常需要去处理浏览器兼容性问题,特别有些莫名其妙的问题很让人头疼,今天要说这个问题就是这样的,先从插入CSS的三种方法说起: 外部样式(Extern ...

  5. 解决requests获取源代码时中文乱码问题

    用requests获取源代码时,如果是中文网页,就可能会出现乱码,下面我以中关村的网站为例: import requests url = 'http://desk.zol.com.cn/meinv/' ...

  6. TCP的连接(三次握手)和释放(四次挥手)

    1 http都设置哪些header? http协议规定:一个完整的客户端发送给服务端的HTTP请求包括: (1)请求行:包括了请求方法.请求资源路径.HTTP协议版本,eg:GET/Server/im ...

  7. # Codeforces Round #529(Div.3)个人题解

    Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...

  8. 顺序统计:寻找序列中第k小的数

    最直观的解法,排序之后取下标为k的值即可. 但是此处采取的方法为类似快速排序分块的方法,利用一个支点将序列分为两个子序列(支点左边的值小于支点的值,支点右边大于等于支点的值). 如果支点下标等于k,则 ...

  9. Android UI开发第二十四篇——Action Bar

    Action bar是一个标识应用程序和用户位置的窗口功能,并且给用户提供操作和导航模式.在大多数的情况下,当你需要突出展现用户行为或全局导航的activity中使用action bar,因为acti ...

  10. Leetcode:52. N-QueensII

    Description Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...