Skip to content

Commit 67d6aa2

Browse files
committed
Add solution #1229
1 parent 3919851 commit 67d6aa2

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@
11411141
1224|[Maximum Equal Frequency](./solutions/1224-maximum-equal-frequency.js)|Hard|
11421142
1227|[Airplane Seat Assignment Probability](./solutions/1227-airplane-seat-assignment-probability.js)|Medium|
11431143
1228|[Missing Number In Arithmetic Progression](./solutions/1228-missing-number-in-arithmetic-progression.js)|Easy|
1144+
1229|[Meeting Scheduler](./solutions/1229-meeting-scheduler.js)|Medium|
11441145
1232|[Check If It Is a Straight Line](./solutions/1232-check-if-it-is-a-straight-line.js)|Easy|
11451146
1233|[Remove Sub-Folders from the Filesystem](./solutions/1233-remove-sub-folders-from-the-filesystem.js)|Medium|
11461147
1234|[Replace the Substring for Balanced String](./solutions/1234-replace-the-substring-for-balanced-string.js)|Medium|

solutions/1229-meeting-scheduler.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* 1229. Meeting Scheduler
3+
* https://leetcode.com/problems/meeting-scheduler/
4+
* Difficulty: Medium
5+
*
6+
* Given the availability time slots arrays slots1 and slots2 of two people and a meeting
7+
* duration duration, return the earliest time slot that works for both of them and is of
8+
* duration duration.
9+
*
10+
* If there is no common time slot that satisfies the requirements, return an empty array.
11+
*
12+
* The format of a time slot is an array of two elements [start, end] representing an
13+
* inclusive time range from start to end.
14+
*
15+
* It is guaranteed that no two availability slots of the same person intersect with each
16+
* other. That is, for any two time slots [start1, end1] and [start2, end2] of the same
17+
* person, either start1 > end2 or start2 > end1.
18+
*/
19+
20+
/**
21+
* @param {number[][]} slots1
22+
* @param {number[][]} slots2
23+
* @param {number} duration
24+
* @return {number[]}
25+
*/
26+
var minAvailableDuration = function(slots1, slots2, duration) {
27+
slots1.sort((a, b) => a[0] - b[0]);
28+
slots2.sort((a, b) => a[0] - b[0]);
29+
30+
let i = 0;
31+
let j = 0;
32+
33+
while (i < slots1.length && j < slots2.length) {
34+
const overlapStart = Math.max(slots1[i][0], slots2[j][0]);
35+
const overlapEnd = Math.min(slots1[i][1], slots2[j][1]);
36+
37+
if (overlapEnd - overlapStart >= duration) {
38+
return [overlapStart, overlapStart + duration];
39+
}
40+
41+
if (slots1[i][1] < slots2[j][1]) {
42+
i++;
43+
} else {
44+
j++;
45+
}
46+
}
47+
48+
return [];
49+
};

0 commit comments

Comments
 (0)