最佳时间买入卖出股票 Best Time to Buy and Sell Stock LeetCode
LeetCode
我们有一个股票的数组,数组是每时间的钱,我们只能买入一次和卖出一次,求我们的最大收益。
我们知道了一个数组,那么我们可以在低价买入,然后高价卖出,但是需要知道我们的低价需要在高价之前。
我们可以两个变量,一个记录最低价,一个记录我们卖出得到最大钱。
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
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。
最佳时间买入卖出股票 Best Time to Buy and Sell Stock LeetCode的更多相关文章
- Best Time to Buy and Sell Stock - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Best Time to Buy and Sell Stock - LeetCode 注意点 在卖出之前必须要先购入 解法 解法一:遍历一遍,随时记录当前 ...
- Best Time to Buy and Sell Stock leetcode java
题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- Best Time to Buy and Sell Stock——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 121. Best Time to Buy and Sell Stock——Leetcode
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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 al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
[121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...
- [LeetCode] 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 ...
随机推荐
- 201521123008《Java程序设计》第七周实验总结
1.本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 public bool ...
- 201521123019 《Java程序设计》第2周学习总结
一. 本章学习总结 1.掌握了string类型的用法 2.对java数组有了初步了解 3.arrays用法有所掌握 二.书面作业 1.使用Eclipse关联jdk源代码,并查看String对象的源代码 ...
- 201521123040《Java程序设计》第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...
- Akka(25): Stream:对接外部系统-Integration
在现实应用中akka-stream往往需要集成其它的外部系统形成完整的应用.这些外部系统可能是akka系列系统或者其它类型的系统.所以,akka-stream必须提供一些函数和方法来实现与各种不同类型 ...
- 13.Linux键盘驱动 (详解)
版权声明:本文为博主原创文章,未经博主允许不得转载. 在上一节分析输入子系统内的intput_handler软件处理部分后,接下来我们开始写input_dev驱动 本节目标: 实现键盘驱动,让开发板的 ...
- Oracle函数之chr
chr()函数将ASCII码转换为字符:字符 –> ASCII码:ascii()函数将字符转换为ASCII码:ASCII码 –> 字符: 在oracle中chr()函数和ascii()是一 ...
- String类的常见面试题(3)
1.判断定义为String类型的s1和s2是否相等 String s1 = "abc"; //这个"abc"对象首先会进常量池 String s2 = &quo ...
- react基础(2)
react基础(1):介绍了如何创建项目,总结了JSX.组件.样式.事件.state.props.refs.map循环,另外还讲了mock数据和ajax 还是用 react基础1 里创建的项目继续写案 ...
- java GUI编程一
一.AWT介绍 所有的可以显示出来的图形元素都称为Component,Component代表了所有的可见的图形元素,Component里面有一种比较特殊的图形元素叫Container,Containe ...
- oracle 例外
一.例外分类oracle将例外分为预定义例外.非预定义例外和自定义例外三种.1).预定义例外用于处理常见的oracle错误.2).非预定义例外用于处理预定义例外不能处理的例外.3).自定义例外用于处理 ...