Skip to content

Commit 0895a84

Browse files
authored
Create maximum-beauty-of-an-array-after-applying-operation.cpp
1 parent d281cf5 commit 0895a84

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Time: O(nlogn)
2+
// Space: O(1)
3+
4+
// sort, two pointers, sliding window
5+
class Solution {
6+
public:
7+
int maximumBeauty(vector<int>& nums, int k) {
8+
sort(begin(nums), end(nums));
9+
int right = 0, left = 0;
10+
for (; right < size(nums); ++right) {
11+
if (nums[right] - nums[left] > k * 2) {
12+
++left;
13+
}
14+
}
15+
return right - left;
16+
}
17+
};

0 commit comments

Comments
 (0)