Skip to content

Commit 2297fda

Browse files
committed
feat: add solutions to lc problem: No.0475
No.0475.Heaters
1 parent a28ba6d commit 2297fda

File tree

5 files changed

+328
-4
lines changed

5 files changed

+328
-4
lines changed

solution/0400-0499/0475.Heaters/README.md

+119-2
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,144 @@
4848
<li><code>1 <= houses[i], heaters[i] <= 10<sup>9</sup></code></li>
4949
</ul>
5050

51-
5251
## 解法
5352

5453
<!-- 这里可写通用的实现逻辑 -->
5554

55+
排序 + 二分查找 + 双指针。
56+
5657
<!-- tabs:start -->
5758

5859
### **Python3**
5960

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

6263
```python
63-
64+
class Solution:
65+
def findRadius(self, houses: List[int], heaters: List[int]) -> int:
66+
houses.sort()
67+
heaters.sort()
68+
69+
def check(r):
70+
m, n = len(houses), len(heaters)
71+
i = j = 0
72+
while i < m:
73+
if j >= n:
74+
return False
75+
mi = heaters[j] - r
76+
mx = heaters[j] + r
77+
if houses[i] < mi:
78+
return False
79+
if houses[i] > mx:
80+
j += 1
81+
else:
82+
i += 1
83+
return True
84+
85+
left, right = 0, int(1e9)
86+
while left < right:
87+
mid = (left + right) >> 1
88+
if check(mid):
89+
right = mid
90+
else:
91+
left = mid + 1
92+
return left
6493
```
6594

6695
### **Java**
6796

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

7099
```java
100+
class Solution {
101+
public int findRadius(int[] houses, int[] heaters) {
102+
Arrays.sort(heaters);
103+
int res = 0;
104+
for (int x : houses) {
105+
int i = Arrays.binarySearch(heaters, x);
106+
if (i < 0) {
107+
i = ~i;
108+
}
109+
int dis1 = i > 0 ? x - heaters[i - 1] : Integer.MAX_VALUE;
110+
int dis2 = i < heaters.length ? heaters[i] - x : Integer.MAX_VALUE;
111+
res = Math.max(res, Math.min(dis1, dis2));
112+
}
113+
return res;
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
int findRadius(vector<int>& houses, vector<int>& heaters) {
124+
sort(houses.begin(), houses.end());
125+
sort(heaters.begin(), heaters.end());
126+
int left = 0, right = 1e9;
127+
while (left < right) {
128+
int mid = left + right >> 1;
129+
if (check(houses, heaters, mid)) right = mid;
130+
else left = mid + 1;
131+
}
132+
return left;
133+
}
134+
135+
bool check(vector<int>& houses, vector<int>& heaters, int r) {
136+
int m = houses.size(), n = heaters.size();
137+
int i = 0, j = 0;
138+
while (i < m)
139+
{
140+
if (j >= n) return false;
141+
int mi = heaters[j] - r;
142+
int mx = heaters[j] + r;
143+
if (houses[i] < mi) return false;
144+
if (houses[i] > mx) ++j;
145+
else ++i;
146+
}
147+
return true;
148+
}
149+
};
150+
```
71151

152+
### **Go**
153+
154+
```go
155+
func findRadius(houses []int, heaters []int) int {
156+
sort.Ints(houses)
157+
sort.Ints(heaters)
158+
m, n := len(houses), len(heaters)
159+
160+
check := func(r int) bool {
161+
var i, j int
162+
for i < m {
163+
if j >= n {
164+
return false
165+
}
166+
mi, mx := heaters[j]-r, heaters[j]+r
167+
if houses[i] < mi {
168+
return false
169+
}
170+
if houses[i] > mx {
171+
j++
172+
} else {
173+
i++
174+
}
175+
}
176+
return true
177+
}
178+
left, right := 0, int(1e9)
179+
for left < right {
180+
mid := (left + right) >> 1
181+
if check(mid) {
182+
right = mid
183+
} else {
184+
left = mid + 1
185+
}
186+
}
187+
return left
188+
}
72189
```
73190

74191
### **...**

solution/0400-0499/0475.Heaters/README_EN.md

+117-2
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,136 @@
4444
<li><code>1 &lt;= houses[i], heaters[i] &lt;= 10<sup>9</sup></code></li>
4545
</ul>
4646

47-
4847
## Solutions
4948

5049
<!-- tabs:start -->
5150

5251
### **Python3**
5352

5453
```python
55-
54+
class Solution:
55+
def findRadius(self, houses: List[int], heaters: List[int]) -> int:
56+
houses.sort()
57+
heaters.sort()
58+
59+
def check(r):
60+
m, n = len(houses), len(heaters)
61+
i = j = 0
62+
while i < m:
63+
if j >= n:
64+
return False
65+
mi = heaters[j] - r
66+
mx = heaters[j] + r
67+
if houses[i] < mi:
68+
return False
69+
if houses[i] > mx:
70+
j += 1
71+
else:
72+
i += 1
73+
return True
74+
75+
left, right = 0, int(1e9)
76+
while left < right:
77+
mid = (left + right) >> 1
78+
if check(mid):
79+
right = mid
80+
else:
81+
left = mid + 1
82+
return left
5683
```
5784

