From fcb7b48f954bbe2ac5edb65d907d22db0c349994 Mon Sep 17 00:00:00 2001 From: aucker Date: Sat, 15 Jun 2024 11:40:17 +0800 Subject: [PATCH] June15: Sliding windows [E] use sort & sliding windows to solve, w/ time complexity of O(NlogN), no ds introduced, so space complexity if O(1) --- daily/June15.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 daily/June15.cpp diff --git a/daily/June15.cpp b/daily/June15.cpp new file mode 100644 index 0000000..4d7e824 --- /dev/null +++ b/daily/June15.cpp @@ -0,0 +1,26 @@ +#include +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& 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; + } +}; \ No newline at end of file