Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Counter solution in Clear category for Home Coming by kurosawa4434
from typing import List
from collections import Counter
def home_coming(souvenirs: List[int], s: int) -> int:
result = 0
for w in range(len(souvenirs), -1, -1):
if w <= result:
return result
for i in range(len(souvenirs)-w+1):
result = max(result, sum(v for v in Counter(souvenirs[i:i+w]).values() if v <= s))
if __name__ == '__main__':
print("Example:")
print(home_coming([1, 1, 4, 1, 4, 4], 2))
# These "asserts" are used for self-checking and not for an auto-testing
assert home_coming([1, 1, 4, 1, 4, 4], 2) == 4, "Example #1"
assert home_coming([1, 2, 5, 3, 4, 5, 6, 7], 1) == 6, "Example #2"
assert home_coming([1, 2, 8, 8, 8, 8, 8, 3, 4, 1], 1) == 4, "Example #3"
print("Coding complete? Click 'Check' to earn cool rewards!")
May 21, 2020