Skip to content

Commit 89b7ad3

Browse files
committed
feat: add solutions to lc problems: No.1552,1838
1 parent 6066d15 commit 89b7ad3

File tree

9 files changed

+546
-27
lines changed

9 files changed

+546
-27
lines changed

solution/1500-1599/1552.Magnetic Force Between Two Balls/README.md

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,135 @@
4242
<li><code>2 &lt;= m &lt;= position.length</code></li>
4343
</ul>
4444

45-
4645
## 解法
4746

4847
<!-- 这里可写通用的实现逻辑 -->
4948

49+
二分查找。
50+
51+
先排序,然后二分枚举相邻两球之间的间距,只需要统计当前间距下能放下多少个小球,记为 cnt,若 `cnt >= m`,说明此间距符合条件。继续二分查找,最终找到符合条件的最大间距。
52+
5053
<!-- tabs:start -->
5154

5255
### **Python3**
5356

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

5659
```python
57-
60+
class Solution:
61+
def maxDistance(self, position: List[int], m: int) -> int:
62+
position.sort()
63+
64+
def check(f):
65+
pre = position[0]
66+
cnt = 1
67+
for pos in position[1:]:
68+
if pos - pre >= f:
69+
cnt += 1
70+
pre = pos
71+
return cnt >= m
72+
73+
left, right = 1, position[-1]
74+
while left < right:
75+
mid = (left + right + 1) >> 1
76+
if check(mid):
77+
left = mid
78+
else:
79+
right = mid - 1
80+
return left
5881
```
5982

6083
### **Java**
6184

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

6487
```java
88+
class Solution {
89+
public int maxDistance(int[] position, int m) {
90+
Arrays.sort(position);
91+
int left = 1, right = position[position.length - 1];
92+
while (left < right) {
93+
int mid = (left + right + 1) >> 1;
94+
if (check(position, mid, m)) {
95+
left = mid;
96+
} else {
97+
right = mid - 1;
98+
}
99+
}
100+
return left;
101+
}
102+
103+
private boolean check(int[] position, int f, int m) {
104+
int pre = position[0];
105+
int cnt = 1;
106+
for (int i = 1; i < position.length; ++i) {
107+
if (position[i] - pre >= f) {
108+
++cnt;
109+
pre = position[i];
110+
}
111+
}
112+
return cnt >= m;
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int maxDistance(vector<int>& position, int m) {
123+
sort(position.begin(), position.end());
124+
int left = 1, right = position[position.size() - 1];
125+
while (left < right) {
126+
int mid = (left + right + 1) >> 1;
127+
if (check(position, mid, m)) left = mid;
128+
else right = mid - 1;
129+
}
130+
return left;
131+
}
132+
133+
bool check(vector<int>& position, int f, int m) {
134+
int pre = position[0];
135+
int cnt = 1;
136+
for (int i = 1; i < position.size(); ++i) {
137+
if (position[i] - pre >= f) {
138+
++cnt;
139+
pre = position[i];
140+
}
141+
}
142+
return cnt >= m;
143+
}
144+
};
145+
```
65146

147+
### **Go**
148+
149+
```go
150+
func maxDistance(position []int, m int) int {
151+
sort.Ints(position)
152+
left, right := 1, position[len(position)-1]
153+
for left < right {
154+
mid := (left + right + 1) >> 1
155+
if check(position, mid, m) {
156+
left = mid
157+
} else {
158+
right = mid - 1
159+
}
160+
}
161+
return left
162+
}
163+
164+
func check(position []int, f, m int) bool {
165+
pre, cnt := position[0], 1
166+
for i := 1; i < len(position); i++ {
167+
if position[i]-pre >= f {
168+
cnt++
169+
pre = position[i]
170+
}
171+
}
172+
return cnt >= m
173+
}
66174
```
67175

68176
### **...**

solution/1500-1599/1552.Magnetic Force Between Two Balls/README_EN.md

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,125 @@
3838
<li><code>2 &lt;= m &lt;= position.length</code></li>
3939
</ul>
4040

41-
4241
## Solutions
4342

4443
<!-- tabs:start -->
4544

4645
### **Python3**
4746

4847
```python
49-
48+
class Solution:
49+
def maxDistance(self, position: List[int], m: int) -> int:
50+
position.sort()
51+
52+
def check(f):
53+
pre = position[0]
54+
cnt = 1
55+
for pos in position[1:]:
56+
if pos - pre >= f:
57+
cnt += 1
58+
pre = pos
59+
return cnt >= m
60+
61+
left, right = 1, position[-1]
62+
while left < right:
63+
mid = (left + right + 1) >> 1
64+
if check(mid):
65+
left = mid
66+
else:
67+
right = mid - 1
68+
return left
5069
```
5170

5271
### **Java**
5372

