这是悦乐书的第172次更新,第174篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第31题(顺位题号是121)。假设有一个数组,其中第i个元素是第i天给定股票的价格。如果只被允许完成最多一笔交易(即买入并卖出一股股票),请设计算法以找到最大利润。请注意,在购买之前不能出售股票。例如:

输入:[7,1,5,3,6,4]

输出:5

说明:在第2天买入(价格= 1)并在第5天卖出(价格= 6),利润= 6-1 = 5。不是7-1 = 6,因为售价需要大于购买价格。

输入:[7,6,4,3,1]

输出:0

说明:在这种情况下,不进行任何交易,即最大利润= 0。

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 解题

特殊情况一:当传入的数组为null时,直接返回0。

特殊情况二:当传入的数组不为null,但是其内只有0个或者1个元素时,无法支持买入并卖出操作,直接返回0。

正常情况:要想股票获利,需要满足低买高卖的条件,否则不获利甚至亏损。先将第1天的价格单独拿出来,接着开始遍历数组(从第二个元素开始遍历),如果第二天的价格高于第一天的价格,此时的最大利润是第二天的价格减去第一天的价格与最大利润初始值之间的最大值,否则买入价格就应该换成第二天的价格,依次向后循环,直到比较完所有的价格。最后,如果最大利润大于0,此时最大利润就是其本身,否则最大利润为0。

public int maxProfit(int[] prices) {
if (prices == null || prices.length <= 1) {
return 0;
}
int maxProfit = Integer.MIN_VALUE;
int buyPrice = prices[0];
for (int i=1; i < prices.length; i++) {
if (prices[i] > buyPrice) {
maxProfit = Math.max(maxProfit, prices[i]-buyPrice);
} else {
buyPrice = prices[i];
}
}
if (maxProfit > 0) {
return maxProfit;
}
return 0;
}

对于上面的最大利润的初始值,也可以设为0,最后直接返回最大利润即可,就无需再加多一步判断了。

03 小结

此题的解法只用了一层循环,因此时间复杂度是O(n);此题只创建了两个变量,因此空间复杂度是O(1)

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode算法题-Best Time to Buy and Sell Stock的更多相关文章

  1. LeetCode算法题-Best Time to Buy and Sell Stock II

    这是悦乐书的第173次更新,第175篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第32题(顺位题号是122).假设有一个数组,其中第i个元素是第i天给定股票的价格.设计 ...

  2. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  3. 【刷题-LeetCode】188 Best Time to Buy and Sell Stock IV

    Best Time to Buy and Sell Stock IV Say you have an array for which the i-th element is the price of ...

  4. 【刷题-LeetCode】123 Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  5. 【刷题-LeetCode】122 Best Time to Buy and Sell Stock II

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  6. 【刷题-LeetCode】121 Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  7. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  8. LeetCode解题报告—— Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  9. 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Go基础系列:defer、panic和recover

    defer关键字 defer关键字可以让函数或语句延迟到函数语句块的最结尾时,即即将退出函数时执行,即便函数中途报错结束.即便已经panic().即便函数已经return了,也都会执行defer所推迟 ...

  2. DRF之项目搭建

    DRF,全称Django Restful Framework,是一个基于Django的Restful接口框架,是主要用来做API接口的,为前端提供数据的接口.在前面一片博客中,我们构建了一个vue的项 ...

  3. 关于 Uboot 中有趣的 0xdeadbeef 填充

    在 Uboot 的 Start.S 中存在以下源码: .globl _start _start: b start_code ldr pc, _undefined_instruction ldr pc, ...

  4. SpringBoot系列——EnableScheduling,对定时器的支持

    前言 定时器功能在项目里面往往会用到,比如定时发送邮件.定时释放数据库资源:这里记录一下springboot对定时器的支持的简单实例 cron表达式 开始之前要先介绍一下cron表达式,这里当一下百度 ...

  5. 多选穿梭框总结 (vue + element)

    博客地址:https://ainyi.com/23 示例 介绍 实现省市区三级多选联动,可任选一个省级.市级.区级,加入已选框,也可以在已选框中删除对应的区域. 选择对应仓库,自动勾选仓库对应的省,取 ...

  6. [转]VirtualBox centos7扩容

    本文转自:https://www.cnblogs.com/xd502djj/p/7367704.html 有时候扩容还真不如重新建立一个大硬盘的系统,但是如果你安装了好多东西的话,那还是来扩容一下吧. ...

  7. .NET页面导出Excel

    public static void CreateExcel(DataSet ds)        {            string filename = DateTime.Now.ToStri ...

  8. ABP框架 sql语句(转载)

    ABP.Core实现SQL语句仓储,支持EF.Core兼容的数据库  来源:https://blog.csdn.net/qq_28699537/article/details/80522680?tds ...

  9. Runnable和Callable接口辨析

    突然发现和启动一个线程有关的有三函数,run(), call(), start(),有点小乱,所以特别梳理一下 首先说一下start(),这个是最好说的,感觉start()和run()这俩名字是真的有 ...

  10. LintCode Majority Number II / III

    Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...