Skip to content

Commit 602de0e

Browse files
committed
feat: add solutions to lc problem: No.0774
No.0774.Minimize Max Distance to Gas Station
1 parent bfe5db1 commit 602de0e

File tree

7 files changed

+253
-3
lines changed

7 files changed

+253
-3
lines changed

solution/0700-0799/0774.Minimize Max Distance to Gas Station/README.md

+91-1
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,112 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:二分查找(浮点数二分)**
48+
49+
我们二分枚举相邻两个加油站间的距离,找到最小的距离,使得加油站的数量不超过 $k$。
50+
51+
时间复杂度 $O(n\log M)$。其中 $n$ 为加油站的数量;而 $M$ 为答案的范围,即 $10^8$ 除以答案的精度 $10^{-6}$。
52+
4753
<!-- tabs:start -->
4854

4955
### **Python3**
5056

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

5359
```python
54-
60+
class Solution:
61+
def minmaxGasDist(self, stations: List[int], k: int) -> float:
62+
def check(x):
63+
return sum(int((b - a) / x) for a, b in pairwise(stations)) <= k
64+
65+
left, right = 0, 1e8
66+
while right - left > 1e-6:
67+
mid = (left + right) / 2
68+
if check(mid):
69+
right = mid
70+
else:
71+
left = mid
72+
return left
5573
```
5674

5775
### **Java**
5876

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

6179
```java
80+
class Solution {
81+
public double minmaxGasDist(int[] stations, int k) {
82+
double left = 0, right = 1e8;
83+
while (right - left > 1e-6) {
84+
double mid = (left + right) / 2.0;
85+
if (check(mid, stations, k)) {
86+
right = mid;
87+
} else {
88+
left = mid;
89+
}
90+
}
91+
return left;
92+
}
93+
94+
private boolean check(double x, int[] stations, int k) {
95+
int s = 0;
96+
for (int i = 0; i < stations.length - 1; ++i) {
97+
s += (int) ((stations[i + 1] - stations[i]) / x);
98+
}
99+
return s <= k;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
double minmaxGasDist(vector<int>& stations, int k) {
110+
double left = 0, right = 1e8;
111+
auto check = [&](double x) {
112+
int s = 0;
113+
for (int i = 0; i < stations.size() - 1; ++i) {
114+
s += (int) ((stations[i + 1] - stations[i]) / x);
115+
}
116+
return s <= k;
117+
};
118+
while (right - left > 1e-6) {
119+
double mid = (left + right) / 2.0;
120+
if (check(mid)) {
121+
right = mid;
122+
} else {
123+
left = mid;
124+
}
125+
}
126+
return left;
127+
}
128+
};
129+
```
62130
131+
### **Go**
132+
133+
```go
134+
func minmaxGasDist(stations []int, k int) float64 {
135+
check := func(x float64) bool {
136+
s := 0
137+
for i, v := range stations[:len(stations)-1] {
138+
s += int(float64(stations[i+1]-v) / x)
139+
}
140+
return s <= k
141+
}
142+
var left, right float64 = 0, 1e8
143+
for right-left > 1e-6 {
144+
mid := (left + right) / 2.0
145+
if check(mid) {
146+
right = mid
147+
} else {
148+
left = mid
149+
}
150+
}
151+
return left
152+
}
63153
```
64154

65155
### **...**

solution/0700-0799/0774.Minimize Max Distance to Gas Station/README_EN.md

+85-1
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,97 @@
3737
### **Python3**
3838

3939
```python
40-
40+
class Solution:
41+
def minmaxGasDist(self, stations: List[int], k: int) -> float:
42+
def check(x):
43+
return sum(int((b - a) / x) for a, b in pairwise(stations)) <= k
44+
45+
left, right = 0, 1e8
46+
while right - left > 1e-6:
47+
mid = (left + right) / 2
48+
if check(mid):
49+
right = mid
50+
else:
51+
left = mid
52+
return left
4153
```
4254

4355
### **Java**
4456

