121. Best Time to Buy and Sell Stock I II III IV
来源:互联网 发布:黑蜘蛛软件 编辑:程序博客网 时间:2024/04/29 20:31
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 only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Example 1:
Input: [7, 1, 5, 3, 6, 4]Output: 5max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]Output: 0In this case, no transaction is done, i.e. max profit = 0.
class Solution {public: int maxProfit(vector<int>& prices) { if(prices.empty()) return 0; int result = 0; for(int i = 0; i< prices.size();){ int j = i+1; while(j < prices.size() && prices[j]>=prices[i] ){ result = max(result,prices[j] - prices[i]); j++; } i = j; } return result; }};//解法二:连续最大数组子段和class Solution {public: int maxProfit(vector<int>& prices) { if(prices.empty()) return 0; int result = 0; int curMax = 0; for(int i = 1; i< prices.size();i++){ curMax += prices[i] - prices[i-1]; curMax = max(0,curMax); result = max(result,curMax); } return result; }};
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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
//贪心算法class Solution {public: int maxProfit(vector<int>& prices) { int maxCur = 0; for(int i= 1;i < prices.size();i++){ int temp = prices[i]-prices[i-1]; if(temp > 0) maxCur += temp; } return maxCur; }};
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 algorithm to find the maximum profit. You may complete at most two transactions.
class Solution {public: int maxProfit(vector<int>& prices) { // f[k, ii] represents the max profit up until prices[ii] (Note: NOT ending with prices[ii]) using at most k transactions. // f[k, ii] = max(f[k, ii-1], prices[ii] - prices[jj] + f[k-1, jj]) { jj in range of [0, ii-1] } // = max(f[k, ii-1], prices[ii] + max(f[k-1, jj] - prices[jj])) // f[0, ii] = 0; 0 times transation makes 0 profit // f[k, 0] = 0; if there is only one price data point you can't make any money no matter how many times you can trade int KK = 2; //特例 int res = 0; if(prices.size() < 1) return res; vector<vector<int> > f(KK+1,vector<int>(prices.size(),0)); for(int k = 1; k <= KK; k++){ int tmpMax = f[k-1][0] - prices[0]; for(int ii = 1; ii < prices.size(); ii++){ f[k][ii] = max(f[k][ii-1], tmpMax + prices[ii]); tmpMax = max(tmpMax,f[k-1][ii] - prices[ii]); res = max(res,f[k][ii]); } } return res; }};
188. Best Time to Buy and Sell Stock IV
class Solution {public: int maxProfit(int KK, vector<int>& prices) { int res = 0; if(prices.size() < 1) return res; if (KK>prices.size()/2){ // simple case for (int i=1; i<prices.size(); ++i){ res += max(prices[i] - prices[i-1],0); } return res; } // f[k, ii] represents the max profit up until prices[ii] (Note: NOT ending with prices[ii]) using at most k transactions. // f[k, ii] = max(f[k, ii-1], prices[ii] - prices[jj] + f[k-1, jj]) { jj in range of [0, ii-1] } // = max(f[k, ii-1], prices[ii] + max(f[k-1, jj] - prices[jj])) // f[0, ii] = 0; 0 times transation makes 0 profit // f[k, 0] = 0; if there is only one price data point you can't make any money no matter how many times you can trade //int KK = 2; vector<vector<int> > f(KK+1,vector<int>(prices.size(),0)); for(int k = 1; k <= KK; k++){ int tmpMax = f[k-1][0] - prices[0]; for(int ii = 1; ii < prices.size(); ii++){ f[k][ii] = max(f[k][ii-1], tmpMax + prices[ii]); tmpMax = max(tmpMax,f[k-1][ii] - prices[ii]); res = max(res,f[k][ii]); } } return res; }};
- 121. Best Time to Buy and Sell Stock I II III IV
- LeetCode OJ Best Time to Buy and Sell Stock I II III IV
- LeetCode - Best Time to Buy and Sell Stock I && II && III && IV
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
- LeetCode-Best Time to Buy and Sell Stock I II III IV
- Best Time to Buy and Sell Stock I II III IV
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
- [LeetCODE] Best Time to Buy and Sell Stock I II III IV V
- leetcode笔记--Best Time to Buy and Sell Stock系列(I,II, III, IV)
- Best Time to Buy and Sell Stock I\II\III\IV
- 121.122.123.188. Best Time to Buy and Sell Stock I II III IV
- LeetCode - Best Time To Buy and Sell Stock Series (I, II, III, IV, with Cooldown)
- [LeetCode] Best Time to Buy and Sell Stock I II III IV
- 分类一:Best Time to Buy and Sell Stock I/ II/ III/ IV/ with Cooldown
- [LeetCode] Best Time to Buy and Sell Stock I II III IV 股票买卖
- Best Time to Buy and Sell Stock I II III IV (第四周 动态规划)
- 【LeetCode】 best-time-to-buy-and-sell-stock-i ii iii iv
- [Leetcode] #121#122#123#188 Best Time to Buy and Sell Stock I & II & III & IV
- iOS tableView的Cell的使用方式
- SVN问题:请求的名称有效,但是找不到请求的类型的数据
- 性能分析工具之-- Eclipse Memory Analyzer tool(MAT)(三)
- Java第三章上机实践-实验2-猜数字游戏
- 欢迎使用CSDN-markdown编辑器
- 121. Best Time to Buy and Sell Stock I II III IV
- CorelDRAW中的轮廓图工具该怎么进行使用
- 阿里无线技术架构演进
- 四种启动模式
- 第一个SpringMVC+Hibernate框架搭建之路
- ubuntu15.10 配置wildfly10 服务器
- Java第三章习题3-3(彩票中奖2)
- c++ STL 数据结构底层结构
- 正则表达式学习及应用