Skip to content

Commit 50e10ca

Browse files
committed
feat: add solutions to lc problems: No.0435,0986
1 parent 7b37b03 commit 50e10ca

File tree

9 files changed

+342
-2
lines changed

9 files changed

+342
-2
lines changed

solution/0400-0499/0435.Non-overlapping Intervals/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,35 @@ class Solution {
103103
}
104104
```
105105

106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
int eraseOverlapIntervals(vector<vector<int>> &intervals) {
112+
if (intervals.empty())
113+
{
114+
return 0;
115+
}
116+
sort(intervals.begin(), intervals.end(), [](const auto &a, const auto &b)
117+
{ return a[1] < b[1]; });
118+
int ed = intervals[0][1], cnt = 0;
119+
for (int i = 1; i < intervals.size(); ++i)
120+
{
121+
if (ed <= intervals[i][0])
122+
{
123+
ed = intervals[i][1];
124+
}
125+
else
126+
{
127+
++cnt;
128+
}
129+
}
130+
return cnt;
131+
}
132+
};
133+
```
134+
106135
### **Go**
107136
108137
```go

solution/0400-0499/0435.Non-overlapping Intervals/README_EN.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,35 @@ class Solution {
8686
}
8787
```
8888

89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
int eraseOverlapIntervals(vector<vector<int>> &intervals) {
95+
if (intervals.empty())
96+
{
97+
return 0;
98+
}
99+
sort(intervals.begin(), intervals.end(), [](const auto &a, const auto &b)
100+
{ return a[1] < b[1]; });
101+
int ed = intervals[0][1], cnt = 0;
102+
for (int i = 1; i < intervals.size(); ++i)
103+
{
104+
if (ed <= intervals[i][0])
105+
{
106+
ed = intervals[i][1];
107+
}
108+
else
109+
{
110+
++cnt;
111+
}
112+
}
113+
return cnt;
114+
}
115+
};
116+
```
117+
89118
### **Go**
90119
91120
```go
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int eraseOverlapIntervals(vector<vector<int>> &intervals) {
4+
if (intervals.empty())
5+
{
6+
return 0;
7+
}
8+
sort(intervals.begin(), intervals.end(), [](const auto &a, const auto &b)
9+
{ return a[1] < b[1]; });
10+
int ed = intervals[0][1], cnt = 0;
11+
for (int i = 1; i < intervals.size(); ++i)
12+
{
13+
if (ed <= intervals[i][0])
14+
{
15+
ed = intervals[i][1];
16+
}
17+
else
18+
{
19+
++cnt;
20+
}
21+
}
22+
return cnt;
23+
}
24+
};

solution/0900-0999/0986.Interval List Intersections/README.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,112 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
双指针实现区间合并。
66+
6567
<!-- tabs:start -->
6668

6769
### **Python3**
6870

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

7173
```python
72-
74+
class Solution:
75+
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
76+
i = j = 0
77+
res = []
78+
while i < len(firstList) and j < len(secondList):
79+
l, r = max(firstList[i][0], secondList[j][0]), min(
80+
firstList[i][1], secondList[j][1])
81+
if l <= r:
82+
res.append([l, r])
83+
if firstList[i][1] < secondList[j][1]:
84+
i += 1
85+
else:
86+
j += 1
87+
return res
7388
```
7489

7590
### **Java**
7691

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

7994
```java
95+
class Solution {
96+
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
97+
List<int[]> res = new ArrayList<>();
98+
for (int i = 0, j = 0; i < firstList.length && j < secondList.length;) {
99+
int l = Math.max(firstList[i][0], secondList[j][0]);
100+
int r = Math.min(firstList[i][1], secondList[j][1]);
101+
if (l <= r) {
102+
res.add(new int[]{l, r});
103+
}
104+
if (firstList[i][1] < secondList[j][1]) {
105+
++i;
106+
} else {
107+
++j;
108+
}
109+
}
110+
return res.toArray(new int[res.size()][]);
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
vector<vector<int>> intervalIntersection(vector<vector<int>> &firstList, vector<vector<int>> &secondList) {
121+
vector<vector<int>> res;
122+
for (int i = 0, j = 0; i < firstList.size() && j < secondList.size();)
123+
{
124+
int l = max(firstList[i][0], secondList[j][0]);
125+
int r = min(firstList[i][1], secondList[j][1]);
126+
if (l <= r)
127+
res.push_back({l, r});
128+
if (firstList[i][1] < secondList[j][1])
129+
++i;
130+
else
131+
++j;
132+
}
133+
return res;
134+
}
135+
};
136+
```
80137
138+
### **Go**
139+
140+
```go
141+
func intervalIntersection(firstList [][]int, secondList [][]int) [][]int {
142+
i, j := 0, 0
143+
var res [][]int
144+
for i < len(firstList) && j < len(secondList) {
145+
l, r := max(firstList[i][0], secondList[j][0]), min(firstList[i][1], secondList[j][1])
146+
if l <= r {
147+
res = append(res, []int{l, r})
148+
}
149+
if firstList[i][1] < secondList[j][1] {
150+
i++
151+
} else {
152+
j++
153+
}
154+
}
155+
return res
156+
}
157+
158+
func max(a, b int) int {
159+
if a > b {
160+
return a
161+
}
162+
return b
163+
}
164+
165+
func min(a, b int) int {
166+
if a < b {
167+
return a
168+
}
169+
return b
170+
}
81171
```
82172

