849. Maximize Distance to Closest Person Medium

@problem@discussion
#Array



1/**
2 * [849] Maximize Distance to Closest Person
3 *
4 * 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).
5 * There is at least one empty seat, and at least one person sitting.
6 * Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized. 
7 * Return that maximum distance to the closest person.
8 *  
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/10/distance.jpg" style="width: 650px; height: 257px;" />
11 * Input: seats = [1,0,0,0,1,0,1]
12 * Output: 2
13 * Explanation: 
14 * If Alex sits in the second open seat (i.e. seats[2]), then the closest person has distance 2.
15 * If Alex sits in any other open seat, the closest person has distance 1.
16 * Thus, the maximum distance to the closest person is 2.
17 * 
18 * Example 2:
19 * 
20 * Input: seats = [1,0,0,0]
21 * Output: 3
22 * Explanation: 
23 * If Alex sits in the last seat (i.e. seats[3]), the closest person is 3 seats away.
24 * This is the maximum distance possible, so the answer is 3.
25 * 
26 * Example 3:
27 * 
28 * Input: seats = [0,1]
29 * Output: 1
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	2 <= seats.length <= 2 * 10^4
35 * 	seats[i] is 0 or 1.
36 * 	At least one seat is empty.
37 * 	At least one seat is occupied.
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/maximize-distance-to-closest-person/
43// discuss: https://leetcode.com/problems/maximize-distance-to-closest-person/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn max_dist_to_closest(seats: Vec<i32>) -> i32 {
49        0
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_849() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.