Skip to content

Commit c2e0924

Browse files
committed
Add solution #757
1 parent 53030a1 commit c2e0924

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@
574574
753|[Cracking the Safe](./0753-cracking-the-safe.js)|Hard|
575575
754|[Reach a Number](./0754-reach-a-number.js)|Medium|
576576
756|[Pyramid Transition Matrix](./0756-pyramid-transition-matrix.js)|Medium|
577+
757|[Set Intersection Size At Least Two](./0757-set-intersection-size-at-least-two.js)|Hard|
577578
762|[Prime Number of Set Bits in Binary Representation](./0762-prime-number-of-set-bits-in-binary-representation.js)|Easy|
578579
763|[Partition Labels](./0763-partition-labels.js)|Medium|
579580
783|[Minimum Distance Between BST Nodes](./0783-minimum-distance-between-bst-nodes.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 757. Set Intersection Size At Least Two
3+
* https://leetcode.com/problems/set-intersection-size-at-least-two/
4+
* Difficulty: Hard
5+
*
6+
* You are given a 2D integer array intervals where intervals[i] = [starti, endi] represents
7+
* all the integers from starti to endi inclusively.
8+
*
9+
* A containing set is an array nums where each interval from intervals has at least two
10+
* integers in nums.
11+
*
12+
* For example, if intervals = [[1,3], [3,7], [8,9]], then [1,2,4,7,8,9] and [2,3,4,8,9]
13+
* are containing sets.
14+
*
15+
* Return the minimum possible size of a containing set.
16+
*/
17+
18+
/**
19+
* @param {number[][]} intervals
20+
* @return {number}
21+
*/
22+
var intersectionSizeTwo = function(intervals) {
23+
intervals.sort((a, b) => a[1] - b[1] || b[0] - a[0]);
24+
let size = 0;
25+
let smallest = -1;
26+
let secondSmallest = -1;
27+
28+
for (const [start, end] of intervals) {
29+
const hasTwo = start <= smallest;
30+
const hasOne = start <= secondSmallest;
31+
32+
if (hasTwo) continue;
33+
if (hasOne) {
34+
smallest = secondSmallest;
35+
secondSmallest = end;
36+
size++;
37+
} else {
38+
smallest = end - 1;
39+
secondSmallest = end;
40+
size += 2;
41+
}
42+
}
43+
44+
return size;
45+
};

0 commit comments

Comments
 (0)