原题链接在这里:https://leetcode.com/problems/minimum-time-difference/description/

题目:

Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list.

Example 1:

Input: ["23:59","00:00"]
Output: 1

Note:

  1. The number of time points in the given list is at least 2 and won't exceed 20000.
  2. The input time is legal and ranges from 00:00 to 23:59.

题解:

利用bucket sort. 把时间转化成的位置标记成true. 若是之前已经标记了说明有duplicate, 可以直接return 0.

从头到尾iterate buckets维护最小diff. 再和首尾相差的diff比较取出最小值.

Time Complexity: O(timePoints.size() + 24*60).

Space: O(24*60).

AC Java:

 class Solution {
public int findMinDifference(List<String> timePoints) {
boolean [] mark = new boolean[24*60];
for(String s : timePoints){
String [] parts = s.split(":");
int time = Integer.valueOf(parts[0]) * 60 + Integer.valueOf(parts[1]); if(mark[time]){
return 0;
} mark[time] = true;
} int res = 24*60;
int pre = -1;
int first = Integer.MAX_VALUE;
int last = Integer.MIN_VALUE;
for(int i = 0; i<24*60; i++){
if(mark[i]){
if(pre != -1){
res = Math.min(res, i-pre);
} pre = i;
first = Math.min(first, i);
last = Math.max(last, i);
}
} res = Math.min(res, first+24*60-last);
return res;
}
}

LeetCode Minimum Time Difference的更多相关文章

  1. [LeetCode] Minimum Time Difference 最短时间差

    Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...

  2. [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  3. LeetCode Minimum Absolute Difference in BST

    原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...

  4. LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  5. [Leetcode Week10]Minimum Time Difference

    Minimum Time Difference 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-time-difference/desc ...

  6. 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)

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

  7. 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)

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

  8. 【LeetCode】539. Minimum Time Difference 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...

  9. 51. leetcode 530. Minimum Absolute Difference in BST

    530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...

随机推荐

  1. EasyUI中datagrid双击事件

    EasyUI中datagrid双击事件 在jsp文件底部增加代码: <script type="text/javascript"> //数据表双击事件 $('#tabl ...

  2. Python3.x:pyodbc连接Sybase数据库操作(Windows系统下DNS模式)

    Python3.x:pyodbc连接Sybase数据库操作(Windows系统下DNS模式) 一.安装模块pyodbc pip install pyodbc 二.配置odbc数据源 (1).windo ...

  3. 基于HTML5和SVG的手机菜单动画

    在线演示 本地下载

  4. ubuntu循环登录问题的解决

    之前试过几个方法都不行,包括改/etc/profile,startx,删Xauthority文件等,这些都是因为,形象地来说是药不对症,ubuntu循环登录是有很多个问题造成的,前面的这些例子只是针对 ...

  5. 关于C语言中结构体大小计算

    结构体大小的计算,.网上说法一大堆还都不一样分什么对齐不对齐,偏移量什么的.. 在此稍微举例简单总结下: 对齐原则:每一成员的结束偏移量需对齐为后一成员类型的倍数  补齐原则:最终大小补齐为成员中最大 ...

  6. ICE 中后台开发

    1.https://alibaba.github.io/ice/#/block 2.https://www.zhihu.com/question/266529857/answer/309604282 ...

  7. mooseFS学习篇

    官方网站:http://www.moosefs.org/ About MooseFS MooseFS is a fault tolerant, network distributed file sys ...

  8. webpack打包图片资源找不到问题

    当我们进行前端打包时,需改成如下配置: 往常这样打包是没有问题的,可是今天进行项目打包的时候缺报图片找不到的错误,如图所示: 头部组件的图片资源找不到错误,后台发现因为头部组件的背景图片size过大, ...

  9. ARM的异常处理方式

    1.什么是异常? 正常工作之外的流程都叫异常 异常会打断正在执行的工作,并且一般我们希望异常处理完成后继续回来执行原来的工作 中断是异常的一种 2.异常向量表 所有的CPU都有异常向量表,这是CPU设 ...

  10. git远程分支回退

    [本地代码回退] git reset --hard commit-id :回滚到commit-id,讲commit-id之后提交的commit都去除 git reset --hard HEAD~3:将 ...