Skip to content

Commit 0158e87

Browse files
committed
Add README to 849 Maximize Distance to Closest Person
1 parent cbd7d63 commit 0158e87

File tree

1 file changed

+102
-0
lines changed
  • arrays/849_Maximize_Distance_to_Closest_Person

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Maximize Distance to Closest Person
2+
3+
## Problem Description
4+
5+
You are given an array representing a row of seats where `seats[i] = 1`
6+
represents a person sitting in the `i`th seat, and `seats[i] = 0` represents
7+
that the `i`th seat is empty (0-indexed).
8+
9+
There is at least one empty seat, and at least one person sitting.
10+
11+
Alex wants to sit in the seat such that the distance between him and the
12+
closest person to him is maximized.
13+
14+
Return that maximum distance to the closest person.
15+
16+
**Example 1:**
17+
18+
* Input: `seats = [1, 0, 0, 0, 1, 0, 1]`
19+
* Output: `2`
20+
* Explanation:
21+
22+
If Alex sits in the second open seat (i.e. `seats[2]`), then the closest person
23+
has distance 2.
24+
If Alex sits in any other open seat, the closest person has distance 1.
25+
Thus, the maximum distance to the closest person is 2.
26+
27+
**Example 2:**
28+
29+
* Input: `seats = [1, 0, 0, 0]`
30+
* Output: `3`
31+
* Explanation:
32+
33+
If Alex sits in the last seat (i.e. `seats[3]`), the closest person is 3 seats away.
34+
This is the maximum distance possible, so the answer is 3.
35+
36+
**Example 3:**
37+
38+
* Input: `seats = [0, 1]`
39+
* Output: `1`
40+
41+
**Constraints:**
42+
43+
* `2 <= seats.length <= 2 * 10^4`
44+
* `seats[i]` is `0` or `1`.
45+
* At least one seat is empty.
46+
* At least one seat is occupied.
47+
48+
49+
## Solution
50+
51+
```python
52+
def max_dist_to_closest(seats: list[int]) -> int:
53+
"""
54+
Calculate the maximum distance to the closest occupied seat in a row.
55+
56+
The function finds the maximum possible distance a person can have to the nearest
57+
occupied seat when choosing a seat in a row represented by a binary list,
58+
where 1 represents an occupied seat and 0 represents an empty seat.
59+
60+
:param seats: A list of integers where 0 represents an empty seat and 1 represents
61+
an occupied seat. The list must contain at least one occupied seat.
62+
:return: The maximum distance to the closest occupied seat. The distance is measured
63+
as the number of seats between two positions (inclusive of endpoints).
64+
"""
65+
length = len(seats)
66+
left = None
67+
result = 1
68+
69+
for index in range(length):
70+
if seats[index] == 0:
71+
if left is None:
72+
left = index
73+
if (left == 0) or (index == length - 1):
74+
result = max(result, index - left + 1)
75+
else:
76+
result = max(result, (index - left + 2) // 2)
77+
else:
78+
left = None
79+
return result
80+
```
81+
82+
* **Time Complexity:** $O(n)$
83+
* **Space Complexity:** $O(1)$
84+
85+
## Explanation of the Solution
86+
87+
### Key Insights
88+
89+
1. **Empty Seats Between Occupied Seats:**
90+
* The optimal position is the middle of the longest block of empty seats between two 1s.
91+
2. **Edge Cases:**
92+
* If the first or last seat is empty, the farthest position is at that edge (no need to sit in the middle).
93+
94+
### Approach
95+
96+
1. **Track Empty Seat Blocks Efficiently:**
97+
* Use a single pointer left to mark the start of a block of empty seats.
98+
* If the block touches the start (`left=0`) or end (`index=last seat`) of the row, the distance is the full block length `(index - left + 1)`.
99+
* For internal blocks, the maximum distance is `(index - left + 2) // 2` (middle position).
100+
2. **Update Result Dynamically:**
101+
* When encountering an occupied seat (`1`), reset `left` to `None` (indicating no current empty block).
102+
* For empty seats (`0`), update left if starting a new block, then compute the current maximum distance.

0 commit comments

Comments
 (0)