All files / leetCode 0264.ts

100% Statements 15/15
100% Branches 6/6
100% Functions 1/1
100% Lines 14/14

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  2x   2x 11x 11x 11x 11x 11x 7x 11x 5x 11x 3x 11x     2x    
export default function nthUglyNumber(n: number): number {
  const nums = [1]
 
  for (let [count, i, j, k] = Array.from<number>({ length: 4 }).fill(0); count < n; count++) {
    const m2 = nums[i] * 2
    const m3 = nums[j] * 3
    const m5 = nums[k] * 5
    const min = Math.min(m2, m3, m5)
    if (min === m2)
      i++
    if (min === m3)
      j++
    if (min === m5)
      k++
    nums.push(min)
  }
 
  return nums[n - 1]
}