Skip to content

Commit 71e7033

Browse files
committedMar 30, 2025
Add solution #986
1 parent 30ffd8f commit 71e7033

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,067 LeetCode solutions in JavaScript
1+
# 1,068 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -794,6 +794,7 @@
794794
983|[Minimum Cost For Tickets](./solutions/0983-minimum-cost-for-tickets.js)|Medium|
795795
984|[String Without AAA or BBB](./solutions/0984-string-without-aaa-or-bbb.js)|Medium|
796796
985|[Sum of Even Numbers After Queries](./solutions/0985-sum-of-even-numbers-after-queries.js)|Easy|
797+
986|[Interval List Intersections](./solutions/0986-interval-list-intersections.js)|Medium|
797798
989|[Add to Array-Form of Integer](./solutions/0989-add-to-array-form-of-integer.js)|Easy|
798799
994|[Rotting Oranges](./solutions/0994-rotting-oranges.js)|Medium|
799800
997|[Find the Town Judge](./solutions/0997-find-the-town-judge.js)|Easy|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 986. Interval List Intersections
3+
* https://leetcode.com/problems/interval-list-intersections/
4+
* Difficulty: Medium
5+
*
6+
* You are given two lists of closed intervals, firstList and secondList, where
7+
* firstList[i] = [starti, endi] and secondList[j] = [startj, endj]. Each list of
8+
* intervals is pairwise disjoint and in sorted order.
9+
*
10+
* Return the intersection of these two interval lists.
11+
*
12+
* A closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b.
13+
*
14+
* The intersection of two closed intervals is a set of real numbers that are either empty
15+
* or represented as a closed interval. For example, the intersection of [1, 3] and
16+
* [2, 4] is [2, 3].
17+
*/
18+
19+
/**
20+
* @param {number[][]} firstList
21+
* @param {number[][]} secondList
22+
* @return {number[][]}
23+
*/
24+
var intervalIntersection = function(firstList, secondList) {
25+
const result = [];
26+
let i = 0;
27+
let j = 0;
28+
29+
while (i < firstList.length && j < secondList.length) {
30+
const start = Math.max(firstList[i][0], secondList[j][0]);
31+
const end = Math.min(firstList[i][1], secondList[j][1]);
32+
33+
if (start <= end) {
34+
result.push([start, end]);
35+
}
36+
37+
if (firstList[i][1] < secondList[j][1]) {
38+
i++;
39+
} else {
40+
j++;
41+
}
42+
}
43+
44+
return result;
45+
};

0 commit comments

Comments
 (0)
Please sign in to comment.