function countSort(nums: number[], k: number = -1) {
  if (k == -1) {
    k = nums.reduce((acc, value) => {
      return acc < value ? value : acc
    }, 0)
  }
 
  const counts = Array(k + 1).fill(0)
  for (let i = 0; i < nums.length; i++) {
    const value = nums[i]
    counts[value]++
  }
 
  const results = Array(nums.length)
 
  let idx = 0
  for (let i = 0; i <= k; i++) {
    const count = counts[i]
    nums.fill(i, idx, idx + count)
    idx += count
  }
  return nums
}