Skip to content

Commit cbea16d

Browse files
authored
Update 2831-find-the-longest-equal-subarray.js
1 parent f73669b commit cbea16d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

2831-find-the-longest-equal-subarray.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,31 @@ const longestEqualSubarray = function(nums, k) {
1717
}
1818
return maxf;
1919
};
20+
21+
// another
22+
23+
/**
24+
* @param {number[]} nums
25+
* @param {number} k
26+
* @return {number}
27+
*/
28+
const longestEqualSubarray = function(nums, k) {
29+
const n = nums.length, cnt = {}
30+
let i = 0, res = 0
31+
for(let j = 0; j < n; j++) {
32+
const e = nums[j]
33+
if(cnt[e] == null) cnt[e] = 0
34+
cnt[e]++
35+
res = Math.max(res, cnt[e])
36+
37+
while(j - i + 1 - res > k) {
38+
const pre = nums[i]
39+
if(cnt[pre] == null) cnt[pre] = 0
40+
cnt[pre]--
41+
i++
42+
res = Math.max(res, cnt[nums[i]])
43+
}
44+
}
45+
46+
return res
47+
};

0 commit comments

Comments
 (0)