Skip to content

Commit 48c6d3c

Browse files
committed
Add solution and test-cases for problem 757
1 parent f03226b commit 48c6d3c

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

leetcode/701-800/0757.Set-Intersection-Size-At-Least-Two/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# [757.Set Intersection Size At Least Two][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a 2D integer array `intervals` where `intervals[i] = [starti, endi]` represents all the integers from `starti` to `endi` inclusively.
5+
6+
A **containing set** is an array `nums` where each interval from `intervals` has **at least two** integers in `nums`.
7+
8+
- 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**.
9+
10+
Return the minimum possible size of a containing set.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: intervals = [[1,3],[3,7],[8,9]]
16+
Output: 5
17+
Explanation: let nums = [2, 3, 4, 8, 9].
18+
It can be shown that there cannot be any containing array of size 4.
1319
```
1420

15-
## 题意
16-
> ...
17-
18-
## 题解
21+
**Example 2:**
1922

20-
### 思路1
21-
> ...
22-
Set Intersection Size At Least Two
23-
```go
2423
```
24+
Input: intervals = [[1,3],[1,4],[2,5],[3,5]]
25+
Output: 3
26+
Explanation: let nums = [2, 3, 4].
27+
It can be shown that there cannot be any containing array of size 2.
28+
```
29+
30+
**Example 3:**
2531

32+
```
33+
Input: intervals = [[1,2],[2,3],[2,4],[4,5]]
34+
Output: 5
35+
Explanation: let nums = [1, 2, 3, 4, 5].
36+
It can be shown that there cannot be any containing array of size 4.
37+
```
2638

2739
## 结语
2840

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(intervals [][]int) int {
6+
n := len(intervals)
7+
sort.Slice(intervals, func(i, j int) bool {
8+
a, b := intervals[i], intervals[j]
9+
if a[0] == b[0] {
10+
return a[1] > b[1]
11+
}
12+
return a[0] < b[0]
13+
})
14+
15+
todo := make([]int, n)
16+
for i := 0; i < n; i++ {
17+
todo[i] = 2
18+
}
19+
20+
ans := 0
21+
t := n
22+
for t--; t >= 0; t-- {
23+
s := intervals[t][0]
24+
m := todo[t]
25+
for p := s; p < s+m; p++ {
26+
for i := 0; i <= t; i++ {
27+
if todo[i] > 0 && p <= intervals[i][1] {
28+
todo[i]--
29+
}
30+
}
31+
ans++
32+
}
33+
}
34+
return ans
535
}

leetcode/701-800/0757.Set-Intersection-Size-At-Least-Two/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 3}, {3, 7}, {8, 9}}, 5},
17+
{"TestCase2", [][]int{{1, 3}, {1, 4}, {2, 5}, {3, 5}}, 3},
18+
{"TestCase3", [][]int{{1, 2}, {2, 3}, {2, 4}, {4, 5}}, 5},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)