539. Minimum Time Difference


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.


Brute force 暴力解法:O(n2), 任意两个时间点求差。效率差,忽略。

排序:O(nlgn)

 1 class Solution {
 2 public:
 3     int findMinDifference(vector<string>& timePoints) {
 4         const int SIZE=timePoints.size();
 5         if(SIZE>1440){
 6             return 0;
 7         }
 8
 9         //sorting
10         sort(timePoints.begin(),timePoints.end());
11         int result=INT_MAX;
12         int diff;
13         for(int i=0;i<SIZE-1;i++){
14             diff=getTimeMins(timePoints[i+1])-getTimeMins(timePoints[i]);
15             result = min(result,diff);
16             if(result==0){
17                 return result;
18             }
19         }
20
21         return min(result, getTimeMins(timePoints[0])+1440-getTimeMins(timePoints[SIZE-1]));
22     }
23 private:
24     int getTimeMins(string time24Hours){ //time24Hours in the format of "##:##"
25         return stoi(time24Hours.substr(0,2))*60+stoi(time24Hours.substr(3));
26     }
27 };

优化解法:比O(nlng)排序更好的是linear算法,所有的时间点总共有60*24=1440种可能,可以用数组来存该时间点是否出现过。

 1 class Solution {
 2 public:
 3     int findMinDifference(vector<string>& timePoints) {
 4         const int SIZE=1440;
 5         //there are totally 1440 different time points, if more than this number, there are at least two timepoints are the same
 6         if(timePoints.size()>SIZE){
 7             return 0;
 8         }
 9
10         vector<bool> mask(SIZE, false);
11         for(auto timePoint: timePoints){
12             int time=getTimeMins(timePoint);
13             if(mask[time]){
14                 return 0;
15             }
16             mask[time]=true;
17         }
18
19         int prev = -1;
20         int first = -1; //the first timepoint
21         int last = -1;  //the last timepoint
22         int result = INT_MAX;
23         for(int i=0;i<SIZE;i++){
24             if(mask[i]){
25                 if(prev!=-1){
26                     result = min(result,i-prev);
27                 }
28                 prev = i;
29                 if(first==-1){
30                     first=i;
31                 }
32                 last = i;
33             }
34         }
35         return min(result,first+1440-last);
36     }
37 private:
38     //convert timepoint in string to a number of minutes
39     int getTimeMins(string time24Hours){ //time24Hours in the format of "##:##"
40         return stoi(time24Hours.substr(0,2))*60+stoi(time24Hours.substr(3));
41     }
42 };

原文地址:https://www.cnblogs.com/ruisha/p/9368111.html

时间: 2024-08-30 03:59:44

539. Minimum Time Difference的相关文章

Python解Leetcode: 539. Minimum Time Difference

题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值. 思路: 把给定的链表排序,并且在排序的同时把60进制的时间转化成十进制整数: 遍历排序的数组,求出两个相邻值之间的差值: 求出首尾两个值之间的差值. class Solution(object): def findMinDifference(self, timePoints): """ :type timePoints: List[str] :rtype: int """

539 Minimum Time Difference 最小时间差

给定一个 24 小时制(小时:分钟)的时间列表,找出列表中任意两个时间的最小时间差并已分钟数表示.示例 1:输入: ["23:59","00:00"]输出: 1备注:    1.列表中时间数在 2~20000 之间.    2.每个时间取值在 00:00~23:59 之间.详见:https://leetcode.com/problems/minimum-time-difference/description/C++: class Solution { public:

Leetcode 530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

530. 平衡二叉树中,相邻节点的最小差 Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

[LeetCode] 530. Minimum Absolute Difference in BST Java

题目: Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and

LeetCode Minimum Absolute Difference in BST

原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Ex

530. Minimum Absolute Difference in BST(LeetCode)

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

Binary Search Tree-530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

[leetcode]Binary Search Tree-530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o