Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
recursion solution in Clear category for Greedy Number by kurosawa4434
def greedy_number(line: str, length: int) -> str:
if length == 0:
return ''
for number in sorted(set(line), reverse=True):
num_idx = line.index(number)
if len(line) - num_idx >= length:
return number + greedy_number(line[num_idx + 1:], length - 1)
print("Example:")
print(greedy_number("571", 2))
assert greedy_number("571", 2) == "71"
assert greedy_number("12", 1) == "2"
assert greedy_number("763832", 3) == "832"
assert greedy_number("4368534743453", 5) == "87453"
assert greedy_number("111121", 3) == "121"
assert greedy_number("54", 2) == "54"
print("The first mission is done! Click 'Check' to earn cool rewards!")
Oct. 27, 2021