Skip to content

Commit 7088bbc

Browse files
committed
Time: 521 ms (80.65%), Space: 33.1 MB (93.55%) - LeetHub
1 parent 46aa0ed commit 7088bbc

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> int:
3+
n = len(nums)
4+
nums.sort()
5+
6+
ans = 0
7+
8+
i = 0
9+
while i < n:
10+
j = i + 1
11+
while j < n and nums[j] == nums[i]:
12+
j += 1
13+
lo, hi = bisect_left(nums, nums[i] - k, 0, i), bisect_right(nums, nums[i] + k, j, n)
14+
ans = max(ans, min((hi - j) + (i - lo), numOperations) + j - i)
15+
i = j
16+
17+
l, r = 0, 0
18+
while l < n:
19+
if r < n and nums[r] - nums[l] <= 2*k:
20+
r += 1
21+
ans = max(ans, min(r - l, numOperations))
22+
else:
23+
l += 1
24+
25+
return ans

0 commit comments

Comments
 (0)