DEV Community

Discussion on: Daily Challenge #275 - Casino Chips

Collapse
 
qm3ster profile image
Mihail Malo

"constant time" in Javascript

const solve = chips => {
  const [low, mid, high] = [...chips].sort()
  const extra = low & 1
  const rings = low - extra
  const tower = mid - rings
  const last = (high !== mid) & extra
  return rings * 3 + tower + last
}
  1. first we take whole rings, (0-1, 1-2, 2-0)
  2. then we take a the stack of lone pairs ("tower")
  3. finally we see if remainder from rings that's not in the "tower" can last us one more day with remainder of tower.