forked from edinfazlic/CodinGame-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuperComputer.java
45 lines (37 loc) · 1.05 KB
/
SuperComputer.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
List<Schedule> scheduleTimes = new ArrayList<Schedule>();
for (int i = 0; i < N; i++) {
int J = in.nextInt();
int D = in.nextInt();
scheduleTimes.add(new Schedule(J, J + D - 1));
}
Collections.sort(scheduleTimes);
int calculations = 0, lastEndTime = 0;
for(Schedule schedule : scheduleTimes) {
if(schedule.endTime > lastEndTime && schedule.startTime > lastEndTime) {
lastEndTime = schedule.endTime;
calculations++;
}
}
System.out.println(calculations);
}
static class Schedule implements Comparable<Schedule> {
private int startTime;
private int endTime;
public Schedule(int startTime, int endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
@Override
public int compareTo(Schedule that) {
return endTime < that.endTime ? - 1 : endTime > that.endTime ? 1 : 0;
}
}
}