Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
recursive solution in Clear category for Stacking Cubes by Leonix
from functools import lru_cache
from collections import Counter
def stacking_cubes(all_cubes, first=None):
all_cubes = Counter(all_cubes)
candidate_cubes = (cube for cube in all_cubes if is_stacking(first, cube))
possible_tower_heights = (stacking_cubes(all_cubes - Counter([current]), current) for current in candidate_cubes)
return (0 if first is None else first[2]) + max(possible_tower_heights, default=0)
@lru_cache()
def is_stacking(cube1, cube2):
if cube1 is None:
return True
x1, y1, h1 = cube1
x2, y2, h2 = cube2
x_intersect = (x1 <= x2 < x1 + h1) or (x2 <= x1 < x2 + h2)
y_intersect = (y1 <= y2 < y1 + h1) or (y2 <= y1 < y2 + h2)
return x_intersect and y_intersect
July 15, 2020
Comments: