8/32
Part 3 of 6 · A growing windowLearn and do
Grow on the right, shrink from the left
Some windows don't have a size you know in advance. The practice problem's first level asks for the longest stretch of the log in which no client appears twice.
Netflix candidates reported it three times (3 sources), more than any other window question in this course's bank, and it's on Netflix's LeetCode company list (1 source). It's usually asked on a string: the longest substring without a repeated character. A string is a list of characters, so it's the same code.
Starting over at every index checks the same keys again
The obvious way starts a stretch at every index and extends it until a key repeats:
def longest_every_start(keys):
best = 0
for start in range(len(keys)):
seen = set()
for key in keys[start:]:
if key in seen:
break
seen.add(key)
best = max(best, len(seen))
return best
log = ["u3", "u7", "u3", "u1", "u9", "u7", "u2", "u1"]
print(longest_every_start(log))
The log is u3 u7 u3 u1 u9 u7 u2 u1. What's the longest stretch with no key twice?
Pick one to see what it prints.
Pick an answer above to see the rest