Leetcode Problem:
Summary
- Given a positive integer days representing the total number of days an employee is available for work, and 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.
Approach
- The approach used is to first sort the meetings array, then initialize two pointers l and r to the start and end of the first meeting respectively
- Then, iterate over the rest of the meetings and update the end of the last meeting that ends earliest
- Finally, add the available days before the first meeting and after the last meeting to the answer.
Complexity
- O(n log n) due to the sorting of meetings array, where n is the number of meetings.
Solution Code:
class Solution {
public:
int countDays(int days, vector>& meetings) {
sort(meetings.begin(), meetings.end());
int l = meetings[0][0];
int r = meetings[0][1];
int ans = 0;
ans += l - 1;
for(int i = 1 ; i < meetings.size(); i++){
int ll = meetings[i][0];
int rr = meetings[i][1];
cout << ll << " " << rr << endl;
if(r < ll){
ans += ll - r - 1;
}
r = max(r, rr);
}
ans += days - r;
return ans;
}
};
class Solution {
public:
int countDays(int days, vector>& meetings) {
sort(meetings.begin(), meetings.end());
int l = meetings[0][0];
int r = meetings[0][1];
int ans = 0;
ans += l - 1;
for(int i = 1 ; i < meetings.size(); i++){
int ll = meetings[i][0];
int rr = meetings[i][1];
cout << ll << " " << rr << endl;
if(r < ll){
ans += ll - r - 1;
}
r = max(r, rr);
}
ans += days - r;
return ans;
}
};
'알고리즘' 카테고리의 다른 글
Minimum Operations to Make a Uni-Value Grid (0) | 2025.03.27 |
---|---|
Check if Grid can be Cut into Sections (0) | 2025.03.25 |
Count the Number of Complete Components (0) | 2025.03.22 |
Find All Possible Recipes from Given Supplies (0) | 2025.03.21 |
Minimum Cost Walk in Weighted Graph (0) | 2025.03.20 |