Stock(买卖股票)
来源:互联网 发布:血糖检测数据 编辑:程序博客网 时间:2024/04/29 19:30
1. 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(int[] prices) { if(prices.length <2) return 0; int max = 0; int minprice = prices[0]; for(int i=0; i<prices.length; i++){ if(prices[i]-minprice>max){ max = prices[i]-minprice; } if(minprice>prices[i]){ minprice = prices[i]; } } return max; }}
2. 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(int[] prices) { if(prices.length < 2){ return 0; } int sum = 0; for(int i=1; i<prices.length; i++){ if(prices[i-1]<prices[i]){ sum += prices[i]-prices[i-1]; } } return sum; }}
3. 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.Note:You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
第一种方法比较简单,就是当成两个两个stcokI 来求解,,即求[0,i],[i+1,n]两个最大值之和。时间复杂度优化后为O(n)。
class Solution { public int maxProfit(int[] prices) { if(prices.length < 2){ return 0; } int[] startprices = new int[prices.length]; int[] endprices = new int[prices.length]; for(int i=0; i<prices.length; i++){ startprices[i] = 0; endprices[i] = 0; } int minprices = prices[0]; for(int i=1; i<prices.length; i++){ startprices[i] = Math.max(startprices[i-1], prices[i]-minprices); minprices = Math.min(minprices, prices[i]); } int maxprices = prices[prices.length-1]; for(int i=prices.length-2; i>=0; i--){ endprices[i] = Math.max(endprices[i+1], maxprices-prices[i]); maxprices = Math.max(maxprices, prices[i]); } int max = 0; for(int i=0; i<prices.length; i++){ max = Math.max(max, startprices[i]+endprices[i]); } return max; }}
4. Best Time to Buy and Sell Stock IV
Say you have an array for which the *i*th element is the price of a given stock on day *i*.Design an algorithm to find the maximum profit. You may complete at most **k** transactions.**Note:**You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).**Credits:**Special thanks to [@Freezen](https://oj.leetcode.com/discuss/user/Freezen) for adding this problem and creating all test cases.
这题使用动态规划
我们定义local[i][j]为在到达第i天时最多可进行j次交易并且最后一次交易在最后一天卖出的最大利润,此为局部最优。然后我们定义global[i][j]为在到达第i天时最多可进行j次交易的最大利润,此为全局最优。它们的递推式为:
local[i][j] = max(global[i - 1][j - 1] + max(diff, 0), local[i - 1][j] + diff)
global[i][j] = max(local[i][j], global[i - 1][j])
其中局部最优值是比较前一天并少交易一次的全局最优加上大于0的差值,和前一天的局部最优加上差值中取较大值,而全局最优比较局部最优和前一天的全局最优。
如果k的值远大于prices的天数,比如k是好几百万,而prices的天数就为若干天的话,上面的DP解法就非常的没有效率,应该直接用[Best Time to Buy and Sell Stock II 的方法来求解。
class Solution { public int maxProfit(int k, int[] prices) { int n = prices.length; if(n<2){ return 0; } if(k>=n){ return maxstock(prices); } int[][] g = new int[n][k+1]; int[][] l = new int[n][k+1]; for(int i=1; i<n;i++){ int diff = prices[i]-prices[i-1]; for(int j=1;j<k+1;j++){ l[i][j] = Math.max(g[i-1][j-1]+Math.max(diff, 0), l[i-1][j]+diff); g[i][j] = Math.max(g[i-1][j], l[i][j]); } } return g[n-1][k]; } public int maxstock(int[] prices){ if(prices.length < 2){ return 0; } int sum = 0; for(int i=1; i<prices.length; i++){ if(prices[i-1]<prices[i]){ sum += prices[i]-prices[i-1]; } } return sum; }}
阅读全文
0 0
- Stock(买卖股票)
- 买卖股票
- Best Time to Buy and Sell Stock 买卖股票最好的时间
- Best Time to Buy and Sell Stock II 买卖股票最好的时间II
- Best Time to Buy and Sell Stock III 买卖股票的最好时间
- Best Time to Buy and Sell Stock 买卖股票的最佳时机
- Best Time to Buy and Sell Stock II 多次买卖股票的最佳时机
- Best Time to Buy and Sell Stock III 两次买卖股票的最佳时机 (重重重)
- LeetCode OJ 之 Best Time to Buy and Sell Stock (买卖股票的最佳时间)
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
- Best Time to Buy and Sell Stock II DP买卖股票
- Leetcode 122 Best Time to Buy and Sell Stock II 不限次数买卖股票最大收益
- Leetcode 123 Best Time to Buy and Sell Stock III 至多两次买卖股票最大收益
- Leetcode 121 Best Time to Buy and Sell Stock 单次买卖股票最大收益
- 【LintCode】 Best Time to Buy and Sell Stock 买卖股票的最佳时机
- 【LintCode】 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
- 【LintCode】 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III
- LintCode--best-time-to-buy-and-sell-stock(买卖股票的最佳时机)
- 简单的Makefile
- Maven项目中JSP页面无法得到静态资源的解决方法
- ffmpeg VS2013 编译调试
- 切换JDK版本时修改JAVA_HOME环境变量不生效
- 何时考虑阻抗匹配
- Stock(买卖股票)
- Redis集群方案理解及实践
- [shell] -- linux下日志文件的指定起始行和指定行数读取
- Android设计模式实战---责任链模式
- Spring Boot 动态数据源(多数据源自动切换)[转载]
- 欢迎使用CSDN-markdown编辑器
- HDFS小文件的合并优化
- Java中的Date和时区转换
- Ubuntu下PX4飞控开发环境搭建