83173
### **...**

solution/0900-0999/0986.Interval List Intersections/README_EN.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,101 @@
6161
### **Python3**
6262

6363
```python
64-
64+
class Solution:
65+
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
66+
i = j = 0
67+
res = []
68+
while i < len(firstList) and j < len(secondList):
69+
l, r = max(firstList[i][0], secondList[j][0]), min(
70+
firstList[i][1], secondList[j][1])
71+
if l <= r:
72+
res.append([l, r])
73+
if firstList[i][1] < secondList[j][1]:
74+
i += 1
75+
else:
76+
j += 1
77+
return res
6578
```
6679

6780
### **Java**
6881

6982
```java
83+
class Solution {
84+
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
85+
List<int[]> res = new ArrayList<>();
86+
for (int i = 0, j = 0; i < firstList.length && j < secondList.length;) {
87+
int l = Math.max(firstList[i][0], secondList[j][0]);
88+
int r = Math.min(firstList[i][1], secondList[j][1]);
89+
if (l <= r) {
90+
res.add(new int[]{l, r});
91+
}
92+
if (firstList[i][1] < secondList[j][1]) {
93+
++i;
94+
} else {
95+
++j;
96+
}
97+
}
98+
return res.toArray(new int[res.size()][]);
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
vector<vector<int>> intervalIntersection(vector<vector<int>> &firstList, vector<vector<int>> &secondList) {
109+
vector<vector<int>> res;
110+
for (int i = 0, j = 0; i < firstList.size() && j < secondList.size();)
111+
{
112+
int l = max(firstList[i][0], secondList[j][0]);
113+
int r = min(firstList[i][1], secondList[j][1]);
114+
if (l <= r)
115+
res.push_back({l, r});
116+
if (firstList[i][1] < secondList[j][1])
117+
++i;
118+
else
119+
++j;
120+
}
121+
return res;
122+
}
123+
};
124+
```
70125
126+
### **Go**
127+
128+
```go
129+
func intervalIntersection(firstList [][]int, secondList [][]int) [][]int {
130+
i, j := 0, 0
131+
var res [][]int
132+
for i < len(firstList) && j < len(secondList) {
133+
l, r := max(firstList[i][0], secondList[j][0]), min(firstList[i][1], secondList[j][1])
134+
if l <= r {
135+
res = append(res, []int{l, r})
136+
}
137+
if firstList[i][1] < secondList[j][1] {
138+
i++
139+
} else {
140+
j++
141+
}
142+
}
143+
return res
144+
}
145+
146+
func max(a, b int) int {
147+
if a > b {
148+
return a
149+
}
150+
return b
151+
}
152+
153+
func min(a, b int) int {
154+
if a < b {
155+
return a
156+
}
157+
return b
158+
}
71159
```
72160

73161
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> intervalIntersection(vector<vector<int>> &firstList, vector<vector<int>> &secondList) {
4+
vector<vector<int>> res;
5+
for (int i = 0, j = 0; i < firstList.size() && j < secondList.size();)
6+
{
7+
int l = max(firstList[i][0], secondList[j][0]);
8+
int r = min(firstList[i][1], secondList[j][1]);
9+
if (l <= r)
10+
res.push_back({l, r});
11+
if (firstList[i][1] < secondList[j][1])
12+
++i;
13+
else
14+
++j;
15+
}
16+
return res;
17+
}
18+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
func intervalIntersection(firstList [][]int, secondList [][]int) [][]int {
2+
i, j := 0, 0
3+
var res [][]int
4+
for i < len(firstList) && j < len(secondList) {
5+
l, r := max(firstList[i][0], secondList[j][0]), min(firstList[i][1], secondList[j][1])
6+
if l <= r {
7+
res = append(res, []int{l, r})
8+
}
9+
if firstList[i][1] < secondList[j][1] {
10+
i++
11+
} else {
12+
j++
13+
}
14+
}
15+
return res
16+
}
17+
18+
func max(a, b int) int {
19+
if a > b {
20+
return a
21+
}
22+
return b
23+
}
24+
25+
func min(a, b int) int {
26+
if a < b {
27+
return a
28+
}
29+
return b
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
3+
List<int[]> res = new ArrayList<>();
4+
for (int i = 0, j = 0; i < firstList.length && j < secondList.length;) {
5+
int l = Math.max(firstList[i][0], secondList[j][0]);
6+
int r = Math.min(firstList[i][1], secondList[j][1]);
7+
if (l <= r) {
8+
res.add(new int[]{l, r});
9+
}
10+
if (firstList[i][1] < secondList[j][1]) {
11+
++i;
12+
} else {
13+
++j;
14+
}
15+
}
16+
return res.toArray(new int[res.size()][]);
17+
}
18+
}

0 commit comments

Comments
 (0)