Skip to content

Commit 93ba8e9

Browse files
committed
feat: add solutions to lc problem: No.1272
No.1272.Remove Interval
1 parent a9d74d7 commit 93ba8e9

File tree

6 files changed

+241
-2
lines changed

6 files changed

+241
-2
lines changed

solution/1200-1299/1272.Remove Interval/README.md

+90-1
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,111 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:分类讨论**
54+
55+
我们记要删除的区间为 $[x, y)$,遍历区间列表,对于每个区间 $[a, b)$,有以下三种情况:
56+
57+
- $a \geq y$ 或 $b \leq x$,表示该区间与要删除的区间没有交集,直接将该区间加入答案;
58+
- $a \lt x$,$b \gt y$,表示该区间与要删除的区间有交集,将该区间分成两个区间加入答案;
59+
- $a \geq x$,$b \leq y$,表示该区间被要删除的区间完全覆盖,不加入答案。
60+
61+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为区间列表的长度。
62+
5363
<!-- tabs:start -->
5464

5565
### **Python3**
5666

5767
<!-- 这里可写当前语言的特殊实现逻辑 -->
5868

5969
```python
60-
70+
class Solution:
71+
def removeInterval(self, intervals: List[List[int]], toBeRemoved: List[int]) -> List[List[int]]:
72+
x, y = toBeRemoved
73+
ans = []
74+
for a, b in intervals:
75+
if a >= y or b <= x:
76+
ans.append([a, b])
77+
else:
78+
if a < x:
79+
ans.append([a, x])
80+
if b > y:
81+
ans.append([y, b])
82+
return ans
6183
```
6284

6385
### **Java**
6486

6587
<!-- 这里可写当前语言的特殊实现逻辑 -->
6688

6789
```java
90+
class Solution {
91+
public List<List<Integer>> removeInterval(int[][] intervals, int[] toBeRemoved) {
92+
int x = toBeRemoved[0], y = toBeRemoved[1];
93+
List<List<Integer>> ans = new ArrayList<>();
94+
for (var e : intervals) {
95+
int a = e[0], b = e[1];
96+
if (a >= y || b <= x) {
97+
ans.add(Arrays.asList(a, b));
98+
} else {
99+
if (a < x) {
100+
ans.add(Arrays.asList(a, x));
101+
}
102+
if (b > y) {
103+
ans.add(Arrays.asList(y, b));
104+
}
105+
}
106+
}
107+
return ans;
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
vector<vector<int>> removeInterval(vector<vector<int>>& intervals, vector<int>& toBeRemoved) {
118+
int x = toBeRemoved[0], y = toBeRemoved[1];
119+
vector<vector<int>> ans;
120+
for (auto& e : intervals) {
121+
int a = e[0], b = e[1];
122+
if (a >= y || b <= x) {
123+
ans.push_back(e);
124+
} else {
125+
if (a < x) {
126+
ans.push_back({a, x});
127+
}
128+
if (b > y) {
129+
ans.push_back({y, b});
130+
}
131+
}
132+
}
133+
return ans;
134+
}
135+
};
136+
```
68137
138+
### **Go**
139+
140+
```go
141+
func removeInterval(intervals [][]int, toBeRemoved []int) (ans [][]int) {
142+
x, y := toBeRemoved[0], toBeRemoved[1]
143+
for _, e := range intervals {
144+
a, b := e[0], e[1]
145+
if a >= y || b <= x {
146+
ans = append(ans, e)
147+
} else {
148+
if a < x {
149+
ans = append(ans, []int{a, x})
150+
}
151+
if b > y {
152+
ans = append(ans, []int{y, b})
153+
}
154+
}
155+
}
156+
return
157+
}
69158
```
70159

71160
### **...**

solution/1200-1299/1272.Remove Interval/README_EN.md

+80-1
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,92 @@
4747
### **Python3**
4848

4949
```python
50-
50+
class Solution:
51+
def removeInterval(self, intervals: List[List[int]], toBeRemoved: List[int]) -> List[List[int]]:
52+
x, y = toBeRemoved
53+
ans = []
54+
for a, b in intervals:
55+
if a >= y or b <= x:
56+
ans.append([a, b])
57+
else:
58+
if a < x:
59+
ans.append([a, x])
60+
if b > y:
61+
ans.append([y, b])
62+
return ans
5163
```
5264