5473
```java
74+
class Solution {
75+
public int maxDistance(int[] position, int m) {
76+
Arrays.sort(position);
77+
int left = 1, right = position[position.length - 1];
78+
while (left < right) {
79+
int mid = (left + right + 1) >> 1;
80+
if (check(position, mid, m)) {
81+
left = mid;
82+
} else {
83+
right = mid - 1;
84+
}
85+
}
86+
return left;
87+
}
88+
89+
private boolean check(int[] position, int f, int m) {
90+
int pre = position[0];
91+
int cnt = 1;
92+
for (int i = 1; i < position.length; ++i) {
93+
if (position[i] - pre >= f) {
94+
++cnt;
95+
pre = position[i];
96+
}
97+
}
98+
return cnt >= m;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int maxDistance(vector<int>& position, int m) {
109+
sort(position.begin(), position.end());
110+
int left = 1, right = position[position.size() - 1];
111+
while (left < right) {
112+
int mid = (left + right + 1) >> 1;
113+
if (check(position, mid, m)) left = mid;
114+
else right = mid - 1;
115+
}
116+
return left;
117+
}
118+
119+
bool check(vector<int>& position, int f, int m) {
120+
int pre = position[0];
121+
int cnt = 1;
122+
for (int i = 1; i < position.size(); ++i) {
123+
if (position[i] - pre >= f) {
124+
++cnt;
125+
pre = position[i];
126+
}
127+
}
128+
return cnt >= m;
129+
}
130+
};
131+
```
55132

133+
### **Go**
134+
135+
```go
136+
func maxDistance(position []int, m int) int {
137+
sort.Ints(position)
138+
left, right := 1, position[len(position)-1]
139+
for left < right {
140+
mid := (left + right + 1) >> 1
141+
if check(position, mid, m) {
142+
left = mid
143+
} else {
144+
right = mid - 1
145+
}
146+
}
147+
return left
148+
}
149+
150+
func check(position []int, f, m int) bool {
151+
pre, cnt := position[0], 1
152+
for i := 1; i < len(position); i++ {
153+
if position[i]-pre >= f {
154+
cnt++
155+
pre = position[i]
156+
}
157+
}
158+
return cnt >= m
159+
}
56160
```
57161

58162
### **...**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int maxDistance(vector<int>& position, int m) {
4+
sort(position.begin(), position.end());
5+
int left = 1, right = position[position.size() - 1];
6+
while (left < right) {
7+
int mid = (left + right + 1) >> 1;
8+
if (check(position, mid, m)) left = mid;
9+
else right = mid - 1;
10+
}
11+
return left;
12+
}
13+
14+
bool check(vector<int>& position, int f, int m) {
15+
int pre = position[0];
16+
int cnt = 1;
17+
for (int i = 1; i < position.size(); ++i) {
18+
if (position[i] - pre >= f) {
19+
++cnt;
20+
pre = position[i];
21+
}
22+
}
23+
return cnt >= m;
24+
}
25+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func maxDistance(position []int, m int) int {
2+
sort.Ints(position)
3+
left, right := 1, position[len(position)-1]
4+
for left < right {
5+
mid := (left + right + 1) >> 1
6+
if check(position, mid, m) {
7+
left = mid
8+
} else {
9+
right = mid - 1
10+
}
11+
}
12+
return left
13+
}
14+
15+
func check(position []int, f, m int) bool {
16+
pre, cnt := position[0], 1
17+
for i := 1; i < len(position); i++ {
18+
if position[i]-pre >= f {
19+
cnt++
20+
pre = position[i]
21+
}
22+
}
23+
return cnt >= m
24+
}
Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
11
class Solution {
22
public int maxDistance(int[] position, int m) {
33
Arrays.sort(position);
4-
// 最小磁力的可能最小值
5-
int min = 1;
6-
// 最小磁力的可能最大值
7-
int max = (position[position.length - 1] - position[0]) / (m - 1);
8-
int ans = -1;
9-
while (min <= max) {
10-
int mid = (min + max) / 2;
11-
if (checkDistance(mid, position, m)) {
12-
ans = mid;
13-
min = mid + 1;
4+
int left = 1, right = position[position.length - 1];
5+
while (left < right) {
6+
int mid = (left + right + 1) >> 1;
7+
if (check(position, mid, m)) {
8+
left = mid;
149
} else {
15-
max = mid - 1;
10+
right = mid - 1;
1611
}
1712
}
18-
return ans;
13+
return left;
1914
}
2015

21-
private boolean checkDistance(int mid, int[] position, int m) {
22-
int count = 1;
16+
private boolean check(int[] position, int f, int m) {
2317
int pre = position[0];
24-
for (int i = 1; i < position.length; i++) {
25-
if (position[i] - pre >= mid) {
26-
count++;
27-
if (count >= m) {
28-
return true;
29-
}
18+
int cnt = 1;
19+
for (int i = 1; i < position.length; ++i) {
20+
if (position[i] - pre >= f) {
21+
++cnt;
3022
pre = position[i];
3123
}
3224
}
33-
return false;
25+
return cnt >= m;
3426
}
3527
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def maxDistance(self, position: List[int], m: int) -> int:
3+
position.sort()
4+
5+
def check(f):
6+
pre = position[0]
7+
cnt = 1
8+
for pos in position[1:]:
9+
if pos - pre >= f:
10+
cnt += 1
11+
pre = pos
12+
return cnt >= m
13+
14+
left, right = 1, position[-1]
15+
while left < right:
16+
mid = (left + right + 1) >> 1
17+
if check(mid):
18+
left = mid
19+
else:
20+
right = mid - 1
21+
return left

0 commit comments

Comments
 (0)