|
3 | 3 | importjava.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 | publicclass_539 {
|
17 | 20 |
|
18 |
| -publicintfindMinDifference(List<String>timePoints) { |
19 |
| -// there are in total 24*60 = 1440 possible time points |
20 |
| -finalintALL_POSSIBLE_TIMEPOINTS =1440; |
21 |
| -boolean[]allTimePoints =newboolean[ALL_POSSIBLE_TIMEPOINTS]; |
22 |
| -for (StringeachTime :timePoints) { |
23 |
| -String[]timeParts =eachTime.split(":"); |
24 |
| -inthour =Integer.valueOf(timeParts[0]); |
25 |
| -intminute =Integer.valueOf(timeParts[1]); |
26 |
| -intvalue =hour *60 +minute; |
27 |
| -if (allTimePoints[value]) { |
28 |
| -return0; |
| 21 | +publicstaticclassSoluiton1 { |
| 22 | + |
| 23 | +publicintfindMinDifference(List<String>timePoints) { |
| 24 | +/**there are in total 24*60 = 1440 possible time points*/ |
| 25 | +finalintALL_POSSIBLE_TIMEPOINTS =1440; |
| 26 | +boolean[]allTimePoints =newboolean[ALL_POSSIBLE_TIMEPOINTS]; |
| 27 | +for (StringeachTime :timePoints) { |
| 28 | +String[]timeParts =eachTime.split(":"); |
| 29 | +inthour =Integer.valueOf(timeParts[0]); |
| 30 | +intminute =Integer.valueOf(timeParts[1]); |
| 31 | +intvalue =hour *60 +minute; |
| 32 | +if (allTimePoints[value]) { |
| 33 | +return0; |
| 34 | + } |
| 35 | +allTimePoints[value] =true; |
29 | 36 | }
|
30 |
| -allTimePoints[value] =true; |
31 |
| - } |
32 | 37 |
|
33 |
| -intmin =Integer.MAX_VALUE; |
34 |
| -intprev =0; |
35 |
| -intfirst =Integer.MAX_VALUE; |
36 |
| -intlast =Integer.MIN_VALUE; |
37 |
| -for (inti =0;i <ALL_POSSIBLE_TIMEPOINTS;i++) { |
38 |
| -if (allTimePoints[i]) { |
39 |
| -if (first !=Integer.MAX_VALUE) { |
40 |
| -min =Math.min(min,i -prev); |
| 38 | +intmin =Integer.MAX_VALUE; |
| 39 | +intprev =0; |
| 40 | +intfirst =Integer.MAX_VALUE; |
| 41 | +intlast =Integer.MIN_VALUE; |
| 42 | +for (inti =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 | +returnmin; |
46 | 54 | }
|
47 |
| -min =Math.min(min, (ALL_POSSIBLE_TIMEPOINTS -last +first)); |
48 |
| -returnmin; |
49 | 55 | }
|
50 | 56 |
|
51 | 57 | }
|