Maximize Distance to Closest Person LT849
In a row of seats
, 1
represents a person sitting in that seat, and 0
represents that the seat is empty.
There is at least one empty seat, and at least one person sitting.
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。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 closest person.
Example 1:
Input: [1,0,0,0,1,0,1]
Output: 2 Explanation: If Alex sits in the second open seat (seats[2]), then the closest person has distance 2. If Alex sits in any other open seat, the closest person has distance 1. Thus, the maximum distance to the closest person is 2.
Example 2:
Input: [1,0,0,0]
Output: 3 Explanation: If Alex sits in the last seat, the closest person is 3 seats away. This is the maximum distance possible, so the answer is 3.
Note:
1 <= seats.length <= 20000
seats
contains only 0s or 1s, at least one0
, and at least one1
.
Idea 1. Two pointers, one pointer (prev) to store the last index of one, if there are K(i - 1 - prev) zeros, the closest distance is (K+1)/2, A[i] = 1
Note two corner case: leading zeros before 1 (A[i] = 1), seat could be at A[0], the distance is i
ending zeros (A[A.length-1] = 0), seat could be at A[A.length-1], the distance is i - prev, i = A.length - 1.
Time complexity: O(n)
Space complexity: O(1)
class Solution { public int maxDistToClosest(int[] seats) { int prev = -1; int result = 0; for(int i = 0; i < seats.length; ++i) { if(seats[i] == 1) { if(prev == -1) { result = i; } else if( i >= prev + 2) { int zeros = i - 1 - prev; result = Math.max(result, (zeros+1)/2); } prev = i; } else if(i == seats.length-1) { result = Math.max(result, i - prev); } } return result; } }
Idea 2. Take each A[i] = 0 as potential seat, calculating the distance to the left A[left] = 1 (i - left) and the right A[right] = 1 (right - i), the distance is the min of (i - left, right - i).
Time complexity: O(n)
Space complexity: O(n)
use array to store the distance to the right
1 class Solution { 2 public int maxDistToClosest(int[] seats) { 3 int n = seats.length; 4 int[] right = new int[n]; 5 6 Arrays.fill(right, n); 7 for(int i = n-1; i >= 0; --i) { 8 if(seats[i] == 1) { 9 right[i] = 0; 10 } 11 else if(i < n-1) { 12 right[i] = right[i+1] + 1; 13 } 14 } 15 16 int result = 0; 17 int left = n; 18 for(int i = 0; i < n; ++i) { 19 if(seats[i] == 1) { 20 left = 0; 21 } 22 else { 23 ++left; 24 } 25 result = Math.max(result, Math.min(left, right[i])); 26 } 27 28 return result; 29 } 30 }
Idea 2.b two pointer
Time complexity: O(n)
Space complexity: O(1)
1 class Solution { 2 public int maxDistToClosest(int[] seats) { 3 int n = seats.length; 4 int result = 0; 5 6 int prev = -1; 7 8 for(int i = 0, right = 0; i < n; ++i) { 9 if(seats[i] == 1) { 10 prev = i; 11 } 12 else { 13 while((right < n && seats[right] == 0) || i > right) { 14 ++right; 15 } 16 17 int leftDist = (prev == -1? n : i - prev); 18 int rightDist = (right == n? n : right - i); 19 result = Math.max(result, Math.min(leftDist, rightDist)); 20 } 21 } 22 return result; 23 } 24 }
