最佳时间买入卖出股票 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 ...
随机推荐
- 201521123037 《Java程序设计》第7周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 查看ArrayLi ...
- 201521123115 《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2.书面作业 1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过?哪 ...
- 201521123023《java程序设计》第四周学习总结
1. 本周学习总结 思维导图 常规: (1)抽象类:不能被直接实例化.只能作为其它类的父类,这一点与final类正好相反.用关键词abstract声明. (2)继承:只能有一个父类,即单继承,子类继承 ...
- Win8打开chm右侧空白解决方法
Win8下打开CHM文件,左侧有目录,但是右侧空白.而且打开的时候,还弹出很多IE窗口. 感觉应该不是文件本身的问题.下面是我的解决方法,其他系统也可以试一试. 最初打开文件如下 首先:1,右键关联c ...
- Python笔记2.1
所有类型如下图: 一 基础数据类型 1)数字类型 复制代码 >>> 2/2+2*2 5.0 >>> (50-5*6)/4 5.0 >>> 8/5 ...
- 201521123063 《Java程序设计》 第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 读操作 (1)读取控制台输入: BufferedReader br = new BufferedReader( ...
- JAVA课程设计个人博客 学生成绩管理 201521123001 张陈东芳
1. 团队课程设计博客链接 http://www.cnblogs.com/kawajiang/p/7062407.html 2.个人负责模块或任务说明 我主要负责实现学生信息的添加功能.学生成绩的录入 ...
- HttpServletRequest获取URL、URI
从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 import j ...
- java web项目修改favicon.ico图标的方式
1.修改整个项目的tomcat图标 找到tomcat的根目录(tomcat-webapps-ROOT目录),然后将修改的favicon.ico图标覆盖掉本地的图标,然后再重启项目,刷新,清除浏览器缓存 ...
- SQL映射文件实现多种方式查询
1.单条件查询在test中代码如下 2.多条件查询时需要把查询条件编辑为对象或者是集合传入,例如 通过对象进行查询 或者是通过集合进行查询列如Map集合 还有就是通过@Param注解实现多参数的入参, ...