CódigoNão tentada
Maximize a distância até a pessoa mais próxima
Dificuldade: média
Dados assentos (1 ocupado, 0 vazio), retorne a maior distância possível até a pessoa mais próxima. Vãos nas pontas contam inteiros; vãos entre pessoas contam pela metade.
Ver solução de referência
export function maxDistToClosest(seats: number[]): number { let prev = -1; // the last taken seat we saw let best = 0; for (let i = 0; i < seats.length; i++) { if (seats[i] === 1) { if (prev === -1) { best = i; // empty seats at the start: sit at seat 0 } else { best = Math.max(best, Math.floor((i - prev) / 2)); // sit in the middle of the gap } prev = i; } } return Math.max(best, seats.length - 1 - prev); // empty seats at the end: sit at the last seat}
Ctrl Enter