java.lang.Object
g2701_2800.s2779_maximum_beauty_of_an_array_after_applying_operation.Solution

public class Solution extends Object
2779 - Maximum Beauty of an Array After Applying Operation.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>.</p> <p>In one operation, you can do the following:</p> <ul> <li>Choose an index <code>i</code> that <strong>hasn&rsquo;t been chosen before</strong> from the range <code>[0, nums.length - 1]</code>.</li> <li>Replace <code>nums[i]</code> with any integer from the range <code>[nums[i] - k, nums[i] + k]</code>.</li> </ul> <p>The <strong>beauty</strong> of the array is the length of the longest subsequence consisting of equal elements.</p> <p>Return <em>the <strong>maximum</strong> possible beauty of the array</em> <code>nums</code> <em>after applying the operation any number of times.</em></p> <p><strong>Note</strong> that you can apply the operation to each index <strong>only once</strong>.</p> <p>A <strong>subsequence</strong> 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.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [4,6,1,2], k = 2</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong></p> <p>In this example, we apply the following operations:</p> <ul> <li> <p>Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2].</p> </li> <li> <p>Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4].</p> </li> </ul> <p>After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3).</p> <p>It can be proven that 3 is the maximum possible length we can achieve.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,1,1,1], k = 10</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong></p> <p>In this example we don&rsquo;t have to apply any operations.</p> <p>The beauty of the array nums is 4 (whole array).</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>0 <= nums[i], k <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maximumBeauty

      public int maximumBeauty(int[] nums, int k)