leetcode之Best Time to Buy and Sell Stock
来源:互联网 发布:访客网络需要开启吗 编辑:程序博客网 时间:2024/05/29 19:08
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
题目描述:你有一个数组,第i个元素代表第i天股票的价格,设计一个算法获得最大的利润(买股票之前必须先卖掉手中的股票)
问题1:Best Time to Buy and Sell Stock I,你至多买一次股票
思路:一维动态规划,记maxprofile[i]表示到i天为止最大的收益,minprices表示第i天为止最小的买入价格,那么递推关系式如下:
maxprofile[i] = max(maxprofile[i-1], prices[i] - minprices)
也就是第i天的最大收益为:max(第i-1天的最大收益,以最小价格买入,第i天的价格卖出的收益)
O(n)时间复杂度,O(1)空间复杂度
代码如下:
class Solution {public: int maxProfit(vector<int>& prices) { int len = prices.size(); if(len < 2) return 0; int minPrices = prices[0];//到当前位置i为止的最小价格 int maxProfile = 0; for(int i = 1; i < len; ++i){ minPrices = min(prices[i], minPrices); maxProfile = max(maxProfile, prices[i] - minPrices); } return maxProfile; }};
问题2:Best Time to Buy and Sell Stock II,不限制你的买入次数,求最大收益
思路:贪心算法,从前向后遍历数组,只要当天的价格高于前一天的价格,就算入收益。
O(n)时间复杂度,O(1)空间复杂度,代码如下:
class Solution {public: int maxProfit(vector<int>& prices) { int len = prices.size(); if(len < 2) return 0; int maxProfile = 0; for(int i = 1; i < len; ++i){ int diff = prices[i] - prices[i-1]; if(diff > 0) maxProfile += diff; } return maxProfile; }};
问题3:Best Time to Buy and Sell Stock III 最多购买2次股票,求最大收益
思路:动态规划,将数组以第i天划分为两半,l[i], r[i]。其中l[i]表示第i天之前的最大收益,r[i]表示第i天之后的最大收益,那么max(l[i]+r[i])(0<=i<=len-1),即为最大收益,而l[i]与r[i]的求法同Best Time to Buy and Sell Stock I,O(n)时间复杂度,O(n)空间复杂度。
代码如下:
class Solution {public: int maxProfit(vector<int>& prices) { int len = prices.size(); if(len < 2) return 0; int *l = new int[len]; int *r = new int[len]; l[0] = 0; int m = prices[0];//最低买入价格 for(int i = 1; i < len; ++i){ l[i] = max(l[i-1], prices[i]-m); m = min(prices[i], m); } r[len-1] = 0; m = prices[len-1];//最高卖出价格 for(int i = len - 2; i >= 0; --i){ r[i] = max(r[i+1], m - prices[i]); m = max(prices[i], m); } m = 0; for(int i = 0; i < len; ++i) m = max(m, r[i]+l[i]); return m; }};
问题4:Best Time to Buy and Sell Stock IV,最多进行k此交易,求最大收益
思路:采用动态规划法求解,定义变量g[i][j]表示到第i天为止最多进行j次交易的最大收益,即全局最优,l[i][j]表示到第i天为止进行最多j次交易并且最后一次交易在当天(第i天)进行的最大收益,即局部最优,那么有如下递推关系式:
g[i][j] = max(g[i-1][j], l[i][j]),第i天进行至多j次的最大收益要么为第i-1天进行至多j次的最大收益,要么为最后一次交易在第i天进行的至多j次交易的最大收益。
l[i][j] = max(g[i-1][j-1] + max(0, prices[i] - prices[i-1]), l[i-1][j] + prices[i] - prices[i-1])
l[i][j]由两个变量构成,全局进行到i-1天至多j-1次交易(g[i-1][j-1]),在第i天进行一次交易(如果赚钱);局部进行到第i-1天至多进行j次交易,加上第i天的交易,不管是否赚钱(否则不满足最后一次交易在当天进行的前提)。该算法的时间复杂度为O(nk),空间复杂度为O(k)(调优后的结果),如果k远大于数组大小时,算法的效率比较底下,因此可以采用Best Time to Buy and Sell Stock II不限次数的解法。
代码如下:
class Solution {public: int maxProfits(vector<int>& prices) {//不限次数 int maxProfiles = 0; int len = prices.size(); for(int i = 1; i < len; ++i) maxProfiles = max(maxProfiles, maxProfiles + prices[i]-prices[i-1]); return maxProfiles; } int maxProfit(int k, vector<int>& prices) { int len = prices.size(); if(len < 2) return 0; if(k > len) return maxProfits(prices); vector<int> l(k + 1, 0); vector<int> g(k + 1, 0); for(int i = 1; i < len; ++i){ int diff = prices[i] - prices[i-1]; for(int j = k; j >= 1; --j){ l[j] = max(g[j-1]+max(diff, 0), l[j]+diff); g[j] = max(g[j], l[j]); } } return g[k]; }};
- LeetCode之Best Time to Buy and Sell Stock
- LeetCode之Best Time to Buy and Sell Stock II
- LeetCode之Best Time to Buy and Sell Stock III
- LeetCode 之 Best Time to Buy and Sell Stock
- LeetCode 之 Best Time to Buy and Sell Stock II
- LeetCode之Best Time to Buy and Sell Stock
- LeetCode之Best Time to Buy and Sell Stock II
- leetcode之Best Time to Buy and Sell Stock
- leetcode之Best Time to Buy and Sell Stock III
- leetcode之Best Time to Buy and Sell Stock
- leetcode之Best Time to Buy and Sell Stock II
- Leetcode之Best Time to Buy and Sell Stock
- leetcode 之 Best Time to Buy and Sell Stock
- LeetCode之Best Time to Buy and Sell Stock III
- LeetCode之Best Time to Buy and Sell Stock
- LeetCode之Best Time to Buy and Sell Stock II
- leetcode之Best Time to Buy and Sell Stock
- LeetCode 之 Best Time to Buy and Sell Stock II
- 如何提高个人知识的价值
- ——黑马程序员——OC中self和super关键字的使用
- Node.js学习笔记之三:事件_EventEmitter
- leetcode-Valid Anagram
- [温故而知新] 《深度探索c++对象模型》——构造函数
- leetcode之Best Time to Buy and Sell Stock
- CSDN中怎样让图片居中和首行缩进
- 偏执却管用的10条Java编程技巧
- vim配置
- Linux下无效唤醒的应用
- SSD6 Exercise1 深入分析
- iOS 画板的简单实现
- 一些关于git的基本使用方法
- multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded