Skip to content

Commit 9a0109c

Browse files
committed
Add solution and test-cases for problem 1288
1 parent 09a1dcb commit 9a0109c

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

leetcode/1201-1300/1288.Remove-Covered-Intervals/README.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
# [1288.Remove Covered Intervals][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+
Given an array `intervals` where `intervals[i] = [li, ri]` represent the interval `[li, ri)`, remove all intervals that are covered by another interval in the list.
5+
6+
The interval `[a, b)` is covered by the interval `[c, d)` if and only if `c <= a` and `b <= d`.
7+
8+
Return the number of remaining intervals.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: intervals = [[1,4],[3,6],[2,8]]
14+
Output: 2
15+
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
1316
```
1417

15-
## 题意
16-
> ...
18+
**Example 2:**
1719

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Remove Covered Intervals
23-
```go
2420
```
25-
21+
Input: intervals = [[1,4],[2,3]]
22+
Output: 1
23+
```
2624

2725
## 结语
2826

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(intervals [][]int) int {
6+
l := len(intervals)
7+
sort.Slice(intervals, func(i, j int) bool {
8+
if intervals[i][0] == intervals[j][0] {
9+
return intervals[i][1] > intervals[j][1]
10+
}
11+
return intervals[i][0] < intervals[j][0]
12+
})
13+
ans := 1
14+
maxEnd := intervals[0][1]
15+
for i := 1; i < l; i++ {
16+
if intervals[i][0] == intervals[i-1][0] {
17+
continue
18+
}
19+
if intervals[i][1] <= maxEnd {
20+
continue
21+
}
22+
maxEnd = intervals[i][1]
23+
ans++
24+
}
25+
return ans
526
}

leetcode/1201-1300/1288.Remove-Covered-Intervals/Solution_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ 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{
17+
{1, 4}, {3, 6}, {2, 8},
18+
}, 2},
19+
{"TestCase2", [][]int{{1, 4}, {2, 3}}, 1},
1920
}
2021

2122
// 开始测试
@@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)