Best Time to Buy and Sell Stock I II III
来源:互联网 发布:今日值得买网站源码 编辑:程序博客网 时间:2024/05/01 20:50
Best Time to Buy and Sell Stock I II III
I
对于第一题,本质意义是求出数组中某两个元素(day[i],day[i])的最大差值。但是,要求j>i
int maxProfit(vector<int> &prices) { if(prices.size() ==0) return 0; int min=prices[0], profit=0; for(int i=0; i<prices.size(); i++) { if(prices[i] - min > profit) profit = prices[i] - min; if(prices[i] < min) min = prices[i]; } return profit; }
II
第二题是可以进行任意多次交易,那么对数组遍历,找到临时最小值和临时最大值。
int maxProfit(vector<int> &prices) { if (prices.size() == 0)return 0;int profit = 0, tmpMin=prices[0], tmpMax=prices[0];int n = prices.size();for (int i = 1; i < n; i++){ //寻找比tmpMIN小的元素if (prices[i] < tmpMin){ //比tmpMin小,则更新较小的tmpMin = prices[i];}else{ //比tmpMin大,则可以出手卖掉,并更新tmpMinprofit += (prices[i]-tmpMin);tmpMin = prices[i];}}return profit; }
III
第三题比较复杂,只允许买卖两次,有两种方法。
其一,由于两次操作之间没有交互,即第一次买卖完成之后才能进行第二次的买卖,因此可以用分治法将[0...n]中取任意一个i,分别去求[0...i]和[i+1...n-1]的最大利润,那么两者之和就是两次操作的最大利润,但是这个算法的时间复杂度是O^2,超时了。
//取[m, n]之间的最大利润int Profit1(vector<int>& prices, int m, int n){int size = prices.size();if(m >= size)return 0;int minPrice = prices[m], profit = 0;for (int i = m+1; i < n; i++){if (prices[i] - minPrice > profit){profit = prices[i] - minPrice;}if (prices[i] < minPrice){minPrice = prices[i];}}return profit;}int maxProfit(vector<int> &prices){if(prices.size() == 0)return 0;int n = prices.size();int max1 = 0;for (int i = 1; i < n; i++){int tmp1 = Profit1(prices, 0, i);int tmp2 = Profit1(prices, i+1, n);if(tmp1 + tmp2 > max1)max1 = tmp1 + tmp2;}return max1;}
方法二,DP,参考了一位网友的方法:http://blog.csdn.net/pickless/article/details/12034365
DP的关键是找出地推关系式
int maxProfit(vector<int> &prices){int n = prices.size();if(0==n || 1==n)return 0;int * l = new int[n];int * r = new int[n];memset(l, 0, sizeof(int) * n);memset(r, 0, sizeof(int) * n);int minPrice = prices[0];for (int i = 1; i < n; i++){l[i] = prices[i] - minPrice > l[i-1]? prices[i] - minPrice : l[i-1];minPrice = prices[i] < minPrice ? prices[i] : minPrice;}int maxPrice = prices[n-1];for (int i = n-2; i >=0; i--){r[i] = maxPrice - prices[i] > r[i+1] ? maxPrice - prices[i] : r[i+1];maxPrice = prices[i] > maxPrice ? prices[i] : maxPrice;}int profit = 0;for (int i = 0; i < n; i++){profit = l[i] + r[i] > profit ? l[i] + r[i] : profit;}delete l;delete r;return profit;}
0 0
- Best Time to Buy and Sell Stock I & II & III
- Best Time to Buy and Sell Stock I && II && III
- Best Time to Buy and Sell Stock I II III
- Best Time to Buy and Sell Stock I && II && III
- Best Time to Buy and Sell Stock I II III
- Buy and sell stock at best time(i,ii,iii)
- leetcode Best Time to Buy and Sell Stock(I~III)(*)
- [Leetcode] Best Time to Buy and Sell Stock I | III
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
- leetcode Best Time to Buy and Sell Stock I II III
- [leetcode]Best Time to Buy and Sell Stock I, II, III
- 【LeetCode】Best Time to Buy and Sell Stock I && II && III
- LeetCode Best Time to Buy and Sell Stock I,II,III
- LeetCode:Best Time to Buy and Sell Stock I & II & III
- leetcode Best Time to Buy and Sell Stock I & II & III
- 【leetcode】Best Time to Buy and Sell Stock I && II && III
- LeetCode: Best Time to Buy and Sell Stock I、II、III
- 中秋快乐&&股市飘红--- Best Time to Buy and Sell Stock III&& II&&I
- 黑马程序员——初识Java
- DSP 中断 调试小结
- Leetcode:Maximum Subarray
- 伪命题:微信开放JS-SDK,助力网页开发
- 教育已经插上互联网的翅膀,邢帅教育欢迎您
- Best Time to Buy and Sell Stock I II III
- ewgwg
- Spring Framework源码(十):SpringMVC之文件上传
- 重要开源协议的比较(BSD,Apache,GPL,LGPL,MIT) – 整理
- 杨辉三角形
- Servlet的基于annotation的服务端验证
- Cocos2d-x 3.2 -- 总结数学类Vec2/Size/Rect
- 限制UIAlertView上的TextField的输入字符长度
- apk应用版本更新,后台下载