-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDaysWithoutMeetings3169.java
More file actions
50 lines (39 loc) · 1.58 KB
/
CountDaysWithoutMeetings3169.java
File metadata and controls
50 lines (39 loc) · 1.58 KB
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
46
47
48
49
50
import java.util.Arrays;
import java.util.Comparator;
public class CountDaysWithoutMeetings3169 {
/*
* You are given a positive integer days representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array meetings of size n where, meetings[i] = [start_i, end_i] represents the starting and ending days of meeting i (inclusive).
Return the count of days when the employee is available for work but no meetings are scheduled.
Note: The meetings may overlap.
*/
class Solution {
public int countDays(int days, int[][] meetings) {
int count = 0;
boolean[] isMeeting = new boolean[days + 1];
for (int i = 0; i < meetings.length; i++) {
for (int j = meetings[i][0]; j <= meetings[i][1]; j++) {
isMeeting[j] = true;
}
}
for (int i = 1; i <= days; i++) {
if (!isMeeting[i]) {
count++;
}
}
return count;
}
}
public int countDays(int days, int[][] meetings) {
int freeDays = 0, latestEnd = 0;
Arrays.sort(meetings, Comparator.comparingInt(a -> a[0]));
for (int[] meeting : meetings) {
int start = meeting[0], end = meeting[1];
if (start > latestEnd + 1) {
freeDays += start - latestEnd - 1;
}
latestEnd = Math.max(latestEnd, end);
}
freeDays += days - latestEnd;
return freeDays;
}
}