描述:

给一些列数字,表示每条股票的价格,如果可以买卖一次(不能同一天买和卖),求最大利益(即差最大)。

其他三道问题是,如果能买卖无限次,买卖两次,买卖k次。

题一:

实质是求后面一个数减前一个数的最大差值。

维护一个最小值,和当前最大值。只需遍历一次,空间也是常数。

int maxProfit(vector<int>& prices) {
if (prices.size() < )
return ;
int min_ = prices[];
int ret = ; for (int i = ; i < prices.size(); i++) {
ret = max(ret, prices[i] - min_);
min_ = min(min_, prices[i]);
}
return ret;
}

题二:

只要是后一个数比前一个大,都增。

int maxProfit(vector<int>& prices) {
if (prices.size() < )
return ; int ret = ;
for (int i = ; i < prices.size() - ; i++) {
ret += max(prices[i + ] - prices[i], );
}
return ret;
}

题三:

可进行两次操作。

其中一个思路,可以关注分界点,可以枚举分界点,求左右两边的最优操作,在LeetCode会超时,显然,复杂度n^2。

思考下优化,我们可以计算每个点的最大值,左边不用重复计算,每次分界点往左移,都像题一那样计算最大值即可;

      而右边,其实可以反向计算一遍,但是,右边改成求最小值。

      最后加起来即可。

int maxProfit(vector<int>& prices) {
int size = prices.size();
if (size < )
return ;
int* left = new int[size]{};
int* right = new int[size]{};
int ret = ; int lmin = prices[];
int lmax = ;
for (int i = ; i < size; i++) {
lmax = max(lmax, prices[i] - lmin);
left[i] = lmax;
lmin = min(lmin, prices[i]);
} int rmin = ;
int rmax = prices[size - ];
for (int i = size - ; i >= ; i--) {
rmin = min(rmin, prices[i] - rmax);
right[i] = -rmin;
rmax = max(rmax, prices[i]);
} for (int i = ; i < size - ; i++) {
ret = max(ret, left[i] + right[i + ]);
}
return max(ret, left[size - ]);
}

思路二:

int maxProfit(vector<int>& prices) {

    int n = prices.size();
if(n==) return ;
int sell1 = , sell2 = , buy1 = INT_MIN, buy2 = INT_MIN; for(int i =; i<n; i++)
{
buy1 = max(buy1, -prices[i]);
sell1 = max(sell1, prices[i]+buy1);
buy2 = max(buy2, sell1-prices[i]);
sell2 = max(sell2, prices[i]+buy2);
}
return sell2;
}

题四:

动态规划:

其中diff表示今天和昨天的差。

global[i][j] = max(local[i][j], global[i-1][j])

local[i][j] = max(global[i-1][j-1] + max(diff,0), local[i-1][j] + diff)

local[i][j]表示最后一次卖出在今天的最大利益,局部最优。

global[i][j]表示全局最优。

第一条式子:要么在今天卖出最优,要么前一天的全局最优。

第二条式子:前者为之前的全局最优加上最后一次交易在今天。

        注意diff,我们要的是不大于j的交易次数;

        如果i - 1天还持有,则i天卖出,共j - 1次操作;如果i-1天不持有,则i - 1天买入,i天卖出,共j次操作。

      后者为i - 1天卖出加上今天diff,表示i - 1天还持有,加上今天的。

int maxProfit(int k, vector<int>& prices) {
if (prices.size() < ) return ; int days = prices.size();
if (k >= days) return maxProfit2(prices); auto local = vector<vector<int> >(days, vector<int>(k + ));
auto global = vector<vector<int> >(days, vector<int>(k + )); for (int i = ; i < days ; i++) {
int diff = prices[i] - prices[i - ]; for (int j = ; j <= k; j++) {
local[i][j] = max(global[i - ][j - ], local[i - ][j] + diff);
global[i][j] = max(global[i - ][j], local[i][j]);
}
} return global[days - ][k];
} int maxProfit2(vector<int>& prices) {
int maxProfit = ; for (int i = ; i < prices.size(); i++) {
if (prices[i] > prices[i - ]) {
maxProfit += prices[i] - prices[i - ];
}
} return maxProfit;
}

类似题三的做法:

