Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
2 for loops + 1 defaultdict solution in Speedy category for Home Coming by r_tchaik
from typing import List
from collections import defaultdict
def home_coming(souvenirs: List[int], s: int) -> int:
onboard = 0
for l in range(len(souvenirs)):
interval = defaultdict(int)
total = 0
for souvenir in souvenirs[l:]:
interval[souvenir] += 1
if (current := interval[souvenir]) <= s:
total += 1
elif current == s + 1:
total -= current - 1
onboard = max(onboard, total)
return onboard
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 30, 2020
Comments: