买卖股票的最佳时机I、II、III、IV
2017-09-04 16:12
603 查看
买卖股票的最佳时机I
假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。思路:这个比较简单,只要遍历一次就好,同时更新最小的值。可以得到最大的利润。
代码如下:
class Solution { public: /* * @param prices: Given an integer array * @return: Maximum profit */ int maxProfit(vector<int> &prices) { // write your code here if(prices.size()<2)return 0;//防止越界 int maxprofit = 0;//最大利润 int minlow = prices[0];//最低价格 for(int i = 1;i != prices.size();++i){ minlow = min(minlow,prices[i]); maxprofit = max(maxprofit,prices[i] - minlow); } return maxprofit; } };
买卖股票的最佳时机II
假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格。设计一个算法来找到最大的利润。你可以完成尽可能多的交易(多次买卖股票)。然而,你不能同时参与多个交易(你必须在再次购买前出售股票)。思路:这个问题,显然只要能有收益的时候就脱出卖,这样就可以获得最大的利润。
代码如下:
class Solution { public: /* * @param prices: Given an integer array * @return: Maximum profit */ int maxProfit(vector<int> &prices) { // write your code here //这里应该是有利润就要出手 if(prices.size()<2)return 0; int low = prices[0]; int profit = 0; for(int i=1;i!=prices.size();++i){ if(prices[i]>low){ profit = profit+prices[i]-low; } low = prices[i]; } return profit; } };
买卖股票的最佳时机III&IV
假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格。设计一个算法来找到最大的利润。你最多可以完成 k 笔交易。注意事项:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票)样例
给定价格 = [4,4,6,1,1,4,2,5], 且 k = 2, 返回 6.
思路:典型的动态规划问题。
代码如下:
public class Solution { /* * @param K: An integer * @param prices: An integer array * @return: Maximum profit */ public int maxProfit(int K, int[] prices) { int len = prices.length; if (K >= len / 2) return quickSolve(prices); int[][] t = new int[K + 1][len]; for (int i = 1; i <= K; i++) { int tmpMax = -prices[0]; for (int j = 1; j < len; j++) { t[i][j] = Math.max(t[i][j - 1], prices[j] + tmpMax); tmpMax = Math.max(tmpMax, t[i - 1][j - 1] - prices[j]); } } return t[K][len - 1]; } private int quickSolve(int[] prices) { int len = prices.length, profit = 0; for (int i = 1; i < len; i++) // as long as there is a price gap, we gain a profit. if (prices[i] > prices[i - 1]) profit += prices[i] - prices[i - 1]; return profit; } }
另一种简单的解法
在Discuss中看到一种很棒的解法,代码只有10行左右,但是不是很好理解。第二种解法的核心是假设手上最开始只有0元钱,那么如果买入股票的价格为price,手上的钱需要减去这个price,如果卖出股票的价格为price,手上的钱需要加上这个price。
它定义了4个状态:
Buy1[i]表示前i天做第一笔交易买入股票后剩下的最多的钱;
Sell1[i]表示前i天做第一笔交易卖出股票后剩下的最多的钱;
Buy2[i]表示前i天做第二笔交易买入股票后剩下的最多的钱;
Sell2[i]表示前i天做第二笔交易卖出股票后剩下的最多的钱;
那么
Sell2[i]=max{Sell2[i-1],Buy2[i-1]+prices[i]}
Buy2[i]=max{Buy2[i-1],Sell[i-1]-prices[i]}
Sell1[i]=max{Sell[i-1],Buy1[i-1]+prices[i]}
Buy1[i]=max{Buy[i-1],-prices[i]}
可以发现上面四个状态都是只与前一个状态有关,所以可以不使用数组而是使用变量来存储即可。
class Solution { public: int maxProfit(vector<int>& prices) { int buy1=numeric_limits<int>::min(); int buy2=numeric_limits<int>::min(); int sell1=0; int sell2=0; for(int i=0;i<prices.size();i++) { sell2=max(sell2,buy2+prices[i]); buy2=max(buy2,sell1-prices[i]); sell1=max(sell1,buy1+prices[i]); buy1=max(buy1,-prices[i]); } return sell2; } };
相关文章推荐
- LintCode 买卖股票的最佳时机I II III IV
- 买卖股票的最佳时机I II III IV
- **[Lintcode]Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV Leetcode
- 题目:买卖股票的最佳时机 II
- lintcode:买卖股票的最佳时机 IV
- Lintcode 买卖股票的最佳时机 II
- 【初级算法】2.买卖股票的最佳时机 II
- Leetcode 买卖股票的最佳时机 II
- 题目:买卖股票的最佳时机 III
- lintcode--买卖股票的最佳时机II
- 【LintCode】 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III
- LeetCode 122. 买卖股票的最佳时机 II
- 买卖股票的最佳时机 II
- LeetCode - 122. 买卖股票的最佳时机 II
- lintcode:买卖股票的最佳时机 II
- [Lintcode] #150 买卖股票的最佳时机 II
- LintCode 买卖股票的最佳时机 III
- [Leetcode]2018.3.22买卖股票的最佳时机 II
- LeetCode:买卖股票的最佳时机 II
- 【LintCode】 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II