4557
```java
58+
class Solution {
59+
public double minmaxGasDist(int[] stations, int k) {
60+
double left = 0, right = 1e8;
61+
while (right - left > 1e-6) {
62+
double mid = (left + right) / 2.0;
63+
if (check(mid, stations, k)) {
64+
right = mid;
65+
} else {
66+
left = mid;
67+
}
68+
}
69+
return left;
70+
}
71+
72+
private boolean check(double x, int[] stations, int k) {
73+
int s = 0;
74+
for (int i = 0; i < stations.length - 1; ++i) {
75+
s += (int) ((stations[i + 1] - stations[i]) / x);
76+
}
77+
return s <= k;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
double minmaxGasDist(vector<int>& stations, int k) {
88+
double left = 0, right = 1e8;
89+
auto check = [&](double x) {
90+
int s = 0;
91+
for (int i = 0; i < stations.size() - 1; ++i) {
92+
s += (int) ((stations[i + 1] - stations[i]) / x);
93+
}
94+
return s <= k;
95+
};
96+
while (right - left > 1e-6) {
97+
double mid = (left + right) / 2.0;
98+
if (check(mid)) {
99+
right = mid;
100+
} else {
101+
left = mid;
102+
}
103+
}
104+
return left;
105+
}
106+
};
107+
```
46108
109+
### **Go**
110+
111+
```go
112+
func minmaxGasDist(stations []int, k int) float64 {
113+
check := func(x float64) bool {
114+
s := 0
115+
for i, v := range stations[:len(stations)-1] {
116+
s += int(float64(stations[i+1]-v) / x)
117+
}
118+
return s <= k
119+
}
120+
var left, right float64 = 0, 1e8
121+
for right-left > 1e-6 {
122+
mid := (left + right) / 2.0
123+
if check(mid) {
124+
right = mid
125+
} else {
126+
left = mid
127+
}
128+
}
129+
return left
130+
}
47131
```
48132

49133
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
double minmaxGasDist(vector<int>& stations, int k) {
4+
double left = 0, right = 1e8;
5+
auto check = [&](double x) {
6+
int s = 0;
7+
for (int i = 0; i < stations.size() - 1; ++i) {
8+
s += (int) ((stations[i + 1] - stations[i]) / x);
9+
}
10+
return s <= k;
11+
};
12+
while (right - left > 1e-6) {
13+
double mid = (left + right) / 2.0;
14+
if (check(mid)) {
15+
right = mid;
16+
} else {
17+
left = mid;
18+
}
19+
}
20+
return left;
21+
}
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func minmaxGasDist(stations []int, k int) float64 {
2+
check := func(x float64) bool {
3+
s := 0
4+
for i, v := range stations[:len(stations)-1] {
5+
s += int(float64(stations[i+1]-v) / x)
6+
}
7+
return s <= k
8+
}
9+
var left, right float64 = 0, 1e8
10+
for right-left > 1e-6 {
11+
mid := (left + right) / 2.0
12+
if check(mid) {
13+
right = mid
14+
} else {
15+
left = mid
16+
}
17+
}
18+
return left
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public double minmaxGasDist(int[] stations, int k) {
3+
double left = 0, right = 1e8;
4+
while (right - left > 1e-6) {
5+
double mid = (left + right) / 2.0;
6+
if (check(mid, stations, k)) {
7+
right = mid;
8+
} else {
9+
left = mid;
10+
}
11+
}
12+
return left;
13+
}
14+
15+
private boolean check(double x, int[] stations, int k) {
16+
int s = 0;
17+
for (int i = 0; i < stations.length - 1; ++i) {
18+
s += (int) ((stations[i + 1] - stations[i]) / x);
19+
}
20+
return s <= k;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def minmaxGasDist(self, stations: List[int], k: int) -> float:
3+
def check(x):
4+
return sum(int((b - a) / x) for a, b in pairwise(stations)) <= k
5+
6+
left, right = 0, 1e8
7+
while right - left > 1e-6:
8+
mid = (left + right) / 2
9+
if check(mid):
10+
right = mid
11+
else:
12+
left = mid
13+
return left

solution/0700-0799/0775.Global and Local Inversions/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
根据题意,我们可以发现,一个数组中的局部倒置一定是全局倒置,但是全局倒置不一定是局部倒置。也就是说,全局倒置的数量一定大于等于局部倒置的数量。
6262

63-
因此,我们枚举每个数 `nums[i]`,其中 $ 2 \leq i \leq n - 1$,维护前缀数组 $nums[0,..i-2]$ 中的最大值,记为 $mx$。如果存在 $mx$ 大于 $nums[i]$,则说明全局倒置的数量大于局部倒置的数量,返回 `false` 即可。
63+
因此,我们枚举每个数 `nums[i]`,其中 $2 \leq i \leq n - 1$,维护前缀数组 $nums[0,..i-2]$ 中的最大值,记为 $mx$。如果存在 $mx$ 大于 $nums[i]$,则说明全局倒置的数量大于局部倒置的数量,返回 `false` 即可。
6464

6565
遍历结束后,返回 `true`
6666

0 commit comments

Comments
 (0)