5885
### **Java**
5986

6087
```java
88+
class Solution {
89+
public int findRadius(int[] houses, int[] heaters) {
90+
Arrays.sort(heaters);
91+
int res = 0;
92+
for (int x : houses) {
93+
int i = Arrays.binarySearch(heaters, x);
94+
if (i < 0) {
95+
i = ~i;
96+
}
97+
int dis1 = i > 0 ? x - heaters[i - 1] : Integer.MAX_VALUE;
98+
int dis2 = i < heaters.length ? heaters[i] - x : Integer.MAX_VALUE;
99+
res = Math.max(res, Math.min(dis1, dis2));
100+
}
101+
return res;
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
int findRadius(vector<int>& houses, vector<int>& heaters) {
112+
sort(houses.begin(), houses.end());
113+
sort(heaters.begin(), heaters.end());
114+
int left = 0, right = 1e9;
115+
while (left < right) {
116+
int mid = left + right >> 1;
117+
if (check(houses, heaters, mid)) right = mid;
118+
else left = mid + 1;
119+
}
120+
return left;
121+
}
122+
123+
bool check(vector<int>& houses, vector<int>& heaters, int r) {
124+
int m = houses.size(), n = heaters.size();
125+
int i = 0, j = 0;
126+
while (i < m)
127+
{
128+
if (j >= n) return false;
129+
int mi = heaters[j] - r;
130+
int mx = heaters[j] + r;
131+
if (houses[i] < mi) return false;
132+
if (houses[i] > mx) ++j;
133+
else ++i;
134+
}
135+
return true;
136+
}
137+
};
138+
```
61139

140+
### **Go**
141+
142+
```go
143+
func findRadius(houses []int, heaters []int) int {
144+
sort.Ints(houses)
145+
sort.Ints(heaters)
146+
m, n := len(houses), len(heaters)
147+
148+
check := func(r int) bool {
149+
var i, j int
150+
for i < m {
151+
if j >= n {
152+
return false
153+
}
154+
mi, mx := heaters[j]-r, heaters[j]+r
155+
if houses[i] < mi {
156+
return false
157+
}
158+
if houses[i] > mx {
159+
j++
160+
} else {
161+
i++
162+
}
163+
}
164+
return true
165+
}
166+
left, right := 0, int(1e9)
167+
for left < right {
168+
mid := (left + right) >> 1
169+
if check(mid) {
170+
right = mid
171+
} else {
172+
left = mid + 1
173+
}
174+
}
175+
return left
176+
}
62177
```
63178

64179
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int findRadius(vector<int>& houses, vector<int>& heaters) {
4+
sort(houses.begin(), houses.end());
5+
sort(heaters.begin(), heaters.end());
6+
int left = 0, right = 1e9;
7+
while (left < right) {
8+
int mid = left + right >> 1;
9+
if (check(houses, heaters, mid)) right = mid;
10+
else left = mid + 1;
11+
}
12+
return left;
13+
}
14+
15+
bool check(vector<int>& houses, vector<int>& heaters, int r) {
16+
int m = houses.size(), n = heaters.size();
17+
int i = 0, j = 0;
18+
while (i < m)
19+
{
20+
if (j >= n) return false;
21+
int mi = heaters[j] - r;
22+
int mx = heaters[j] + r;
23+
if (houses[i] < mi) return false;
24+
if (houses[i] > mx) ++j;
25+
else ++i;
26+
}
27+
return true;
28+
}
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
func findRadius(houses []int, heaters []int) int {
2+
sort.Ints(houses)
3+
sort.Ints(heaters)
4+
m, n := len(houses), len(heaters)
5+
6+
check := func(r int) bool {
7+
var i, j int
8+
for i < m {
9+
if j >= n {
10+
return false
11+
}
12+
mi, mx := heaters[j]-r, heaters[j]+r
13+
if houses[i] < mi {
14+
return false
15+
}
16+
if houses[i] > mx {
17+
j++
18+
} else {
19+
i++
20+
}
21+
}
22+
return true
23+
}
24+
left, right := 0, int(1e9)
25+
for left < right {
26+
mid := (left + right) >> 1
27+
if check(mid) {
28+
right = mid
29+
} else {
30+
left = mid + 1
31+
}
32+
}
33+
return left
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def findRadius(self, houses: List[int], heaters: List[int]) -> int:
3+
houses.sort()
4+
heaters.sort()
5+
6+
def check(r):
7+
m, n = len(houses), len(heaters)
8+
i = j = 0
9+
while i < m:
10+
if j >= n:
11+
return False
12+
mi = heaters[j] - r
13+
mx = heaters[j] + r
14+
if houses[i] < mi:
15+
return False
16+
if houses[i] > mx:
17+
j += 1
18+
else:
19+
i += 1
20+
return True
21+
22+
left, right = 0, int(1e9)
23+
while left < right:
24+
mid = (left + right) >> 1
25+
if check(mid):
26+
right = mid
27+
else:
28+
left = mid + 1
29+
return left

0 commit comments

Comments
 (0)