Skip to content

Commit a2eae88

Browse files
chore: add LeetCode daily solution
1 parent 8a9544c commit a2eae88

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Set Intersection Size At Least Two (Hard)
2+
3+
**Problem ID:** 757
4+
**Date:** 2025-11-20
5+
**Link:** https://leetcode.com/problems/set-intersection-size-at-least-two/
6+
7+
## Approach
8+
9+
To solve the "Set Intersection Size At Least Two" problem, we need to ensure that every interval in the given list has at least two integers from a selected set. The main idea is to strategically choose integers such that the total count of selected integers is minimized while still satisfying the requirement for all intervals.
10+
11+
### Approach:
12+
13+
1. **Sort Intervals**: Start by sorting the intervals based on their ending points. This allows us to handle overlapping intervals more effectively and ensures that we can cover as many intervals as possible with the least number of integers.
14+
15+
2. **Greedy Selection**: Use a greedy approach to select integers. For each interval, check if it is already covered by the previously selected integers. If it is not covered, we need to add integers to ensure that this interval has at least two integers from our set.
16+
17+
3. **Choosing Integers**:
18+
- If an interval `[start, end]` is not covered, check the last two integers that were added to the set. If both are less than `start`, add the two largest integers from the interval (i.e., `end` and `end-1`).
19+
- If one of the last two integers is within the interval, add the other integer to ensure that the interval is covered with at least two integers.
20+
21+
4. **Data Structures**: Use a list to maintain the selected integers. A set can also be used to quickly check if an integer is already included in the selected integers.
22+
23+
5. **Complexity**: The overall time complexity is O(n log n) due to sorting the intervals, where n is the number of intervals. The greedy selection process runs in O(n), resulting in an efficient solution.
24+
25+
### Summary:
26+
The solution effectively combines sorting and a greedy selection strategy to ensure that each interval is covered by at least two integers while minimizing the total number of integers selected. By focusing on the end points of intervals and ensuring coverage through careful selection, we achieve the required result efficiently.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Arrays;
2+
import java.util.Comparator;
3+
4+
public class Solution {
5+
public int intersectionSizeTwo(int[][] intervals) {
6+
Arrays.sort(intervals, Comparator.comparingInt(a -> a[1]));
7+
int count = 0;
8+
int end1 = -1, end2 = -1;
9+
10+
for (int[] interval : intervals) {
11+
if (end2 < interval[0]) {
12+
count += 2;
13+
end1 = interval[1];
14+
end2 = interval[1] - 1;
15+
} else if (end1 < interval[0]) {
16+
count += 1;
17+
end1 = end2;
18+
end2 = interval[1];
19+
}
20+
}
21+
22+
return count;
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var intersectionSizeTwo = function(intervals) {
2+
intervals.sort((a, b) => a[1] - b[1]);
3+
let count = 0;
4+
let end = -1;
5+
let secondEnd = -1;
6+
7+
for (let [start, finish] of intervals) {
8+
if (end < finish - 1) {
9+
count += 2;
10+
secondEnd = finish;
11+
end = finish - 1;
12+
} else if (end === finish - 1) {
13+
count += 1;
14+
end = secondEnd;
15+
}
16+
}
17+
18+
return count;
19+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def intersectionSizeTwo(self, intervals: List[List[int]]) -> int:
3+
intervals.sort(key=lambda x: (x[1], x[0]))
4+
end1, end2 = -1, -1
5+
count = 0
6+
7+
for start, end in intervals:
8+
if end1 < start:
9+
count += 2
10+
end1, end2 = end - 1, end
11+
elif end2 < start:
12+
count += 1
13+
end1, end2 = end2, end
14+
15+
end1 = max(end1, end - 1)
16+
end2 = max(end2, end)
17+
18+
return count

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
284284
- 2025-11-17 — [Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/) (Easy) → `Easy/2025-11-17-1437-Check-If-All-1-s-Are-at-Least-Length-K-Places-Away`
285285
- 2025-11-18 — [1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/) (Easy) → `Easy/2025-11-18-717-1-bit-and-2-bit-Characters`
286286
- 2025-11-19 — [Keep Multiplying Found Values by Two](https://leetcode.com/problems/keep-multiplying-found-values-by-two/) (Easy) → `Easy/2025-11-19-2154-Keep-Multiplying-Found-Values-by-Two`
287+
- 2025-11-20 — [Set Intersection Size At Least Two](https://leetcode.com/problems/set-intersection-size-at-least-two/) (Hard) → `Hard/2025-11-20-757-Set-Intersection-Size-At-Least-Two`

0 commit comments

Comments
 (0)