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的更多相关文章

  1. Best Time to Buy and Sell Stock - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Best Time to Buy and Sell Stock - LeetCode 注意点 在卖出之前必须要先购入 解法 解法一:遍历一遍,随时记录当前 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

    [121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...

  9. [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 ...

随机推荐

  1. 关于在git添加远程地址的过程中遇到的问题

    问题产生的过程 我根据菜鸟教程的步骤,做了如下操作: 1.打开安装文件夹中的git-bash程序 2.设置username和email 3.添加远程地址 结果如下: 之后通过百度知道要先git ini ...

  2. 201521123008《Java程序设计》第二周实验总结

    本周学习总结 ① 数据类型,其中char是占用两个字节的内存空间,其他和以前学过的一样.除了十进制位,整型也可以用八进制或者十六进制表示.浮点型不精确. ②运算符,算术,赋值,逻辑,位运算. ③str ...

  3. VBScript中Msgbox函数的用法

    MsgBox(prompt[, buttons][, title][, helpfile, context]) [用途]:弹出对话框,并获取用户的操作结果. [参数说明]: propmt:对话框中展示 ...

  4. 201521123071 《JAVA程序设计》第十三周学习总结

    第13周作业-多线程 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 1.常用端口号:Web服务:80 FTP服务:21 Telnet服务:23 2.网络 ...

  5. 201521123112《Java程序设计》第10周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...

  6. Java课程设计 学生基本信息管理系统 团队博客

    学生基本信息管理系统团队博客 项目git地址 https://git.oschina.net/Java_goddess/kechengsheji 项目git提交记录截图 项目功能架构图与主要功能流程图 ...

  7. Struts2第十一篇【简单UI标签、数据回显】

    Struts2UI标签 Sturts2为了简化我们的开发,也为我们提供了UI标签-也就是显示页面的标签-.. 但是呢,Struts2是服务端的框架,因此使用页面的标签是需要在服务器端解析然后再被浏览器 ...

  8. java web项目修改favicon.ico图标的方式

    1.修改整个项目的tomcat图标 找到tomcat的根目录(tomcat-webapps-ROOT目录),然后将修改的favicon.ico图标覆盖掉本地的图标,然后再重启项目,刷新,清除浏览器缓存 ...

  9. JAVA数据流再传递

    有一个filter类,在请求进入的时候读取了URL信息,并且读取了requestBod中的参数信息,那么在请求到达实际的控制层时,入参信息是拿不到的,对这种情况就需要数据流做再传递处理. 处理原理:使 ...

  10. 渗透相关website

    开源安全测试方法论:http://www.isecom.org/research/osstmm.html 信息系统安全评估框架:www.oissg.org/issaf 开放式web应用程序安全项目(O ...