Skip to content

Commit 7e2b6e6

Browse files
authored
Create 3169. Count Days Without Meetings (#750)
2 parents c34bbc3 + b00c04b commit 7e2b6e6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

3169. Count Days Without Meetings

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
};

0 commit comments

Comments
 (0)