Last active
December 27, 2015 11:09
-
-
Save yssharma/7315878 to your computer and use it in GitHub Desktop.
Playing around with scheduling problems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package puzzles; | |
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| import java.util.List; | |
| import java.util.Scanner; | |
| /* | |
| * | |
| * Sample Input: | |
| 8 60 | |
| 08 00 10 15 | |
| 22 00 23 15 | |
| 17 00 19 00 | |
| 07 00 09 45 | |
| 09 00 13 00 | |
| 16 00 17 45 | |
| 12 00 13 30 | |
| 11 30 12 30 | |
| Sample Output: | |
| 00 00 07 00 | |
| 13 30 16 00 | |
| 19 00 22 00 | |
| Sample Input: | |
| 5 120 | |
| 16 00 17 00 | |
| 10 30 14 30 | |
| 20 45 22 15 | |
| 10 00 13 15 | |
| 09 00 11 00 | |
| Sample Output: | |
| 00 00 09 00 | |
| 17 00 20 45 | |
| */ | |
| class Time implements Comparable<Time> { | |
| int start; | |
| int end; | |
| public Time(int start, int end) { | |
| this.start = start; | |
| this.end = end; | |
| } | |
| @Override | |
| public String toString() { | |
| return "" + String.format("%02d", start/60) + " " + String.format("%02d", start % 60) + " " + String.format("%02d", end / 60) + " " + String.format("%02d", end% 60); | |
| } | |
| @Override | |
| public int compareTo(Time t) { | |
| if (this.start < t.start) | |
| return -1; | |
| else if (this.start > t.start) | |
| return 1; | |
| else | |
| return 0; | |
| } | |
| } | |
| public class MeetingSchedules { | |
| public void print(List<Time> list) { | |
| System.out.println("-----------------------"); | |
| for (Time t : list) | |
| System.out.println(t); | |
| } | |
| public void solve() { | |
| Scanner sc = new Scanner(System.in); | |
| int N = sc.nextInt(); | |
| int K = sc.nextInt(); | |
| List<Time> list = new ArrayList<Time>(); | |
| int START = 0; | |
| int END = 24 * 60; | |
| for (int i = 0; i < N; i++) { | |
| list.add(new Time((sc.nextInt() * 60 + sc.nextInt()), | |
| (sc.nextInt() * 60 + sc.nextInt()))); | |
| } | |
| print(list); | |
| Collections.sort(list); | |
| print(list); | |
| List<Time> result = new ArrayList<Time>(); | |
| for (int i = 0; i < N; i++) { | |
| if (i == 0) { | |
| if (list.get(i).start - START >= K) { | |
| result.add(new Time(START, list.get(i).start)); | |
| } | |
| continue; | |
| } | |
| if (i == N - 1) { | |
| if (list.get(i).end - END >= K) { | |
| result.add(new Time(list.get(i).end, END)); | |
| } | |
| } | |
| if (list.get(i).start - list.get(i - 1).end >= K) { | |
| result.add(new Time(list.get(i - 1).end, list.get(i).start)); | |
| } | |
| } | |
| print(result); | |
| } | |
| public static void main(String[] args) { | |
| new MeetingSchedules().solve(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment