LeetCode Minimum Time Difference
原题链接在这里: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:
- The number of time points in the given list is at least 2 and won't exceed 20000.
- 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的更多相关文章
- [LeetCode] Minimum Time Difference 最短时间差
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- LeetCode Minimum Absolute Difference in BST
原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- [Leetcode Week10]Minimum Time Difference
Minimum Time Difference 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-time-difference/desc ...
- 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...
- 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】539. Minimum Time Difference 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...
- 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 ...
随机推荐
- Oracle11g:分区表数据操作出现ORA-14400异常处理
Oracle11g:分区表数据操作出现ORA-14400异常处理 问题: 当对已分区的表数据进行操作(例如新增,修改),出现异常提示: ORA: 插入的分区关键字未映射到任何分区 分析: 意思说的是插 ...
- socket IPC(本地套接字 domain)
1.简介 socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是UNIX Domain Socket.虽然网络socket也可用于同一台主机的进程间通讯(通 ...
- MyBatis正在爬的坑
换了份工作,开始接触Mybatis,开一篇文章记录一下自己遇到的坑 2018-06-20 今天遇到了一个问题,编好的sql语句在数据库可以执行但是写到程序里边就GG,什么问题呢?一直纠结在程序哪里写错 ...
- java分页的实现(后台工具类和前台jsp页面)
1.首先,新建一个类Page.java public class Page implements Serializable { private static final long serialVers ...
- 常见linux系统中RPM包的通用命名规则
本文重点说一下在常见的linux系统中,RPM包通用的命名规则. RPM包的一般格式为:name-version-arch.rpmname-version-arch.src.rpm 例:httpd-2 ...
- java web 实体类生成
工具下载地址:https://download.csdn.net/download/g342105676/10813246
- json-lib反序列化抽象属性及对象
使用json默认反序列化接口反序列化对象时,对象的类型必须的确定的,比如不能是抽象类型,否则会报无法实例化对象的异常 如有下列类定义: public abstract class AbstracObj ...
- vue2 遇到的问题汇集ing
1 .子路由 { path: '/order-list', //订单列表 name: "order-list", component(resolve) { require.ensu ...
- linux的文件
今日感慨:linux根目录下的文件夹含义 bin的知识,二进制文件,其用途依系统或应用而定 . 也就是说,一般来讲是机器代码,汇编语言编译后的结果,(DOS下汇编语言编译后与.com文件相类似),用d ...
- Django进阶Template篇001 - 常用模板标签及过滤器
一.模板的组成 HTML代码+逻辑控制代码 二.逻辑控制代码的组成 1.变量(使用双大括号来引用变量) {{ var_name }} 2.标签(tag)的使用(使用大括号和百分号的组成来表示使用tag ...