|
3 | 3 | import java.util.List;
|
4 | 4 |
|
5 | 5 | /**
|
6 |
| - * 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. |
| 6 | + * 539. Minimum Time Difference |
| 7 | + * |
| 8 | + * Given a list of 24-hour clock time points in "Hour:Minutes" format, |
| 9 | + * find the minimum minutes difference between any two time points in the list. |
7 | 10 |
|
8 | 11 | Example 1:
|
9 | 12 | Input: ["23:59","00:00"]
|
|
15 | 18 | */
|
16 | 19 | public class _539 {
|
17 | 20 |
|
18 |
| - public int findMinDifference(List<String> timePoints) { |
19 |
| -// there are in total 24*60 = 1440 possible time points |
20 |
| - final int ALL_POSSIBLE_TIMEPOINTS = 1440; |
21 |
| - boolean[] allTimePoints = new boolean[ALL_POSSIBLE_TIMEPOINTS]; |
22 |
| - for (String eachTime : timePoints) { |
23 |
| - String[] timeParts = eachTime.split(":"); |
24 |
| - int hour = Integer.valueOf(timeParts[0]); |
25 |
| - int minute = Integer.valueOf(timeParts[1]); |
26 |
| - int value = hour * 60 + minute; |
27 |
| - if (allTimePoints[value]) { |
28 |
| - return 0; |
| 21 | + public static class Soluiton1 { |
| 22 | + |
| 23 | + public int findMinDifference(List<String> timePoints) { |
| 24 | + /**there are in total 24*60 = 1440 possible time points*/ |
| 25 | + final int ALL_POSSIBLE_TIMEPOINTS = 1440; |
| 26 | + boolean[] allTimePoints = new boolean[ALL_POSSIBLE_TIMEPOINTS]; |
| 27 | + for (String eachTime : timePoints) { |
| 28 | + String[] timeParts = eachTime.split(":"); |
| 29 | + int hour = Integer.valueOf(timeParts[0]); |
| 30 | + int minute = Integer.valueOf(timeParts[1]); |
| 31 | + int value = hour * 60 + minute; |
| 32 | + if (allTimePoints[value]) { |
| 33 | + return 0; |
| 34 | + } |
| 35 | + allTimePoints[value] = true; |
29 | 36 | }
|
30 |
| - allTimePoints[value] = true; |
31 |
| - } |
32 | 37 |
|
33 |
| - int min = Integer.MAX_VALUE; |
34 |
| - int prev = 0; |
35 |
| - int first = Integer.MAX_VALUE; |
36 |
| - int last = Integer.MIN_VALUE; |
37 |
| - for (int i = 0; i < ALL_POSSIBLE_TIMEPOINTS; i++) { |
38 |
| - if (allTimePoints[i]) { |
39 |
| - if (first != Integer.MAX_VALUE) { |
40 |
| - min = Math.min(min, i - prev); |
| 38 | + int min = Integer.MAX_VALUE; |
| 39 | + int prev = 0; |
| 40 | + int first = Integer.MAX_VALUE; |
| 41 | + int last = Integer.MIN_VALUE; |
| 42 | + for (int i = 0; i < ALL_POSSIBLE_TIMEPOINTS; i++) { |
| 43 | + if (allTimePoints[i]) { |
| 44 | + if (first != Integer.MAX_VALUE) { |
| 45 | + min = Math.min(min, i - prev); |
| 46 | + } |
| 47 | + first = Math.min(first, i); |
| 48 | + last = Math.max(last, i); |
| 49 | + prev = i; |
41 | 50 | }
|
42 |
| - first = Math.min(first, i); |
43 |
| - last = Math.max(last, i); |
44 |
| - prev = i; |
45 | 51 | }
|
| 52 | + min = Math.min(min, (ALL_POSSIBLE_TIMEPOINTS - last + first)); |
| 53 | + return min; |
46 | 54 | }
|
47 |
| - min = Math.min(min, (ALL_POSSIBLE_TIMEPOINTS - last + first)); |
48 |
| - return min; |
49 | 55 | }
|
50 | 56 |
|
51 | 57 | }
|
0 commit comments