最佳时间买入卖出股票 Best Time to Buy and Sell Stock LeetCode
2016-10-01 08:56
519 查看
LeetCode
我们有一个股票的数组,数组是每时间的钱,我们只能买入一次和卖出一次,求我们的最大收益。
我们知道了一个数组,那么我们可以在低价买入,然后高价卖出,但是需要知道我们的低价需要在高价之前。
我们可以两个变量,一个记录最低价,一个记录我们卖出得到最大钱。
我们不断计算当前最小和当前价格的卖出得到的钱,如果大于我们的最大卖出钱就记下,这样就得到我们的最大卖出钱。
我们来个测试,UWP的测试其实和我发的单元测试是一样。
新建测试,然后写一个类
代码:https://github.com/lindexi/Algorithm/blob/master/Algorithm/Model/BestTimetoBuyandSellStock.cs
![](https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png)
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。
我们有一个股票的数组,数组是每时间的钱,我们只能买入一次和卖出一次,求我们的最大收益。
我们知道了一个数组,那么我们可以在低价买入,然后高价卖出,但是需要知道我们的低价需要在高价之前。
我们可以两个变量,一个记录最低价,一个记录我们卖出得到最大钱。
public static int MaxProfit(int[] price) { if (price.Length <= 1) { return 0; } int minPrice = price[0];//最小的钱 int maxProfit = 0;//收益 for (int i = 1; i < price.Length; i++) { minPrice = Math.Min(minPrice, price[i]); int currentProfit = price[i] - minPrice; maxProfit = Math.Max(maxProfit, currentProfit); } return maxProfit; }
我们不断计算当前最小和当前价格的卖出得到的钱,如果大于我们的最大卖出钱就记下,这样就得到我们的最大卖出钱。
我们来个测试,UWP的测试其实和我发的单元测试是一样。
新建测试,然后写一个类
[TestClass] public class BestTimetoBuyandSellStock { [TestMethod] public void MaxProfit() { int[] price = new[] { 2,3,2,5 }; var temp = Algorithm.Model.BestTimetoBuyandSellStock.MaxProfit(price); Assert.AreEqual(temp, 3); price = new[] { 5, 15, 1, 3, 6, 5, 3, 2, 5, 6, 7, 2, 2, 3 }; temp = Algorithm.Model.BestTimetoBuyandSellStock.MaxProfit(price); Assert.AreEqual(temp, 10); } }
代码:https://github.com/lindexi/Algorithm/blob/master/Algorithm/Model/BestTimetoBuyandSellStock.cs
![](https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png)
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。
相关文章推荐
- Best Time to Buy and Sell Stock II 最佳时间买入卖出股票(多次买卖)@LeetCode
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- Best Time to Buy and Sell Stock III 最佳时间买入卖出股票(最多两次买卖)@LeetCode
- LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- [LeetCode 123] - 买入与卖出股票的最佳时机III(Best Time to Buy and Sell Stock III)
- Best Time to Buy and Sell Stock 最佳时间买入卖出股票(一次买入卖出) @LeetCode
- [LeetCode 121] - 买入与卖出股票的最佳时机(Best Time to Buy and Sell Stock)
- [LeetCode 122] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
- LeetCode 121. Best Time to Buy and Sell Stock--股票买入后再卖出,求最大收益,最多交易一次
- [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间
- leetcode 121.Best Time to Buy and Sell Stock 股票买卖最佳时间
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
- LeetCode OJ:Best Time to Buy and Sell Stock II(股票买入卖出最佳实际II)