-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode_539.java
28 lines (24 loc) · 939 Bytes
/
Leetcode_539.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public int findMinDifference(List<String> timePoints) {
// convert input to minutes
int[] minutes = new int[timePoints.size()];
for (int i = 0; i < timePoints.size(); i++) {
String time = timePoints.get(i);
int h = Integer.parseInt(time.substring(0, 2));
int m = Integer.parseInt(time.substring(3));
minutes[i] = h * 60 + m;
}
// sort times in ascending order
Arrays.sort(minutes);
// find minimum difference across adjacent elements
int ans = Integer.MAX_VALUE;
for (int i = 0; i < minutes.length - 1; i++) {
ans = Math.min(ans, minutes[i + 1] - minutes[i]);
}
// consider difference between last and first element
return Math.min(
ans,
24 * 60 - minutes[minutes.length - 1] + minutes[0]
);
}
}