Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions leetcode/701-800/0757.Set-Intersection-Size-At-Least-Two/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
# [757.Set Intersection Size At Least Two][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
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.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Set Intersection Size At Least Two
```go
```
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.
```

**Example 3:**

```
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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func Solution(intervals [][]int) int {
n := len(intervals)
sort.Slice(intervals, func(i, j int) bool {
a, b := intervals[i], intervals[j]
if a[0] == b[0] {
return a[1] > b[1]
}
return a[0] < b[0]
})

todo := make([]int, n)
for i := 0; i < n; i++ {
todo[i] = 2
}

ans := 0
t := n
for t--; t >= 0; t-- {
s := intervals[t][0]
m := todo[t]
for p := s; p < s+m; p++ {
for i := 0; i <= t; i++ {
if todo[i] > 0 && p <= intervals[i][1] {
todo[i]--
}
}
ans++
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{1, 3}, {3, 7}, {8, 9}}, 5},
{"TestCase2", [][]int{{1, 3}, {1, 4}, {2, 5}, {3, 5}}, 3},
{"TestCase3", [][]int{{1, 2}, {2, 3}, {2, 4}, {4, 5}}, 5},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading