Best Time to Buy and Sell Stock 最佳时间买入卖出股票(一次买入卖出) @LeetCode
来源:互联网 发布:nba2k16卡顿优化补丁 编辑:程序博客网 时间:2024/04/26 06:25
题目:
最佳时间买入卖出股票:你有一个数组保存了股票在第i天的价钱,现在你只能进行一次买入卖出,如何赚的最多
思路:
min记录最小买入价
maxProfit记录最大利润
遍历array,不断更新最小买入价,计算更新最大利润
package Level2;/** * 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. */public class S121 {public static void main(String[] args) {}public int maxProfit(int[] prices) {// 当没有任何prices信息的情况if(prices.length == 0){return 0;} int min = prices[0];// 记录最小买入价的index int maxProfit = 0;// 记录最大profit for(int i=1; i<prices.length; i++){ // 更新最小买入价 if(prices[i] < min){ min = prices[i]; } // 计算当前利润 int currentProfit = prices[i] - min; // 如果当前利润超过最大利润,更新最大利润 if(currentProfit > maxProfit){ maxProfit = currentProfit; } } return maxProfit; }}
public class Solution { public int maxProfit(int[] prices) { if(prices.length == 0){ return 0; } int minBuy = prices[0]; int maxProfit = 0; for(int i=0; i<prices.length; i++){ minBuy = Math.min(minBuy, prices[i]); maxProfit = Math.max(maxProfit, prices[i]-minBuy); } return maxProfit; }}
采用“局部最优和全局最优解法”:
思路是维护两个变量,一个是到目前为止最好的交易,另一个是在当前一天卖出的最佳交易(也就是局部最优)。递推式是local[i+1]=max(local[i]+prices[i+1]-price[i],0), global[i+1]=max(local[i+1],global[i])。这样一次扫描就可以得到结果,时间复杂度是O(n)。而空间只需要两个变量,即O(1)。
http://blog.csdn.net/linhuanmars/article/details/23162793
有两点值得注意:
1 在第i天买入,在i+1天卖出,再在i+1天买入,在i+2天卖出,等效于在i天买入在i+2天卖出:p[i+2]-p[i] = -p[i]+p[i+1]-p[i+1]+p[i+2]
2 如果计算出没两天的差值:diff1, diff2, diff3 则此题就转化为maximum subarray的问题
public class Solution { public int maxProfit(int[] prices) { int len = prices.length; if(len == 0){ return 0; } int[] local = new int[len+1]; int[] global = new int[len+1]; local[0] = 0; global[0] = 0; for(int i=1; i<len; i++) { int diff = prices[i] - prices[i-1]; local[i] = Math.max(local[i-1]+diff, Math.max(diff, 0)); global[i] = Math.max(global[i-1], local[i]); } return global[len-1]; }}
- Best Time to Buy and Sell Stock 最佳时间买入卖出股票(一次买入卖出) @LeetCode
- LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)
- 最佳时间买入卖出股票 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
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- LeetCode 121. Best Time to Buy and Sell Stock--股票买入后再卖出,求最大收益,最多交易一次
- 股票的最佳买入卖出时间
- LeetCode OJ 之 Best Time to Buy and Sell Stock (买卖股票的最佳时间)
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
- LeetCode-121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)
- 股票买入卖出手续费
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- LeetCode WIith JS || 121. Best Time to Buy and Sell Stock[求股票的最佳收益,只能交易一次]
- LeetCode OJ 之 Best Time to Buy and Sell Stock II (买卖股票的最佳时间 2)
- LeetCode OJ 之 Best Time to Buy and Sell Stock III (买卖股票的最佳时间 - 三)
- leetcode 122. Best Time to Buy and Sell Stock II 股票买卖最佳时间(二)
- Delphi 选择文件夹对话框 (有新建文件夹按钮)修正版
- 如何用C语言获取网卡的mac地址?(当然,你也可以用类似方法获取所有ip)
- 隐藏状态栏——在ios5-ios7
- delphi 选择目录对话框(SelectDirectory 函数)
- CODE 85: Rotate Image
- Best Time to Buy and Sell Stock 最佳时间买入卖出股票(一次买入卖出) @LeetCode
- ORA-15149: another ASM instance found running on the host
- 关于学习的偶感
- [iPhone开发]怎么区分一个api是公有的还是私有的
- IO_STACK_LOCATION
- 【博客】csdn文章书写标准
- ArcGIS for Android地图上实际距离与对应的屏幕像素值计算
- 计算机基础知识面试
- 迷茫中前行