Skip to content

Commit e1509e2

Browse files
committed
Time: 775 ms (37.28%), Space: 31.7 MB (40.83%) - LeetHub
1 parent 8b28ecc commit e1509e2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# time complexity: O(nlogn)
2+
# space complexity: O(logn)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def maxDistinctElements(self, nums: List[int], k: int) -> int:
8+
nums.sort()
9+
10+
count = 0
11+
prev = float('-inf')
12+
for num in nums:
13+
curr = min(max(num - k, prev + 1), num + k)
14+
if curr > prev:
15+
count += 1
16+
prev = curr
17+
return count
18+
19+
20+
nums = [1, 2, 2, 3, 3, 4]
21+
k = 2
22+
print(Solution().maxDistinctElements(nums, k))
23+
nums = [4, 4, 4, 4]
24+
k = 1
25+
print(Solution().maxDistinctElements(nums, k))

0 commit comments

Comments
 (0)