diff --git a/leetcode/1201-1300/1288.Remove-Covered-Intervals/README.md b/leetcode/1201-1300/1288.Remove-Covered-Intervals/README.md index 48bdd5d1e..3a46d3976 100644 --- a/leetcode/1201-1300/1288.Remove-Covered-Intervals/README.md +++ b/leetcode/1201-1300/1288.Remove-Covered-Intervals/README.md @@ -1,28 +1,26 @@ # [1288.Remove Covered Intervals][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 +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. + +The interval `[a, b)` is covered by the interval `[c, d)` if and only if `c <= a` and `b <= d`. + +Return the number of remaining intervals. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: intervals = [[1,4],[3,6],[2,8]] +Output: 2 +Explanation: Interval [3,6] is covered by [2,8], therefore it is removed. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Remove Covered Intervals -```go ``` - +Input: intervals = [[1,4],[2,3]] +Output: 1 +``` ## 结语 diff --git a/leetcode/1201-1300/1288.Remove-Covered-Intervals/Solution.go b/leetcode/1201-1300/1288.Remove-Covered-Intervals/Solution.go index d115ccf5e..c7882b26f 100644 --- a/leetcode/1201-1300/1288.Remove-Covered-Intervals/Solution.go +++ b/leetcode/1201-1300/1288.Remove-Covered-Intervals/Solution.go @@ -1,5 +1,26 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(intervals [][]int) int { + l := len(intervals) + sort.Slice(intervals, func(i, j int) bool { + if intervals[i][0] == intervals[j][0] { + return intervals[i][1] > intervals[j][1] + } + return intervals[i][0] < intervals[j][0] + }) + ans := 1 + maxEnd := intervals[0][1] + for i := 1; i < l; i++ { + if intervals[i][0] == intervals[i-1][0] { + continue + } + if intervals[i][1] <= maxEnd { + continue + } + maxEnd = intervals[i][1] + ans++ + } + return ans } diff --git a/leetcode/1201-1300/1288.Remove-Covered-Intervals/Solution_test.go b/leetcode/1201-1300/1288.Remove-Covered-Intervals/Solution_test.go index 14ff50eb4..c22752a7d 100644 --- a/leetcode/1201-1300/1288.Remove-Covered-Intervals/Solution_test.go +++ b/leetcode/1201-1300/1288.Remove-Covered-Intervals/Solution_test.go @@ -10,12 +10,13 @@ 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, 4}, {3, 6}, {2, 8}, + }, 2}, + {"TestCase2", [][]int{{1, 4}, {2, 3}}, 1}, } // 开始测试 @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }