原题链接在这里: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. 20145201《Java程序设计》第1次实验报告

    实验内容 一.命令行下java程序开发 1.建立Code目录,输入mkdir 20145201命令建立实验目录,并使用dir命令查看目录建立情况. 运行结果如图 2.进入实验目录,输入mkdir ex ...

  2. 【转】React Native中ES5 ES6写法对照

    很多React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教程和例子都是 ...

  3. POJ 1442 优先队列

    题意:有一些ADD和GET操作.n次ADD操作,每次往序列中加入一个数,由ADD操作可知序列长度为1-n时序列的组成.GET操作输入一个序列长度,输出当前长度序列第i大的元素的值.i初始为0,每次GE ...

  4. JAVA基础补漏--ArrayList

    今天在写代码的时候,index定义的时候用了Integer,在list.remove(index)的时候,总是不成功,后来发现如果用Integer定义的时候,index不再是基础数据类型,被识别为re ...

  5. 智能DNS

    DNS查找下一个服务的地址   一.智能DNS APP通过域名访问DNS服务器,DNS根据域名对应一组IP中随机选择一个,发给APP.从这个意义说智能DNS,智能DNS相当一个七层的负载均衡. 二.H ...

  6. sed 案例

    sed:Stream Editor文本流编辑,sed是一个“非交互式的”面向字符流的编辑器.能同时处理多个文件多行的内容,可以不对原文件改动,把整个文件输入到屏幕,可以把只匹配到模式的内容输入到屏幕上 ...

  7. 用JAR的方式运行SpringBoot项目

    接 Spring Boot 快速入门(Eclipse) 步骤一:部署方式 Springboot 和 web 应用程序不一样,其本质上是一个 Java 应用程序,那么又如何部署呢? 通常来说,Sprin ...

  8. segment fault本质

    要谈segment fault,必须要谈指针. 指针的本质是什么?只不过是一种带*的数据类型,其特色有: 1.宽度 2.声明 3.赋值 4.++与-- 5.+与- 6.求差值 7.比较 当声明int ...

  9. 我的nodejs 快速入门

    每行以封号结尾(可有可无) 变量定义没有类型 都用var 或者直接const log打印:console.log(db); 内置属性这样写法:__filename.__dirname等 functio ...

  10. 多线程-模拟阻塞queue队列

    前阵子学习了多线程,现在进行总结一下,模拟队列. 分析问题: (1)首先需要一个容器存放元素,这里用linkedList队列. (2)每次像容器中添加或删除元素的时候需要计数,所以这里需要一个计数器, ...