Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Track Tooth Ends solution in Clear category for Toothpicks by kurosawa4434
from collections import defaultdict
def toothpicks(step: int) -> int:
ends = defaultdict(int)
next_ends = [(0, 0)]
tooth = 0
for s in range(step):
tooth += len(next_ends)
new_ends = []
for nx, ny in next_ends:
if s % 2:
np_1, np_2 = (nx-1, ny), (nx+1, ny)
else:
np_1, np_2 = (nx, ny-1), (nx, ny+1)
ends[np_1] += 1
ends[np_2] += 1
new_ends += [np_1, np_2]
next_ends = [np for np in new_ends if ends[np] < 2]
return tooth
Feb. 23, 2023
Comments: