You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/0700-0799/0757.Set Intersection Size At Least Two/README_EN.md
+53-1Lines changed: 53 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,7 +71,33 @@ It can be shown that there cannot be any containing array of size 4.
71
71
72
72
<!-- solution:start -->
73
73
74
-
### Solution 1
74
+
### Solution 1: Sorting + Greedy
75
+
76
+
We want to select as few integer points as possible on the number line such that each interval contains at least two points. A classic and effective strategy is to sort intervals by their right endpoints and try to place selected points towards the right side of intervals, so that these points can cover more subsequent intervals.
77
+
78
+
First, sort all intervals by the following rules:
79
+
80
+
1. Sort by right endpoint in ascending order;
81
+
2. If right endpoints are equal, sort by left endpoint in descending order.
82
+
83
+
The reason for this sorting is: intervals with smaller right endpoints have less "operational space" and should be satisfied first; when right endpoints are equal, intervals with larger left endpoints are narrower and should be prioritized.
84
+
85
+
Next, we use two variables $s$ and $e$ to record the **second-to-last point** and **last point** that are common to all currently processed intervals. Initially, $s = e = -1$, indicating that no points have been placed yet.
86
+
87
+
Then we process the sorted intervals $[a, b]$ one by one, and discuss three cases based on their relationship with $\{s, e\}$:
88
+
89
+
1.**If $a \leq s$**:
90
+
The current interval already contains both points $s$ and $e$, so no additional points are needed.
91
+
92
+
2.**If $s < a \leq e$**:
93
+
The current interval only contains one point (i.e., $e$), and needs one more point. To make the new point most helpful for subsequent intervals, we choose the rightmost point $b$ in the interval. Update $\textit{ans} = \textit{ans} + 1$, and set the new two points to $\{e, b\}$.
94
+
95
+
3.**If $a > e$**:
96
+
The current interval does not contain either of the existing two points, so two points need to be added. The optimal choice is to place $\{b - 1, b\}$ at the rightmost side of the interval. Update $\textit{ans} = \textit{ans} + 2$, and set the new two points to $\{b - 1, b\}$.
97
+
98
+
Finally, return the total number of points placed, $\textit{ans}$.
99
+
100
+
The time complexity is $O(n \times \log n)$ and the space complexity is $O(\log n)$, where $n$ is the number of intervals.
75
101
76
102
<!-- tabs:start -->
77
103
@@ -182,6 +208,32 @@ func intersectionSizeTwo(intervals [][]int) int {
182
208
}
183
209
```
184
210
211
+
#### TypeScript
212
+
213
+
```ts
214
+
function intersectionSizeTwo(intervals:number[][]):number {
215
+
intervals.sort((a, b) => (a[1] !==b[1] ?a[1] -b[1] :b[0] -a[0]));
0 commit comments