Skip to content

Create 3169. Count Days Without Meetings #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions 3169. Count Days Without Meetings
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
public:
int countDays(int days, vector<vector<int>>& meetings) {
const int n = meetings.size(), N=2*n;
vector<unsigned> info(N);
int i=0;

// Encode meeting start and end
for (auto& m : meetings) {
const unsigned s= m[0], e=m[1];
info[i++]=(s<<1)|1; // Start, mark with LSB=1
info[i++]=(e+1)<<1; // End (exclusive)
}

// Sort the events
sort(info.begin(), info.end());

int overlap=0, cnt=0, last=1;

// Process events in sorted order
for (int i=0; i<N; i++) {
const int x=info[i]>>1; // Extract day
const bool isStart=info[i]&1;

// If no overlap, count the days between last and current
if (overlap==0 && last<x)
cnt+=(x-last);

overlap+=isStart?1:-1; // Increment on start, decrement on end
if(overlap==0) last=x; // Update last when no overlap
}

// free between last & days
if (last<=days)
cnt+=(days-last+1);

return cnt;
}
};
Loading