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 ...
随机推荐
- shell编程学习笔记之正则表达式初识
1.对单字符的查找: 1.1单字符: ‘X’ $ grep ‘q’ passwd //查找单个字符 1.2 范围字符 [] [^] 1.3 任意字符 . $ grep '[1-9]' passwd / ...
- Autofac is designed to track and dispose of resources for you.
https://autofaccn.readthedocs.io/en/latest/best-practices/ Autofac is designed to track and dispose ...
- Gamma函数相关matlab代码
1.Gamma函数: Gamma函数matlab代码: x=:0.5:5syms t y=)*exp(-t),,inf) y=double(y) plot(x,y,) 图像如下: 2.lgΓ(x)函数 ...
- Java容器_01
1. HashTable 和 HashMap 区别? 2.
- Spring Boot的核心
1.1.1. 入口类和@SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序 ...
- spring的AOP动态代理--JDK代理和CGLIB代理区分以及注意事项
大家都知道AOP使用了代理模式,本文主要介绍两个代理模式怎么设置以及区别,对原文一些内容进行了引用后加入了自己的理解和更深入的阐述: 一.JDK代理和CGLIB代理的底层实现区别* JDK代理只能 ...
- pipenv安装库使用国内镜像
pipenv install django超级慢,卡住不动,最后不得不CTRL+C中止 这时有必要改成国内源了,打开Pipfile文件,发现内容是: [[source]] verify_ssl = t ...
- 7z压缩gopath的src的批处理
7zGoPath.bat @echo off pushd "%~dp0" :config for /f "delims=" %%t in ('powershel ...
- go语言的一个简单httpserver
httpserver.go package main import ( "net/http" "flag" "fmt" "log& ...
- Linux 下硬链接和软链接的说明
Linux 链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln 命令产生硬链接. 硬连接 硬连接指通过索引节点来进行连接.在 Li ...