2831. Find the Longest Equal Subarray
Think as dp as first, but the dataset is 10^5 , it should only be linear or n log n.
When n log n solution, maybe binary search or sort. but not see any efficency here.
class Solution:
def longestEqualSubarray(self, nums: List[int], k: int) -> int:
n = len(nums)
M = defaultdict(list)
for i in range(n):
M[nums[i]].append(i)
res = 1
for ll in M.values():
current = 1
ck = k
l = 0
for i in range(1, len(ll)):
ck -= ll[i] - ll[i-1] - 1
while ck < 0 and l < i:
ck += ll[l+1] - ll[l] - 1
l += 1
current = i - l + 1
res = max(res, current)
return res