짬뽕얼큰하게의 맨땅에 헤딩 :: '2025/03/24 글 목록

'2025/03/24'에 해당되는 글 1건

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;
    }
};
블로그 이미지

짬뽕얼큰하게

,