Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Sum of Distinct Cubes by freeman_lex
import heapq as hp
def sum_of_cubes(n: int) -> list[int] | None:
hp.heapify(heap := [(n, [])])
while heap:
rest, split = hp.heappop(heap)
thrashhold = split[-1]-1 if split else round(pow(rest, 1/3))
for i in range(thrashhold, 0, -1):
(split1 := split[:]).append(i)
if not (n1 := rest - pow(i, 3)):
return split1
if n1 > 0:
hp.heappush(heap, (n1, split1))
print("Example:")
print(sum_of_cubes(1729))
# These "asserts" are used for self-checking
assert sum_of_cubes(1729) == [12, 1]
assert sum_of_cubes(8) == [2]
assert sum_of_cubes(11) == None
print("The mission is done! Click 'Check Solution' to earn rewards!")
July 29, 2023
Comments: