All files / problems/assignMiceToHoles index.ts

100% Statements 26/26
80% Branches 4/5
100% Functions 1/1
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 271x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 2x 2x 2x 1x 1x  
// There are N mice and N holes. A mice takes 1 minute to travel 1 unit left or right.
// Find the minimum time after which all mice are in holes.
// •Constraints:
// • 1 < N < 1e5
// • - 1e9 < Alil < Blil < 1e9
 
const assignMiceToHoles = (
  mice: number[],
  holes: number[]
): number | undefined => {
  if (!mice.length) return;
 
  const miceSorted = mice.sort((a, b) => a - b);
  const holesSorted = holes.sort((a, b) => a - b);
 
  let maximum = 0;
 
  for (let x = 0; x < miceSorted.length; x++) {
    const steps = Math.abs(miceSorted[x] - holesSorted[x]);
    maximum = Math.max(steps, maximum);
  }
 
  return maximum;
};
 
export default assignMiceToHoles;