原题链接在这里: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. MySQL-5.7 游标及DECLARE

    1.cursor游标 用来声明一个数据集 游标的声明必须在变量和条件声明之后,在handler声明之前 游标特性: 不灵敏:服务器可以或不复制其结果 只读:不可更新 不可滚动的:只能在一个方向上遍历, ...

  2. Sybase:SAP IQ学习笔记

    Sybase:SAP IQ学习笔记 -- 启动IQ管理 >> start_iq -n utility_db -n utility_db >> dbisql -c "u ...

  3. Maven:Eclipse上Maven的配置

    Eclipse上Maven的配置: 步骤: ①Maven下载地址: http://maven.apache.org/download.cgi# ②解压apache-maven-3.5.0-bin.zi ...

  4. Difference between RouteTable.Routes and HttpConfiguration.Routes?

    https://stackoverflow.com/questions/12533782/difference-between-routetable-routes-and-httpconfigurat ...

  5. hackerrankWeek of Code 32

    hackerrankWeek of Code 32 A.Duplication B.Fight the Monsters! C.Circular Walk D.Geometric Trick E.Ba ...

  6. ImageView显示图像控件

    ImageView显示图像控件 一.简介 1. 2. ImageView,图像视图,直接继承自View类,它的主要功能是用于显示图片,实际上它不仅仅可以用来显示图片,任何Drawable对象都可以使用 ...

  7. spring3: AOP 之代理机制

    Spring AOP通过代理模式实现,目前支持两种代理:JDK动态代理.CGLIB代理来创建AOP代理,Spring建议优先使用JDK动态代理. JDK动态代理:使用java.lang.reflect ...

  8. mysql:字符分割,将字符分割成数组

    1.分割函数:SUBSTRING_INDEX('浙江温州-中国电信','-','1') 2.用例(筛选'-'前至少4个汉字的数据) a.数据分布 b.筛选sql   select t.mobile_n ...

  9. JsonTools 工具类

    import net.sf.json.JSONObject; public class JsonTools { public static JSONObject getJSONObject(Strin ...

  10. 关键C函数备录

    一.搜索指定路径下的文件 (1) intptr_t _findfirst(const char *, struct _finddata_t *);//可以使用通配符*或? (2) int _findn ...