Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

3 of 4
added a commented version
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

#JavaScript (ES6), (削除) 56 (削除ここまで) 52 bytes

Returns a Boolean value.

a=>![...a,1].some(x=>t>0/x?1:(t+=x-1)*x<0?t=0:0,t=0)

Try it online!

###Commented

The accumulator \$t\$ is decremented whenever an empty slot is encountered. For each non-zero value \$x\$, we update \$t\$ to \$max(t+x-1,0)\$. The test fails if we reach a non-empty slot (or the end of the list) with \$t>0\$.

a => // a[] = input
 ![...a, 1] // append a '1' at the end of a[] to force a last test
 .some(x => // for each value x in the updated array:
 t > 0 / x ? // if x = 0, this test is always falsy (0 / 0 -> NaN)
 // if x ≠ 0 and t is greater than 0:
 1 // failed: yield 1
 : // else:
 (t += x - 1) // add x - 1 to t
 * x < 0 ? // if t is negative and x ≠ 0:
 t = 0 // we have accumulated more empty slots than
 // we can consume here: set t to 0
 : // else:
 0, // do nothing
 t = 0 // start with t = 0
 ) // end of some(); return the opposite result
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

AltStyle によって変換されたページ (->オリジナル) /