2779. Maximum Beauty of an Array After Applying Operation
Difficulty: Medium
Topics: Array, Binary Search, Sliding Window, Sorting
You are given a 0-indexed array nums and a non-negative integer k.
In one operation, you can do the following:
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 generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.
Example 1:
Example 2:
Constraints:
Hint:
Solution:
We can utilize sorting and a sliding window approach.
Let's implement this solution in PHP: 2779. Maximum Beauty of an Array After Applying Operation
<?php /** * @param Integer[] $nums * @param Integer $k * @return Integer */ function maximumBeauty($nums, $k) { ... ... ... /** * go to ./solution.php */ } // Example Usage: $nums1 = [4, 6, 1, 2]; $k1 = 2; echo maximumBeauty($nums1, $k1) . "\n"; // Output: 3 $nums2 = [1, 1, 1, 1]; $k2 = 10; echo maximumBeauty($nums2, $k2) . "\n"; // Output: 4 ?> <h3> Explanation: </h3> <ol> <li> <strong>Sorting the Array</strong>: <ul> <li>Sorting ensures that the window defined by indices <em><strong>[i, j]</strong></em> has all elements in increasing order, which makes it easier to check the difference between the smallest and largest values in the window.</li> </ul> </li> <li> <strong>Sliding Window</strong>: <ul> <li>Start with both i and j at the beginning.</li> <li>Expand the window by incrementing j and keep the window valid by incrementing i whenever the condition <em><strong>nums[j] - nums[i] > 2k</strong></em> is violated.</li> <li>At each step, calculate the size of the current valid window <em><strong>j - i 1</strong></em> and update the maxBeauty.</li> </ul> </li> </ol> <hr> <h3> Complexity Analysis: </h3> <ol> <li> <strong>Time Complexity</strong>: <ul> <li>Sorting the array: <em><strong>O(n log n)</strong></em>.</li> <li>Sliding window traversal: <em><strong>O(n)</strong></em>.</li> <li>Overall: <em><strong>O(n log n)</strong></em>.</li> </ul> </li> <li> <strong>Space Complexity</strong>: <ul> <li> <em><strong>O(1)</strong></em>, as the solution uses only a few additional variables.</li> </ul> </li> </ol> <hr> <h3> Examples: </h3> <h4> Input 1: </h4> <pre class="brush:php;toolbar:false">$nums = [4, 6, 1, 2]; $k = 2; echo maximumBeauty($nums, $k); // Output: 3
$nums = [1, 1, 1, 1]; $k = 10; echo maximumBeauty($nums, $k); // Output: 4
This solution adheres to the constraints and efficiently computes the result for large inputs.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of Maximum Beauty of an Array After Applying Operation. For more information, please follow other related articles on the PHP Chinese website!