Skip to content

Commit 0fe1dde

Browse files
committed
Added the Solution of Count Days Without Meetings
1 parent 47fec14 commit 0fe1dde

File tree

2 files changed

+296
-1
lines changed

2 files changed

+296
-1
lines changed

dsa-problems/leetcode-problems/3100-3199.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,18 @@ export const problems = [
418418
"leetCodeLink": "https://leetcode.com/problems/better-compression-of-string",
419419
"solutionLink": "#"
420420
},
421+
{
422+
"problemName": "3169. Count Days Without Meetings",
423+
"difficulty": "Medium",
424+
"leetCodeLink": "https://leetcode.com/problems/count-days-without-meetings/description/",
425+
"solutionLink": "/dsa-solutions/lc-solutions/3100-3199/count-days-without-meetings"
426+
},
427+
421428
{
422429
"problemName": "3179.Find the N-th Value After K Seconds",
423430
"difficulty": "Medium",
424431
"leetCodeLink": "https://leetcode.com/problems/find-the-n-th-value-after-k-seconds/",
425-
"solutionLink": "/dsa-solutions/lc-solutions/3100-3189/find-the-n-th-value-after-k-seconds"
432+
"solutionLink": "/dsa-solutions/lc-solutions/3100-3199/find-the-n-th-value-after-k-seconds"
426433
}
427434
];
428435

Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
---
2+
id: count-days-without-meetings
3+
title: Count Days Without Meetings
4+
sidebar_label: 3169. Count Days Without Meetings
5+
tags:
6+
- Array
7+
- Sorting
8+
description: "This is a solution to the Count Days Without Meetings problem on LeetCode."
9+
---
10+
11+
## Problem Description
12+
You are given a positive integer days representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array meetings of size n where, `meetings[i] = [start_i, end_i]` represents the starting and ending days of meeting i (inclusive).
13+
14+
Return the count of days when the employee is available for work but no meetings are scheduled.
15+
16+
Note: The meetings may overlap.
17+
### Examples
18+
19+
**Example 1:**
20+
21+
```
22+
Input: days = 10, meetings = [[5,7],[1,3],[9,10]]
23+
24+
Output: 2
25+
26+
Explanation:
27+
28+
There is no meeting scheduled on the 4th and 8th days.
29+
30+
```
31+
32+
**Example 2:**
33+
```
34+
Input: days = 5, meetings = [[2,4],[1,3]]
35+
36+
Output: 1
37+
38+
Explanation:
39+
40+
There is no meeting scheduled on the 5th day.
41+
```
42+
**Example 3:**
43+
```
44+
Input: days = 6, meetings = [[1,6]]
45+
46+
Output: 0
47+
48+
Explanation:
49+
50+
Meetings are scheduled for all working days.
51+
```
52+
53+
### Constraints
54+
- `1 <= days <= 10^9`
55+
- `1 <= meetings.length <= 10^5`
56+
- `meetings[i].length == 2`
57+
- `1 <= meetings[i][0] <= meetings[i][1] <= days`
58+
59+
## Solution for Find the N-th Value After K Seconds Problem
60+
### Prerequisite - Do Merge Interval Question On Leetcode
61+
### Approach
62+
To solve the problem of counting non-working days given a list of intervals that denote working days, we can break down the solution into the following steps:
63+
64+
- Merge Intervals: Given a list of intervals representing working days, merge any overlapping intervals to consolidate the total working days. This step ensures that we account for continuous working periods without duplication.
65+
66+
- Count Working Days: After merging the intervals, calculate the total number of working days by summing up the lengths of all the merged intervals.
67+
68+
- Calculate Non-Working Days: Subtract the total number of working days from the total number of days to get the number of non-working days.
69+
70+
### Detailed Steps
71+
#### Merge Intervals:
72+
73+
- Sort the intervals based on the start day.
74+
- Iterate through the intervals and merge them if they overlap.
75+
- Maintain a list of merged intervals.
76+
#### Count Working Days:
77+
78+
- For each merged interval, calculate the length of the interval (end day - start day + 1).
79+
- Sum up the lengths to get the total number of working days.
80+
81+
#### Calculate Non-Working Days:
82+
- Subtract the total number of working days from the total number of days given.
83+
84+
85+
<Tabs>
86+
<TabItem value="Solution" label="Solution">
87+
88+
#### Implementation
89+
```jsx live
90+
function Solution(arr) {
91+
function merge(intervals) {
92+
intervals.sort((a, b) => a[0] - b[0]);
93+
const output = [];
94+
for (let interval of intervals) {
95+
if (output.length === 0 || output[output.length - 1][1] < interval[0]) {
96+
output.push(interval);
97+
} else {
98+
output[output.length - 1][1] = Math.max(output[output.length - 1][1], interval[1]);
99+
}
100+
}
101+
return output;
102+
}
103+
104+
function countDays(days, meetings) {
105+
const ans = merge(meetings);
106+
for (let [st, end] of ans) {
107+
days -= (end - st + 1);
108+
}
109+
return days;
110+
}
111+
const input = [[5,7],[1,3],[9,10]];
112+
const days = 10
113+
const output = countDays(days , input)
114+
return (
115+
<div>
116+
<p>
117+
<b>Input: </b>
118+
{JSON.stringify(input)}
119+
</p>
120+
<p>
121+
<b>Output:</b> {output.toString()}
122+
</p>
123+
</div>
124+
);
125+
}
126+
```
127+
128+
#### Complexity Analysis
129+
130+
- Time Complexity: $ O(nlogn) $
131+
- Space Complexity: $ O(1) $
132+
## Code in Different Languages
133+
<Tabs>
134+
<TabItem value="JavaScript" label="JavaScript">
135+
<SolutionAuthor name="@hiteshgahanolia"/>
136+
```javascript
137+
function merge(intervals) {
138+
intervals.sort((a, b) => a[0] - b[0]);
139+
const output = [];
140+
for (let interval of intervals) {
141+
if (output.length === 0 || output[output.length - 1][1] < interval[0]) {
142+
output.push(interval);
143+
} else {
144+
output[output.length - 1][1] = Math.max(output[output.length - 1][1], interval[1]);
145+
}
146+
}
147+
return output;
148+
}
149+
150+
function countDays(days, meetings) {
151+
const ans = merge(meetings);
152+
for (let [st, end] of ans) {
153+
days -= (end - st + 1);
154+
}
155+
return days;
156+
}
157+
```
158+
159+
</TabItem>
160+
<TabItem value="TypeScript" label="TypeScript">
161+
<SolutionAuthor name="@hiteshgahanolia"/>
162+
```typescript
163+
class Solution {
164+
merge(intervals: number[][]): number[][] {
165+
intervals.sort((a, b) => a[0] - b[0]);
166+
const output: number[][] = [];
167+
for (const interval of intervals) {
168+
if (output.length === 0 || output[output.length - 1][1] < interval[0]) {
169+
output.push(interval);
170+
} else {
171+
output[output.length - 1][1] = Math.max(output[output.length - 1][1], interval[1]);
172+
}
173+
}
174+
return output;
175+
}
176+
177+
countDays(days: number, meetings: number[][]): number {
178+
const ans = this.merge(meetings);
179+
for (const [st, end] of ans) {
180+
days -= (end - st + 1);
181+
}
182+
return days;
183+
}
184+
}
185+
186+
```
187+
</TabItem>
188+
<TabItem value="Python" label="Python">
189+
<SolutionAuthor name="@hiteshgahanolia"/>
190+
```python
191+
class Solution:
192+
def merge(self, intervals):
193+
intervals.sort()
194+
output = []
195+
for interval in intervals:
196+
if not output or output[-1][1] < interval[0]:
197+
output.append(interval)
198+
else:
199+
output[-1][1] = max(output[-1][1], interval[1])
200+
return output
201+
202+
def count_days(self, days, meetings):
203+
ans = self.merge(meetings)
204+
for st, end in ans:
205+
days -= (end - st + 1)
206+
return days
207+
208+
```
209+
</TabItem>
210+
<TabItem value="Java" label="Java">
211+
<SolutionAuthor name="@hiteshgahanolia"/>
212+
```java
213+
import java.util.*;
214+
215+
public class Solution {
216+
public List<int[]> merge(List<int[]> intervals) {
217+
int n = intervals.size();
218+
intervals.sort((a, b) -> a[0] - b[0]);
219+
List<int[]> output = new ArrayList<>();
220+
for (int[] interval : intervals) {
221+
if (output.isEmpty() || output.get(output.size() - 1)[1] < interval[0]) {
222+
output.add(interval);
223+
} else {
224+
output.get(output.size() - 1)[1] = Math.max(output.get(output.size() - 1)[1], interval[1]);
225+
}
226+
}
227+
return output;
228+
}
229+
230+
public int countDays(int days, List<int[]> meetings) {
231+
List<int[]> ans = merge(meetings);
232+
for (int[] i : ans) {
233+
int st = i[0];
234+
int end = i[1];
235+
days -= (end - st + 1);
236+
}
237+
return days;
238+
}
239+
}
240+
241+
```
242+
243+
</TabItem>
244+
<TabItem value="C++" label="C++">
245+
<SolutionAuthor name="@hiteshgahanolia"/>
246+
```cpp
247+
class Solution {
248+
public:
249+
vector<vector<int>> merge(vector<vector<int>>& intervals) {
250+
int n = intervals.size();
251+
sort(intervals.begin(), intervals.end());
252+
vector<vector<int>> output;
253+
for(auto interval : intervals){
254+
if(output.empty() || output.back()[1] < interval[0]){
255+
output.push_back(interval);
256+
}
257+
else{
258+
output.back()[1] = max(output.back()[1], interval[1]);
259+
}
260+
}
261+
return output;
262+
}
263+
int countDays(int days, vector<vector<int>>& meetings) {
264+
vector<vector<int>>ans = merge(meetings);
265+
sort(begin(ans) , end(ans));
266+
for(auto i:ans)
267+
{
268+
int st = i[0];
269+
int end = i[1];
270+
271+
days -=(end -st +1);
272+
}
273+
return days;
274+
}
275+
};
276+
```
277+
</TabItem>
278+
</Tabs>
279+
280+
</TabItem>
281+
</Tabs>
282+
283+
## References
284+
285+
- **LeetCode Problem**: [Count Days Without Meetings](https://leetcode.com/problems/count-days-without-meetings/description/)
286+
287+
- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/count-days-without-meetings/solutions/)
288+

0 commit comments

Comments
 (0)