File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int countDays(int days, vector<vector<int>>& meetings) {
4+ const int n = meetings.size(), N=2*n;
5+ vector<unsigned> info(N);
6+ int i=0;
7+
8+ // Encode meeting start and end
9+ for (auto& m : meetings) {
10+ const unsigned s= m[0], e=m[1];
11+ info[i++]=(s<<1)|1; // Start, mark with LSB=1
12+ info[i++]=(e+1)<<1; // End (exclusive)
13+ }
14+
15+ // Sort the events
16+ sort(info.begin(), info.end());
17+
18+ int overlap=0, cnt=0, last=1;
19+
20+ // Process events in sorted order
21+ for (int i=0; i<N; i++) {
22+ const int x=info[i]>>1; // Extract day
23+ const bool isStart=info[i]&1;
24+
25+ // If no overlap, count the days between last and current
26+ if (overlap==0 && last<x)
27+ cnt+=(x-last);
28+
29+ overlap+=isStart?1:-1; // Increment on start, decrement on end
30+ if(overlap==0) last=x; // Update last when no overlap
31+ }
32+
33+ // free between last & days
34+ if (last<=days)
35+ cnt+=(days-last+1);
36+
37+ return cnt;
38+ }
39+ };
You can’t perform that action at this time.
0 commit comments