Skip to content

Commit b04e80a

Browse files
committed
feat: add solutions to lc problem: No.0849
No.0849.Maximize Distance to Closest Person
1 parent 51b2191 commit b04e80a

File tree

6 files changed

+244
-2
lines changed

6 files changed

+244
-2
lines changed

solution/0800-0899/0849.Maximize Distance to Closest Person/README.md

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

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:一次遍历**
63+
64+
我们定义两个变量 `first``last` 分别表示第一个人和最后一个人的位置,用变量 `d` 表示两个人之间的最大距离。
65+
66+
然后遍历数组 `seats`,如果当前位置有人,如果此前 `last` 更新过,说明此前有人,此时更新 $d = \max(d, i - last)$;如果此前 `first` 没有更新过,说明此前没有人,此时更新 `first = i`,然后更新 `last = i`
67+
68+
最后返回 $\max(first, n - last - 1, d / 2)$ 即可。
69+
70+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `seats` 的长度。
71+
6272
<!-- tabs:start -->
6373

6474
### **Python3**
6575

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

6878
```python
69-
79+
class Solution:
80+
def maxDistToClosest(self, seats: List[int]) -> int:
81+
first = last = None
82+
d = 0
83+
for i, c in enumerate(seats):
84+
if c:
85+
if last is not None:
86+
d = max(d, i - last)
87+
if first is None:
88+
first = i
89+
last = i
90+
return max(first, len(seats) - last - 1, d // 2)
7091
```
7192

7293
### **Java**
7394

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

7697
```java
98+
class Solution {
99+
public int maxDistToClosest(int[] seats) {
100+
int first = -1, last = -1;
101+
int d = 0, n = seats.length;
102+
for (int i = 0; i < n; ++i) {
103+
if (seats[i] == 1) {
104+
if (last != -1) {
105+
d = Math.max(d, i - last);
106+
}
107+
if (first == -1) {
108+
first = i;
109+
}
110+
last = i;
111+
}
112+
}
113+
return Math.max(d / 2, Math.max(first, n - last - 1));
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
int maxDistToClosest(vector<int>& seats) {
124+
int first = -1, last = -1;
125+
int d = 0, n = seats.size();
126+
for (int i = 0; i < n; ++i) {
127+
if (seats[i] == 1) {
128+
if (last != -1) {
129+
d = max(d, i - last);
130+
}
131+
if (first == -1) {
132+
first = i;
133+
}
134+
last = i;
135+
}
136+
}
137+
return max({d / 2, max(first, n - last - 1)});
138+
}
139+
};
140+
```
77141
142+
### **Go**
143+
144+
```go
145+
func maxDistToClosest(seats []int) int {
146+
first, last := -1, -1
147+
d := 0
148+
for i, c := range seats {
149+
if c == 1 {
150+
if last != -1 {
151+
d = max(d, i-last)
152+
}
153+
if first == -1 {
154+
first = i
155+
}
156+
last = i
157+
}
158+
}
159+
return max(d/2, max(first, len(seats)-last-1))
160+
}
161+
162+
func max(a, b int) int {
163+
if a > b {
164+
return a
165+
}
166+
return b
167+
}
78168
```
79169

80170
### **...**

solution/0800-0899/0849.Maximize Distance to Closest Person/README_EN.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,93 @@ This is the maximum distance possible, so the answer is 3.
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def maxDistToClosest(self, seats: List[int]) -> int:
63+
first = last = None
64+
d = 0
65+
for i, c in enumerate(seats):
66+
if c:
67+
if last is not None:
68+
d = max(d, i - last)
69+
if first is None:
70+
first = i
71+
last = i
72+
return max(first, len(seats) - last - 1, d // 2)
6273
```
6374

6475
### **Java**
6576

6677
```java
78+
class Solution {
79+
public int maxDistToClosest(int[] seats) {
80+
int first = -1, last = -1;
81+
int d = 0, n = seats.length;
82+
for (int i = 0; i < n; ++i) {
83+
if (seats[i] == 1) {
84+
if (last != -1) {
85+
d = Math.max(d, i - last);
86+
}
87+
if (first == -1) {
88+
first = i;
89+
}
90+
last = i;
91+
}
92+
}
93+
return Math.max(d / 2, Math.max(first, n - last - 1));
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int maxDistToClosest(vector<int>& seats) {
104+
int first = -1, last = -1;
105+
int d = 0, n = seats.size();
106+
for (int i = 0; i < n; ++i) {
107+
if (seats[i] == 1) {
108+
if (last != -1) {
109+
d = max(d, i - last);
110+
}
111+
if (first == -1) {
112+
first = i;
113+
}
114+
last = i;
115+
}
116+
}
117+
return max({d / 2, max(first, n - last - 1)});
118+
}
119+
};
120+
```
67121
122+
### **Go**
123+
124+
```go
125+
func maxDistToClosest(seats []int) int {
126+
first, last := -1, -1
127+
d := 0
128+
for i, c := range seats {
129+
if c == 1 {
130+
if last != -1 {
131+
d = max(d, i-last)
132+
}
133+
if first == -1 {
134+
first = i
135+
}
136+
last = i
137+
}
138+
}
139+
return max(d/2, max(first, len(seats)-last-1))
140+
}
141+
142+
func max(a, b int) int {
143+
if a > b {
144+
return a
145+
}
146+
return b
147+
}
68148
```
69149

70150
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int maxDistToClosest(vector<int>& seats) {
4+
int first = -1, last = -1;
5+
int d = 0, n = seats.size();
6+
for (int i = 0; i < n; ++i) {
7+
if (seats[i] == 1) {
8+
if (last != -1) {
9+
d = max(d, i - last);
10+
}
11+
if (first == -1) {
12+
first = i;
13+
}
14+
last = i;
15+
}
16+
}
17+
return max({d / 2, max(first, n - last - 1)});
18+
}
19+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func maxDistToClosest(seats []int) int {
2+
first, last := -1, -1
3+
d := 0
4+
for i, c := range seats {
5+
if c == 1 {
6+
if last != -1 {
7+
d = max(d, i-last)
8+
}
9+
if first == -1 {
10+
first = i
11+
}
12+
last = i
13+
}
14+
}
15+
return max(d/2, max(first, len(seats)-last-1))
16+
}
17+
18+
func max(a, b int) int {
19+
if a > b {
20+
return a
21+
}
22+
return b
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maxDistToClosest(int[] seats) {
3+
int first = -1, last = -1;
4+
int d = 0, n = seats.length;
5+
for (int i = 0; i < n; ++i) {
6+
if (seats[i] == 1) {
7+
if (last != -1) {
8+
d = Math.max(d, i - last);
9+
}
10+
if (first == -1) {
11+
first = i;
12+
}
13+
last = i;
14+
}
15+
}
16+
return Math.max(d / 2, Math.max(first, n - last - 1));
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maxDistToClosest(self, seats: List[int]) -> int:
3+
first = last = None
4+
d = 0
5+
for i, c in enumerate(seats):
6+
if c:
7+
if last is not None:
8+
d = max(d, i - last)
9+
if first is None:
10+
first = i
11+
last = i
12+
return max(first, len(seats) - last - 1, d // 2)

0 commit comments

Comments
 (0)