leetcode
leetcode 2401 ~ 2450
数组的最大美丽值

数组的最大美丽值

难度:

标签:

题目描述

You are given a 0-indexed array nums and a non-negative integer k.

In one operation, you can do the following:

  • Choose an index i that hasn't been chosen before from the range [0, nums.length - 1].
  • Replace nums[i] with any integer from the range [nums[i] - k, nums[i] + k].

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.

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:

Input: nums = [4,6,1,2], k = 2
Output: 3
Explanation: In this example, we apply the following operations:
- Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2].
- Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4].
After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3).
It can be proven that 3 is the maximum possible length we can achieve.

Example 2:

Input: nums = [1,1,1,1], k = 10
Output: 4
Explanation: In this example we don't have to apply any operations.
The beauty of the array nums is 4 (whole array).

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i], k <= 105

代码结果

运行时间: 210 ms, 内存: 28.4 MB


/*
 * Problem: Given an integer array nums and a non-negative integer k. 
 * In one operation, you can choose an index i that hasn't been chosen before and replace nums[i] with any integer in the range [nums[i] - k, nums[i] + k]. 
 * The beauty value of the array is defined as the length of the longest subsequence of equal elements. 
 * Return the maximum beauty value the array can achieve after any number of such operations.
 */

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class SolutionStream {
    public int maximizeBeauty(int[] nums, int k) {
        Map<Integer, Long> freqMap = Arrays.stream(nums)
            .boxed()
            .flatMap(num -> Arrays.stream(new int[k * 2 + 1]).mapToObj(i -> num - k + i))
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        return freqMap.values().stream().mapToInt(Long::intValue).max().orElse(0);
    }

    public static void main(String[] args) {
        SolutionStream sol = new SolutionStream();
        int[] nums1 = {4, 6, 1, 2};
        int k1 = 2;
        System.out.println(sol.maximizeBeauty(nums1, k1)); // Output: 3

        int[] nums2 = {1, 1, 1, 1};
        int k2 = 10;
        System.out.println(sol.maximizeBeauty(nums2, k2)); // Output: 4
    }
}

解释

方法:

这个题解使用了滑动窗口的技术。首先,创建一个数组 `cnt`,其长度为最大值 `max(nums) + 1` 或 `2*k + 1` 中的较大者,用于计数 `nums` 中每个元素的出现次数。接着,通过滑动窗口(窗口大小为 `2*k`)迭代 `cnt` 数组,窗口内的元素总和 `tot` 表示在当前的数字范围内,通过调整可以实现的元素值相同的最大数量。这个范围内的所有元素都可以调整到窗口的中间值,从而形成一个长的相同元素的子序列。随着窗口的滑动,更新结果 `ans` 为窗口内元素和的最大值,即可能的最大美丽值。

时间复杂度:

O(n + max(nums))

空间复杂度:

O(max(max(nums), 2*k))

代码细节讲解

🦆
在初始化计数数组`cnt`时,为什么选择`max(max(nums) + 1, 2 * k + 1)`作为长度?
选择`max(max(nums) + 1, 2 * k + 1)`作为计数数组`cnt`的长度是为了确保数组可以包含`nums`中所有可能的元素值,同时也能够满足最大可能的调整范围,即`2*k`。这样可以确保所有元素都能在数组`cnt`中被统计,无论它们的值是多少,只要它们在`nums`的最大值和`2*k`的范围内。
🦆
题解中提到通过调整可以将窗口内所有元素调整到中间值,请问如何确定这个中间值?
在这个算法中,中间值并没有直接指定,而是隐含地表示为窗口中心的值,即窗口的平均值或者中点位置的元素值。因为窗口保持恒定的大小`2*k`,所以窗口的`中间值`可以认为是窗口起始值加上`k`。这个值是理论上最佳的调整目标,因为在它的±k范围内的所有值都可以调整到这个中间值。
🦆
滑动窗口中,当`right`指针超过`2*k`后,为什么要将`left`指针向右移动?
当`right`指针超过`2*k`时,意味着窗口已经超出了其最大允许的大小`2*k`。为了维持窗口大小不变,我们需要将`left`指针向右移动,这样可以确保窗口的大小恒定为`2*k`。这种移动是为了继续探索其他潜在的子数组,同时确保窗口的滑动不会超出数组`cnt`的边界。
🦆
题解中提到`tot`表示当前窗口内通过调整可以实现的元素值相同的最大数量,但是否所有这些元素都能够调整到相同的值?
理论上,窗口内通过调整可以实现的元素值相同的最大数量是基于假设窗口内的所有元素都可以调整到`中间值`。然而,在实际情况中,这种调整只有在元素的原始值与目标值的差值不超过`k`时才是可行的。因此,虽然`tot`给出了理论上的最大可能数量,但实际上能否达到这个数量还取决于窗口内元素值的具体分布和调整的范围`k`。

相关问题