5365
### **Java**
5466

5567
```java
68+
class Solution {
69+
public List<List<Integer>> removeInterval(int[][] intervals, int[] toBeRemoved) {
70+
int x = toBeRemoved[0], y = toBeRemoved[1];
71+
List<List<Integer>> ans = new ArrayList<>();
72+
for (var e : intervals) {
73+
int a = e[0], b = e[1];
74+
if (a >= y || b <= x) {
75+
ans.add(Arrays.asList(a, b));
76+
} else {
77+
if (a < x) {
78+
ans.add(Arrays.asList(a, x));
79+
}
80+
if (b > y) {
81+
ans.add(Arrays.asList(y, b));
82+
}
83+
}
84+
}
85+
return ans;
86+
}
87+
}
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
vector<vector<int>> removeInterval(vector<vector<int>>& intervals, vector<int>& toBeRemoved) {
96+
int x = toBeRemoved[0], y = toBeRemoved[1];
97+
vector<vector<int>> ans;
98+
for (auto& e : intervals) {
99+
int a = e[0], b = e[1];
100+
if (a >= y || b <= x) {
101+
ans.push_back(e);
102+
} else {
103+
if (a < x) {
104+
ans.push_back({a, x});
105+
}
106+
if (b > y) {
107+
ans.push_back({y, b});
108+
}
109+
}
110+
}
111+
return ans;
112+
}
113+
};
114+
```
56115
116+
### **Go**
117+
118+
```go
119+
func removeInterval(intervals [][]int, toBeRemoved []int) (ans [][]int) {
120+
x, y := toBeRemoved[0], toBeRemoved[1]
121+
for _, e := range intervals {
122+
a, b := e[0], e[1]
123+
if a >= y || b <= x {
124+
ans = append(ans, e)
125+
} else {
126+
if a < x {
127+
ans = append(ans, []int{a, x})
128+
}
129+
if b > y {
130+
ans = append(ans, []int{y, b})
131+
}
132+
}
133+
}
134+
return
135+
}
57136
```
58137

59138
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> removeInterval(vector<vector<int>>& intervals, vector<int>& toBeRemoved) {
4+
int x = toBeRemoved[0], y = toBeRemoved[1];
5+
vector<vector<int>> ans;
6+
for (auto& e : intervals) {
7+
int a = e[0], b = e[1];
8+
if (a >= y || b <= x) {
9+
ans.push_back(e);
10+
} else {
11+
if (a < x) {
12+
ans.push_back({a, x});
13+
}
14+
if (b > y) {
15+
ans.push_back({y, b});
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func removeInterval(intervals [][]int, toBeRemoved []int) (ans [][]int) {
2+
x, y := toBeRemoved[0], toBeRemoved[1]
3+
for _, e := range intervals {
4+
a, b := e[0], e[1]
5+
if a >= y || b <= x {
6+
ans = append(ans, e)
7+
} else {
8+
if a < x {
9+
ans = append(ans, []int{a, x})
10+
}
11+
if b > y {
12+
ans = append(ans, []int{y, b})
13+
}
14+
}
15+
}
16+
return
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public List<List<Integer>> removeInterval(int[][] intervals, int[] toBeRemoved) {
3+
int x = toBeRemoved[0], y = toBeRemoved[1];
4+
List<List<Integer>> ans = new ArrayList<>();
5+
for (var e : intervals) {
6+
int a = e[0], b = e[1];
7+
if (a >= y || b <= x) {
8+
ans.add(Arrays.asList(a, b));
9+
} else {
10+
if (a < x) {
11+
ans.add(Arrays.asList(a, x));
12+
}
13+
if (b > y) {
14+
ans.add(Arrays.asList(y, b));
15+
}
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def removeInterval(self, intervals: List[List[int]], toBeRemoved: List[int]) -> List[List[int]]:
3+
x, y = toBeRemoved
4+
ans = []
5+
for a, b in intervals:
6+
if a >= y or b <= x:
7+
ans.append([a, b])
8+
else:
9+
if a < x:
10+
ans.append([a, x])
11+
if b > y:
12+
ans.append([y, b])
13+
return ans

0 commit comments

Comments
 (0)