> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Set Intersection Size At Least Two

> Tested Python solution for LeetCode 757 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 757, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/set-intersection-size-at-least-two/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 757   # by problem number
lcpy gen -s set_intersection_size_at_least_two   # by problem name
```

## Problem

You are given a 2D integer array `intervals` where `intervals[i] = [starti, endi]` represents all the integers from `starti` to `endi` inclusively.

A **containing set** is an array `nums` where each interval from `intervals` has **at least two** integers in `nums`.

For example, if `intervals = [[1,3], [3,7], [8,9]]`, then `[1,2,4,7,8,9]` and `[2,3,4,8,9]` are containing sets.

Return the minimum possible size of a containing set.

### Examples

```
Input: intervals = [[1,3],[3,7],[8,9]]
Output: 5
```

**Explanation:** let nums = \[2, 3, 4, 8, 9]. It can be shown that there cannot be any containing array of size 4.

```
Input: intervals = [[1,3],[1,4],[2,5],[3,5]]
Output: 3
```

**Explanation:** let nums = \[2, 3, 4]. It can be shown that there cannot be any containing array of size 2.

```
Input: intervals = [[1,2],[2,3],[2,4],[4,5]]
Output: 5
```

**Explanation:** let nums = \[1, 2, 3, 4, 5]. It can be shown that there cannot be any containing array of size 4.

### Constraints

* `1 <= intervals.length <= 3000`
* `intervals[i].length == 2`
* `0 <= starti < endi <= 10^8`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/set_intersection_size_at_least_two/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/set_intersection_size_at_least_two/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def intersection_size_two(self, intervals: list[list[int]]) -> int:
        # Sort by end ascending, start descending: later intervals shrink leftward,
        # so tracking the two largest chosen numbers suffices to test coverage.
        srt = sorted(intervals, key=lambda iv: (iv[1], -iv[0]))
        second_last = -1
        last = -1
        count = 0
        for start, end in srt:
            if start <= second_last:
                continue
            if start > last:
                count += 2
                second_last, last = end - 1, end
            else:
                count += 1
                second_last, last = last, end
        return count
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.