|
| 1 | +package easy200; |
| 2 | + |
| 3 | +import java.text.DateFormat; |
| 4 | +import java.text.ParseException; |
| 5 | +import java.text.SimpleDateFormat; |
| 6 | +import java.util.Date; |
| 7 | + |
| 8 | +public class FastSki { |
| 9 | + public String getLast(String[] times) { |
| 10 | + long max = -1 << 20; |
| 11 | + |
| 12 | + for (int i = 0; i < times.length; i++) { |
| 13 | + String[] time = times[i].split("-"); |
| 14 | + DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); |
| 15 | + Date reference = null, date1 = null, date2 = null; |
| 16 | + try { |
| 17 | + reference = dateFormat.parse("00:00:00"); |
| 18 | + date1 = dateFormat.parse(time[0]); |
| 19 | + date2 = dateFormat.parse(time[1]); |
| 20 | + } catch (ParseException e) { |
| 21 | + e.printStackTrace(); |
| 22 | + } |
| 23 | + |
| 24 | + long secondsLeft = (date1.getTime() - reference.getTime()) / 1000L; |
| 25 | + long secondsRight = (date2.getTime() - reference.getTime()) / 1000L; |
| 26 | + max = Math.max(max, secondsRight - secondsLeft); |
| 27 | + } |
| 28 | + |
| 29 | + int hours = 0, minutes = 0, seconds = 0; |
| 30 | + |
| 31 | + while (max >= 3600) { |
| 32 | + hours++; |
| 33 | + max -= 3600; |
| 34 | + } |
| 35 | + while (max >= 60) { |
| 36 | + minutes++; |
| 37 | + max -= 60; |
| 38 | + } |
| 39 | + while (max > 0) { |
| 40 | + seconds++; |
| 41 | + max--; |
| 42 | + } |
| 43 | + |
| 44 | + return String.format("%02d:%02d:%02d", hours, minutes, seconds); |
| 45 | + } |
| 46 | + |
| 47 | + public static void main(String[] args) { |
| 48 | + FastSki f = new FastSki(); |
| 49 | + System.out.println(f.getLast(new String[] { "20:03:05-22:06:31", "04:20:05-15:51:14", "08:39:33-10:31:49" })); |
| 50 | + System.out.println(f.getLast(new String[] { "13:09:14-13:10:27", "12:47:19-20:40:58", "08:47:40-14:25:34", |
| 51 | + "13:44:31-14:17:07", "18:22:38-18:27:29" })); |
| 52 | + System.out.println(f.getLast(new String[] { "14:13:08-23:40:27", "10:35:30-17:01:21", "12:30:02-23:27:19", |
| 53 | + "01:41:40-14:08:05", "11:03:53-16:56:54", "03:07:59-08:08:06", "16:48:06-23:41:52", "10:56:14-20:25:58", |
| 54 | + "10:25:54-17:43:48" })); |
| 55 | + } |
| 56 | +} |
0 commit comments