diff options
-rw-r--r-- | count_days_without_meeting.c | 47 | ||||
-rw-r--r-- | count_days_without_meeting.py | 37 |
2 files changed, 84 insertions, 0 deletions
diff --git a/count_days_without_meeting.c b/count_days_without_meeting.c new file mode 100644 index 0000000..9e1ff35 --- /dev/null +++ b/count_days_without_meeting.c @@ -0,0 +1,47 @@ +// 3169. Count Days Without Meetings +#include "leetcode.h" + +/* + * 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 given a 2d + * integer 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. + */ + +int cmp(const void *a, const void *b) { + int *aa = *(int **)a, *bb = *(int **)b; + return aa[0] == bb[0] ? aa[1] - bb[1] : aa[0] - bb[0]; +} + +int countDays(int days, int **meetings, int meetingsSize, + int *meetingsColSize) { + qsort(meetings, meetingsSize, sizeof(int *), cmp); + int idx = 0, start = meetings[idx][0], end = meetings[idx++][1]; + while (idx < meetingsSize) { + if (meetings[idx][0] > end) { + days -= (end - start + 1); + start = meetings[idx][0]; + end = meetings[idx][1]; + } else + end = end > meetings[idx][1] ? end : meetings[idx][1]; + ++idx; + } + return days - (end - start + 1); +} + +int main() { + int m1i[3][2] = {{5, 7}, {1, 3}, {9, 10}}, m2i[2][2] = {{2, 4}, {1, 3}}, + m3i[1][2] = {{1, 6}}; + struct two_d_arr m1, m2, m3; + two_d_arr_init(&m1, ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i); + two_d_arr_init(&m2, ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i); + two_d_arr_init(&m3, ARRAY_SIZE(m3i), ARRAY_SIZE(m3i[0]), m3i); + printf("%d\n", countDays(10, m1.arr, m1.row_size, m1.col_size)); // expect: 2 + printf("%d\n", countDays(5, m2.arr, m2.row_size, m2.col_size)); // expect: 1 + printf("%d\n", countDays(6, m3.arr, m3.row_size, m3.col_size)); // expect: 0 + two_d_arr_free(&m1); + two_d_arr_free(&m2); + two_d_arr_free(&m3); +} diff --git a/count_days_without_meeting.py b/count_days_without_meeting.py new file mode 100644 index 0000000..c1cee7d --- /dev/null +++ b/count_days_without_meeting.py @@ -0,0 +1,37 @@ +# 3169. Count Days Without Meetings + +""" +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 given a 2d +integer 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. +""" + + +class Solution(object): + def countDays(self, days, meetings): + """ + :type days: int + :type meetings: List[List[int]] + :rtype: int + """ + meetings.sort(key=lambda x: x[0]) + cnt, n = abs(meetings[0][0] - 1), len(meetings) + for i in range(1, n): + if meetings[i][0] <= meetings[i - 1][1]: + if meetings[i][1] < meetings[i - 1][1]: + meetings[i][1] = meetings[i - 1][1] + else: + day = meetings[i][0] - meetings[i - 1][1] + cnt += day - 1 + cnt += abs(meetings[n - 1][1] - days) + return cnt + + +if __name__ == "__main__": + obj = Solution() + print(obj.countDays(days=10, meetings=[[5, 7], [1, 3], [9, 10]])) + print(obj.countDays(days=5, meetings=[[2, 4], [1, 3]])) + print(obj.countDays(days=6, meetings=[[1, 6]])) |