|
| 1 | +# [Problem 757: Set Intersection Size At Least Two](https://leetcode.com/problems/set-intersection-size-at-least-two/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +I need the smallest set of integers such that every interval contains at least two of those integers. This smells like a greedy covering problem. If I process intervals by their right endpoints (earliest finishing first) I can try to place points as far right as possible so they help future intervals too. For each interval I want to know how many of the already chosen points fall inside it. If none, I should add two points inside the interval (preferably the two largest possible, i.e., r-1 and r). If exactly one, add one more (again put it at r). If two or more, nothing to do. |
| 5 | + |
| 6 | +I remember one tricky part: when intervals share the same right endpoint, the ordering by start matters (we should process intervals with larger starts first when ends tie), so that we don't mistakenly think an already chosen point at r suffices for a tighter interval that requires two distinct points. |
| 7 | + |
| 8 | +So approach: sort by end ascending, and for equal ends by start descending, maintain two largest chosen points a (second last) and b (last), and update as we scan. |
| 9 | + |
| 10 | +## Refining the problem, round 2 thoughts |
| 11 | +- Sorting: key = (end, -start). |
| 12 | +- Maintain two latest chosen points a < b (initialize to very small values). For interval [l, r]: |
| 13 | + - If l > b: none of a or b in interval -> pick r-1 and r (ans += 2), set a = r-1, b = r. |
| 14 | + - Else if l > a: exactly one (b) in interval -> pick r (ans += 1), set a = b, b = r. |
| 15 | + - Else: a and b both in interval -> nothing to do. |
| 16 | +- Edge cases: |
| 17 | + - intervals length up to 3000 so sorting O(n log n) is fine. |
| 18 | + - r-1 will always be >= l? Given start < end, r-1 >= l can be equal or greater; r-1 could be < l if interval length is 1? Actually start < end implies at least two integers, e.g., [1,2] => r-1 == 1 == l. So r-1 always >= l. |
| 19 | + - Sorting by start descending for same end prevents incorrect reuse of existing r when we need two distinct points inside tight intervals with same end. |
| 20 | +- Complexity: sorting O(n log n), single pass O(n), space O(1) extra. |
| 21 | + |
| 22 | +## Attempted solution(s) |
| 23 | +```python |
| 24 | +from typing import List |
| 25 | + |
| 26 | +class Solution: |
| 27 | + def intersectionSizeTwo(self, intervals: List[List[int]]) -> int: |
| 28 | + # Sort by end ascending, and for equal end sort by start descending |
| 29 | + intervals.sort(key=lambda x: (x[1], -x[0])) |
| 30 | + |
| 31 | + # a = second-last chosen point, b = last chosen point |
| 32 | + a = -10**18 |
| 33 | + b = -10**18 |
| 34 | + ans = 0 |
| 35 | + |
| 36 | + for l, r in intervals: |
| 37 | + if l > b: |
| 38 | + # neither a nor b is in [l, r]: pick r-1 and r |
| 39 | + ans += 2 |
| 40 | + a = r - 1 |
| 41 | + b = r |
| 42 | + elif l > a: |
| 43 | + # only b is in [l, r]: pick r |
| 44 | + ans += 1 |
| 45 | + a = b |
| 46 | + b = r |
| 47 | + else: |
| 48 | + # both a and b are in [l, r]: nothing to do |
| 49 | + continue |
| 50 | + |
| 51 | + return ans |
| 52 | +``` |
| 53 | +- Notes: |
| 54 | + - Greedy idea: always choose points as far to the right as possible (r-1 and r) so they maximize reuse by later intervals. |
| 55 | + - Sorting by (end asc, start desc) ensures correctness when multiple intervals share the same end. |
| 56 | + - Time complexity: O(n log n) due to sorting, where n = number of intervals. |
| 57 | + - Space complexity: O(1) extra (besides input and sorting). |
0 commit comments