Maximize Distance to Closest Person - Complete Solution Guide
Maximize Distance to Closest Person is LeetCode problem 849, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an array representing a row of seats where seats[i] = 1 represents a person sitting in the i th seat, and seats[i] = 0 represents that the i th seat is empty (0-indexed) . There is at least one empty seat, and at least one person sitting. Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized. Return that maximum distance to the closest person . Example 1: Input: seats = [1,0,0,0,1,0,1] Output: 2 Explanation: If Alex sits in the
Detailed Explanation
The problem asks us to find the maximum distance that Alex can sit from the closest person in a row of seats. The input is an array `seats` where `seats[i] = 1` represents a person sitting in the i-th seat, and `seats[i] = 0` represents an empty seat. We need to find an empty seat for Alex such that the distance to the nearest occupied seat is maximized. There is guaranteed to be at least one empty seat and at least one occupied seat.
Solution Approach
The solution iterates through the `seats` array, keeping track of the index of the last person encountered. When it finds a person, it calculates the distance from the last person (if any) and updates the maximum distance found so far. Special consideration is given to the beginning and the end of the array.
Step-by-Step Algorithm
- Step 1: Initialize `max_dist` to 0 and `last_person` to -1 (indicating no person seen yet).
- Step 2: Iterate through the `seats` array from left to right.
- Step 3: If `seats[i]` is 1 (a person is sitting), check if it's the first person encountered (`last_person == -1`).
- Step 4: If it's the first person, the distance is `i` (distance from the beginning). Update `max_dist` with `i` if it's larger than the current `max_dist`.
- Step 5: If it's not the first person, calculate the distance between the current person and the last person: `(i - last_person) // 2`. Update `max_dist` if this distance is larger.
- Step 6: Update `last_person` to the current index `i`.
- Step 7: After the loop, calculate the distance from the last person to the end of the array: `n - 1 - last_person`. Update `max_dist` if this distance is larger.
- Step 8: Return `max_dist`.
Key Insights
- Insight 1: The maximum distance could be at the beginning of the row (before the first person), in the middle of the row (between two people), or at the end of the row (after the last person).
- Insight 2: We need to keep track of the position of the last person we saw.
- Insight 3: The distance between two people is divided by 2 (integer division) to find the maximum distance Alex can sit in between them.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array.
Companies
Asked at: Roblox, Samsung, Snap, Snowflake, Tinkoff, VK.