Exit
  1. Learn
  2. Check
  3. Solve
  4. Reflect

Compare with the box before

A fitness app shows your longest streak of days in a row with a workout. To know whether today continues the streak, it looks at yesterday.

Questions about neighbours work the same way: at each box, compare it with the box just before it. Start the loop at the second box, because the first box has nothing before it to compare with.

Look one box back: start from the second box.
Runs of equal values
5
0
5
1
1
2
1
3
1
4
7
5
i

run = 1, best = 1.

Move 1 of 6

The streak grows or starts over

Keep the length of the current streak. If this box equals the one before, the streak grows by 1. If it's different, a new streak starts, of length 1.

The current streak can drop back to 1 at any moment, so keep a second variable for the longest streak you've ever seen. For 5, 5, 1, 1, 1, 7 that's 3, the three 1s.

Same as before: the streak grows by 1.
Different: a new streak of 1 starts.
In code
if not nums:
    return 0
run = best = 1
for i in range(1, len(nums)):
    if nums[i] == nums[i - 1]:
        run += 1
    else:
        run = 1
    best = max(best, run)
return best