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. 201521123061 《Java程序设计》第七周学习总结

    201521123061 <Java程序设计>第七周学习总结 1. 本周学习总结 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 贴上源 ...

  2. 201521123048 《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 for (in ...

  3. 201521123054《Java程序设计》第4周总结

    1. 本周学习总结 2. 书面作业 注释的应用 使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) 面向对象设计(大作业1,非常重要) **2.1 将在网上商城购 ...

  4. Python笔记2.1

    所有类型如下图: 一 基础数据类型 1)数字类型 复制代码 >>> 2/2+2*2 5.0 >>> (50-5*6)/4 5.0 >>> 8/5 ...

  5. 201521123038 《Java程序设计》 第九周学习总结

    201521123038 <Java程序设计> 第九周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 ...

  6. XML-为XML添加DTD-Schema方法

    以后都按照如下方式为XML添加dtd或者schema 1,一般从源码jar包里找dtd文件,拷贝到自己的本地目录中: 比如mybatis在如下目录中有dtd :~/ mybatis-3.2.2-sou ...

  7. 自定义Django的中间件

    分析Django的生命周期,我们知道所有的http请求都要经过Django的中间件. 假如现在有一个需求,所有到达服务端的url请求都在系统中记录一条日志,该怎么做呢? Django的中间件的简介 D ...

  8. Tensorflow之卷积神经网络(CNN)

    前馈神经网络的弊端 前一篇文章介绍过MNIST,是采用的前馈神经网络的结构,这种结构有一个很大的弊端,就是提供的样本必须面面俱到,否则就容易出现预测失败.如下图: 同样是在一个图片中找圆形,如果左边为 ...

  9. 《MATLAB从入门到放弃》打通 “矩阵” 障碍

    目录: »   矩阵的生成与大小  >   简单矩阵的生成  >  随机矩阵的生成  >   矩阵的大小 »  矩阵的索引与访问 »  矩阵的拼接与裁剪 >  矩阵的拼接 &g ...

  10. Python 编程基础之高阶函数篇(一)

      高阶函数:能接受函数作为参数的函数. 如: f=abs def   add(x,y,f): return f(x)+f(y) 如果我们用:add(-5,9,f)来调用该高阶函数,则返回结果为:14 ...