2779. Maximum Beauty of an Array After Applying Operation Medium

@problem@discussion
#Array#Binary Search#Sliding Window#Sorting



1/**
2 * [2779] Maximum Beauty of an Array After Applying Operation
3 *
4 * You are given a 0-indexed array nums and a non-negative integer k.
5 * In one operation, you can do the following:
6 * 
7 * 	Choose an index i that hasn't been chosen before from the range [0, nums.length - 1].
8 * 	Replace nums[i] with any integer from the range [nums[i] - k, nums[i] + k].
9 * 
10 * The beauty of the array is the length of the longest subsequence consisting of equal elements.
11 * Return the maximum possible beauty of the array nums after applying the operation any number of times.
12 * Note that you can apply the operation to each index only once.
13 * 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.
14 *  
15 * <strong class="example">Example 1:
16 * 
17 * Input: nums = [4,6,1,2], k = 2
18 * Output: 3
19 * Explanation: In this example, we apply the following operations:
20 * - Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2].
21 * - Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4].
22 * After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3).
23 * It can be proven that 3 is the maximum possible length we can achieve.
24 * 
25 * <strong class="example">Example 2:
26 * 
27 * Input: nums = [1,1,1,1], k = 10
28 * Output: 4
29 * Explanation: In this example we don't have to apply any operations.
30 * The beauty of the array nums is 4 (whole array).
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	1 <= nums.length <= 10^5
36 * 	0 <= nums[i], k <= 10^5
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation/
42// discuss: https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn maximum_beauty(nums: Vec<i32>, k: i32) -> i32 {
48        0
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_2779() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.