blob: 919ce4e572f2f56adc017f89e096600f3ad96616 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# 2779. Maximum Beauty of an Array After Applying Operation
"""
you are given a 0-indexed array 'nums' and a non-negative integer 'k'. in one
operation you can do the following: choose an index 'i' that hasn't been
chosen before from the range '[0, nums.len - 1]', replace 'nums[i]' with any
integer from the range '[nums[i] - k, nums[i] + k]'. the beauty of the array
is the length of the longest subsequence consisting of equal elements. return
the maximum possible beauty of the array 'nums' after applying the operation
any number of times. note that you can apply the operation to each index only
once. a subsequence of an array is a new array generate from the original
array by deleting some elements (possibly none) without changing the order of
the remaining elements.
"""
class Solution(object):
def maximumBeauty(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
nums.sort()
i = 0
for j in range(len(nums)):
if nums[j] - nums[i] > k * 2:
i += 1
return j - i + 1
if __name__ == "__main__":
obj = Solution()
print(obj.maximumBeauty(nums=[4, 6, 1, 2], k=2))
print(obj.maximumBeauty(nums=[1, 1, 1, 1], k=10))
|