879. Maximize Distance to Closest Person
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find an empty seat in a row such that the distance to the nearest person is maximized. We are given an array representing the row, where 1 denotes a person and 0 an empty seat. We need to return this maximum possible distance.
Solution
Maximize Distance to Closest Person in Python
class Solution:
def maxDistToClosest(self, seats: list[int]) -> int:
n = len(seats)
last_person_idx = -1
max_dist = 0
for i in range(n):
if seats[i] == 1:
if last_person_idx == -1:
# This is the first person. The distance for an empty seat
# at the beginning is its index. The best spot is index 0,
# giving distance i.
max_dist = i
else:
# Gap between two people. The best spot is in the middle.
# Distance is half the gap size. Python's // is integer division.
dist = (i - last_person_idx) // 2
max_dist = max(max_dist, dist)
last_person_idx = i
# Consider the empty seats at the end.
# The best spot is the last seat, n-1.
# Distance to the last person is (n-1) - last_person_idx.
dist_at_end = n - 1 - last_person_idx
max_dist = max(max_dist, dist_at_end)
return max_dist
Maximize Distance to Closest Person in Java
class Solution {
public int maxDistToClosest(int[] seats) {
int n = seats.length;
int lastPersonIdx = -1;
int maxDist = 0;
for (int i = 0; i < n; i++) {
if (seats[i] == 1) {
if (lastPersonIdx == -1) {
// This is the first person we've seen.
// The best spot is at index 0, with distance i.
maxDist = i;
} else {
// Gap between two people. Best spot is in the middle.
// Distance is half the gap size.
// Java's integer division handles floor automatically.
int dist = (i - lastPersonIdx) / 2;
maxDist = Math.max(maxDist, dist);
}
lastPersonIdx = i;
}
}
// Consider the empty seats at the end.
// The best spot is the last seat, n-1.
// Distance to the last person is (n-1) - lastPersonIdx.
if (lastPersonIdx != -1) { // Check if any person was found
int distAtEnd = n - 1 - lastPersonIdx;
maxDist = Math.max(maxDist, distAtEnd);
}
return maxDist;
}
}
Maximize Distance to Closest Person in C++
class Solution {
public:
int maxDistToClosest(vector<int>& seats) {
int n = seats.size();
int last_person_idx = -1;
int max_dist = 0;
for (int i = 0; i < n; ++i) {
if (seats[i] == 1) {
if (last_person_idx == -1) {
// This is the first person we've seen.
// The best spot is at index 0, with distance i.
max_dist = i;
} else {
// Gap between two people. Best spot is in the middle.
// Distance is half the gap size.
// C++ integer division handles floor automatically.
int dist = (i - last_person_idx) / 2;
max_dist = std::max(max_dist, dist);
}
last_person_idx = i;
}
}
// Consider the empty seats at the end.
// The best spot is the last seat, n-1.
// Distance to the last person is (n-1) - last_person_idx.
if (last_person_idx != -1) { // Check if any person was found
int dist_at_end = n - 1 - last_person_idx;
max_dist = std::max(max_dist, dist_at_end);
}
return max_dist;
}
};
Maximize Distance to Closest Person in TypeScript
function maxDistToClosest(seats: number[]): number {
const n = seats.length;
let lastPersonIdx = -1;
let maxDist = 0;
for (let i = 0; i < n; i++) {
if (seats[i] === 1) {
if (lastPersonIdx === -1) {
// This is the first person we've seen.
// The best spot is at index 0, with distance i.
maxDist = i;
} else {
// Gap between two people. Best spot is in the middle.
// Distance is half the gap size. Need to use Math.floor.
const dist = Math.floor((i - lastPersonIdx) / 2);
maxDist = Math.max(maxDist, dist);
}
lastPersonIdx = i;
}
}
// Consider the empty seats at the end.
// The best spot is the last seat, n-1.
// Distance to the last person is (n-1) - lastPersonIdx.
if (lastPersonIdx !== -1) { // Check if any person was found
const distAtEnd = n - 1 - lastPersonIdx;
maxDist = Math.max(maxDist, distAtEnd);
}
return maxDist;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.