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 ...
随机推荐
- python项目 配置文件 的设置
一项目目录: 二:默认配置settings的配置:config 文件 __inint__.py文件: #!/usr/bin/env python # -*- coding: utf-8 -*- # C ...
- 周立功CAN-II引脚图
注意:如果需要两个CAN通道通信,直接用杜邦线将L连L,H连H,地连地就行(3.6引脚已做短接处理),不需要反接
- [算法] 将单链表的每K个节点之间逆序
题目 给定一个单链表的头结点,实现一个调整单链表的函数,使得每K个节点之间逆序,如果最后不够K个节点一组,则不调整最后几个节点. 解答 使用栈结构 import java.util.Stack; pu ...
- fabric安装使用
可以使用pip安装fabric,注意使用pip 安装fabric时,一定要指定版本,不要安装2.0版本的,无论怎样都会提示没有api这样模块,所以指定安装 pip install fabric==1. ...
- jmeter-执行多个sql查询语句
1.添加jdbc connection(注意标红部分) 2.添加jdbc request 3.查看结果树
- 缓存技术内部交流_01_Ehcache3简介
参考资料: http://www.ehcache.org/documentation/3.2/getting-started.html http://www.ehcache.org/documenta ...
- css预处理器sass学习
SASS 叫做css预处理器,他的基本思想是用一门专门的编程语言来进行页面样式的设计,然后在编译成正常的css文件. Sass的用法 安装 sass是用ruby语言写的,所以我们在安装sass之前要先 ...
- include,include_once,require,require_once的区别
1.include() include(/path/to/filename) include()语句将在其被调用的位置处包含一个文件.包含一个文件与在该语句所在位置复制制定文件的数据具有相同内容的效果 ...
- 【Demo】CSS3 2D转换
2D转换transform 2D变换方法: translate() 根据左(X轴)和顶部(Y轴)位置给定的参数,从当前元素位置移动. div { transform: translate(50px,1 ...
- Gogs/Gitea 在 docker 中部署
注:Gitea是Gogs的一个分支版本,由多个维护者开发,支持搜索.lfs等,但是BUG较多,稳定性似乎没有Gogs好. #### 安装 ####// Gogs$ docker pull gogs/g ...