Skip to content

Commit

Permalink
June15: Sliding windows [E]
Browse files Browse the repository at this point in the history
use sort & sliding windows to solve, w/ time complexity of O(NlogN),
no ds introduced, so space complexity if O(1)
  • Loading branch information
aucker committed Jun 15, 2024
1 parent 26d202b commit fcb7b48
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions daily/June15.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
/**
* @brief LC: 2779: max beauty of array [M]
* Time: O(nlogn), Space: O(1), no other ds introduced
* Sliding Windows,
*
* @param nums
* @param k
* @return int
*/
int maximumBeauty(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
int ans = 0, le = 0;
for (int ri = 0; ri < nums.size(); ri++) {
while (nums[ri] - nums[le] > k * 2) {
le++;
}
ans = max(ans, ri - le + 1);
}
return ans;
}
};

0 comments on commit fcb7b48

Please sign in to comment.