Maximize Distance to Closest Person
During these trying times, I came across this LeetCode problem and thought it would be a great and appropriate question to blog about.
This is the question:
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.
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.
Some examples of what they’re looking for:
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.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.
This my attempted solution and the solution I found:
/**
* @param {number[]} seats
* @return {number}
*/
var maxDistToClosest = function(seats) {
// // Check if only last seat is taken
// // [0,0,1]
// // => 2
// // Check if last seat is taken
// if (seats[seats.length - 1] === 1) {
// // Check if any middle seat is taken
// let allLeftSeatsOpen = false;
// let firstTakenSeatIndex = null;
// for (let i = 0; i < seats.length - 1; i++) {
// if (seats[i] === 1) {
// allLeftSeatsOpen = !allLeftSeatsOpen;
// firstTakenSeatIndex = i;
// break;
// }
// }
// // Check if all other seats to the right are taken
// // [0,0,1,1]
// let allRightSeatsTaken = true;
// for (let i = firstTakenSeatIndex; i < seats.length - 1; i++) {
// // console.log(seats[i]);
// if (seats[i] === 0) {
// allRightSeatsTaken != allRightSeatsTaken;
// break;
// }
// }
// if (allLeftSeatsOpen && allRightSeatsTaken) {
// return firstTakenSeatIndex;
// }
// }// // Check if only first seat is taken
// // [1,0,0,0]
// // => 3
// // An open seat is somewhere in the middle
// // [1,0,0,0,1,0,1]
// // => 2
let maxSeatCt = -Infinity;
for (let i = 0, j = 0; i < seats.length; i = j) {
// Locate left border of 0 [i]
for (i = j; i < seats.length && seats[i] === 1; i++) {}
// Locate right border of 0 [j - 1]
for (j = i; j < seats.length && seats[j] === 0; j++) {}
console.log(i, j);
if (i === 0 || j === seats.length)
maxSeatCt = Math.max(maxSeatCt, j - i);
else
maxSeatCt = Math.max(maxSeatCt, Math.floor((j - i + 1) / 2));
}
return maxSeatCt
};
At first, I was going to solve for the cases if all the seats on left were empty [0,0,0,1]
and if all the seats on the right were empty [1,0,0,0]
. And lastly, solve for cases in the middle [1,0,0,0,1,0,1]
.
As simple as this sounds though, I was not able to implement a solution even after 2 days of working on the problem. I decided that I did not want to spend any more unnecessary time on the problem and looked for a solution.
I found the solution posted at the bottom of my solution. Locate the right border, locate the left border, and return the maxSeatCount.
From this problem that I encountered, I learned not to spend too much time trying to push through something that I can not come up with an answer for and be willing to ask for help. I learned to deal with this emotional struggles, on top of the logical struggles of programming.
Stay safe everyone!