int maxProfit(int k, vector<int>& prices) {

    int n = prices.size();

    if(k>n/)
{
int buy = INT_MIN, sell = ;
for(int i=; i<n; i++)
{
buy = max(buy, sell-prices[i]);
sell = max(sell, buy+prices[i]);
}
return sell;
} vector<int> sell(k+, );
vector<int> buy(k+, ); for(int i=; i<=k; i++) buy[i] = INT_MIN; for(int i=; i<n; i++)
{
for(int j=; j<k+; j++)
{ buy[j] = max(buy[j], sell[j-]-prices[i]);
sell[j] = max(sell[j], buy[j]+prices[i]); }
}
return sell[k];
}

leetcode 121 股票买卖问题系列的更多相关文章

  1. [LeetCode]121、122、309 买股票的最佳时机系列问题(DP)

    121.买卖股票的最佳时机 题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意 ...

  2. leetcode 121

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  3. 30. leetcode 121. Best Time to Buy and Sell Stock

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  4. LeetCode 121. 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 ...

  5. LeetCode算法扫题系列19

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9104677.html LeetCode算法第19题(难度:中等) 题目:给定一个链表,删 ...

  6. LeetCode算法扫题系列83

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9104582.html LeetCode算法第83题(难度:简单) 题目:给定一个排序链表 ...

  7. [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  8. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  9. [LeetCode] Matrix 值修改系列,例题 Surrounded Regions,Set Matrix Zeroes

    引言 Matrix内部的值修改严格来讲放在一个系列里不大合适,因为对于不同的问题,所用的算法和技巧可能完全不同,权且这样归类,以后需要时再拆分吧. 例题 1 Given a 2D board cont ...

随机推荐

  1. Chrome浏览器中使用 iframe 嵌入网页导致视频不能全屏的问题解决方法

    今天无意中测试了下在 iframe 中嵌入视频, 发现全屏按钮在 Chrome 浏览器中居然无效, 试了好几个视频网站的视频都不能全屏, 但在其他浏览器中似乎都很正常, 应该是 Chrome 60 新 ...

  2. Python数据类型-04.字典

    字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据 ------------ 完美的分割线 ------------- 1.字典引入 # 为何还要用字典?存放一个人的信 ...

  3. linux bash shell 判断目录是否为空的函数

    #!/bin/sh ##方法一 判断输出字符数统计为0 is_empty_dir(){ |wc -w` } ##方法二 判断输出string为空 #is_empty_dir(){ # ` ] #} t ...

  4. 《DSP using MATLAB》Problem 3.2

    1.用x1序列的DTFT来表示x2序列的DTFT 2.代码: %% ------------------------------------------------------------------ ...

  5. 注册dll文件

    1.打开"开始-运行-输入regsvr32 XXX.dll",回车即可 2.win7 64位旗舰版系统运行regsvr32.exe提示版本不兼容 在运行regsvr32.exe的时 ...

  6. 10件在PHP7中不要做的事情

    10件在PHP7中不要做的事情 1. 不要使用mysql_函数 这一天终于来了,从此你不仅仅“不应该”使用mysql_函数.PHP 7 已经把它们从核心中全部移除了,也就是说你需要迁移到好得多的mys ...

  7. 如何查看MySql的BLOB内容

    一款Mysql的工具: SQLyog. 强项在于可以把blob的内容直接显示出来. 我觉得其实做产品能够活挺厉害,因为你做的东西确实为客户提供价值:在云云产品之中,能够让客户发现你并使用,购买你的产品 ...

  8. ffmpeg C++程序编译时报__cxa_end_catch错误

    解决方法在编译sh中加上 -lsupc++ 即可. 2.STL模块函数找不到,链接失败stdc++/include/bits/stl_list.h:466: error: undefined refe ...

  9. LINQ to SQL 系列 如何使用LINQ to SQL插入、修改、删除数据 (转)

    http://www.cnblogs.com/yukaizhao/archive/2010/05/13/linq_to_sql_1.html LINQ和 LINQ to SQL 都已经不是一个新事物了 ...

  10. settimeout()在IE8下参数无效问题解决方法

    遇到这个问题,setTimeout(Scroll(),3000); 这种写法在IE8 下 不能够执行,提示参数无效, setTimeout(function(){Scroll()},3000);这种方 ...