📌  相关文章
📜  可以使 k 更新相等的最大元素 |设置 2

📅  最后修改于: 2022-05-13 01:56:08.870000             🧑  作者: Mango

可以使 k 更新相等的最大元素 |设置 2

给定一个数组nums[]和一个值k 。任务是找到最多 k 个更新/增量可以相等的最大元素

例子:

方法:这是本文第 1 组中讨论的有效方法的空间优化方法。

该任务可以在滑动窗口概念的帮助下解决。基本上对于从 (i 到 j) 的每个窗口,我们查看是否可以使当前窗口的所有元素都等于窗口最后一个元素。如果最多 k 次更新是可能的,则存储窗口的大小,否则丢弃窗口的起始元素

请按照以下步骤解决问题:

  • 对数组nums进行排序。
  • 声明一个整数变量sum来存储数组元素的运行总和。
  • 声明一个整数变量ans以存储nums数组中最频繁元素的最大可能频率。
  • i...j成为我们正在处理的当前窗口。
  • 遍历数组nums。
  • 基本上,在每一步,我们都试图使从nums[i]nums[j]的所有元素都等于nums[j]。
  • 如果从nums[i]nums[j]的所有元素的总和加上k的值足以这样做,那么窗口是有效的。
  • 否则, i的值需要递增,因为nums[i]nums[j]的值之差最大,所以nums[i]k的最大部分等于nums[j] ,这就是为什么它需要从当前窗口中删除。
  • ans的值等于当前有效窗口的大小即(ji)
  • 打印数组 nums 中出现频率最高的元素的频率。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum count of
// equal elements
void getMax(vector& nums, int k)
{
    // Size of nums array
    int n = nums.size();
 
    // Sort the nums array
    sort(nums.begin(), nums.end());
 
    // Stores the running sum
    // of array elements
    long sum = 0;
 
    // Stores the maximum possible frequency
    int ans = 0;
 
    // i is the starting index of the window
    // j is the ending index of the window
    int i, j;
    i = j = 0;
 
    // Traverse the array nums
    for (j = 0; j < n; j++) {
 
        // Add the value of
        // current element to sum
        sum += nums[j];
 
        // If the addition of sum
        // and k is sufficient to
        // make all the array elements
        // from i..j equal to nums[j]
        if ((long)(sum + k)
            >= ((long)nums[j] * (j - i + 1)))
            continue;
 
        // Update the value of ans
        // to store the maximum
        // possible frequency so far
        if ((j - i) > ans)
            ans = j - i;
 
        // Subtract the value of nums[i] from sum,
        // because the addition of sum and
        // k are not sufficient to make
        // all the array elements from i..j
        // equal to nums[j] and difference of
        // values of A[j] and A[i] is maximum,
        // so A[i] takes the maximum part of k
        // to become equal to A[j],
        // that's why it is removed
        // from current window.
        sum -= nums[i];
 
        // Slide the current window
        i++;
    }
 
    // Update the value of ans if required
    ans = max(ans, j - i);
 
    // Print the result
    cout << ans << endl;
}
 
// Driver Code
int main()
{
    vector nums = { 1, 3, 4 };
    int k = 6;
    getMax(nums, k);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG
{
 
  // Function to find the maximum count of
  // equal elements
  static void getMax(int nums[ ], int k)
  {
 
    // Size of nums array
    int n = nums.length;
 
    // Sort the nums array
    Arrays.sort(nums);
 
    // Stores the running sum
    // of array elements
    long sum = 0;
 
    // Stores the maximum possible frequency
    int ans = 0;
 
    // i is the starting index of the window
    // j is the ending index of the window
    int i, j;
    i = j = 0;
 
    // Traverse the array nums
    for (j = 0; j < n; j++) {
 
      // Add the value of
      // current element to sum
      sum += nums[j];
 
      // If the addition of sum
      // and k is sufficient to
      // make all the array elements
      // from i..j equal to nums[j]
      if ((long)(sum + k)
          >= ((long)nums[j] * (j - i + 1)))
        continue;
 
      // Update the value of ans
      // to store the maximum
      // possible frequency so far
      if ((j - i) > ans)
        ans = j - i;
 
      // Subtract the value of nums[i] from sum,
      // because the addition of sum and
      // k are not sufficient to make
      // all the array elements from i..j
      // equal to nums[j] and difference of
      // values of A[j] and A[i] is maximum,
      // so A[i] takes the maximum part of k
      // to become equal to A[j],
      // that's why it is removed
      // from current window.
      sum -= nums[i];
 
      // Slide the current window
      i++;
    }
 
    // Update the value of ans if required
    ans = Math.max(ans, j - i);
 
    // Print the result
    System.out.println(ans);
 
  }
  public static void main (String[] args)
  {
    int nums[ ] = { 1, 3, 4 };
    int k = 6;
    getMax(nums, k);
  }
}
 
// This code is contributed by hrithikgarg03188


Python
# Python program for the above approach
 
# Function to find the maximum count of
# equal elements
def getMax(nums, k):
     
    # Size of nums array
    n = len(nums)
 
    # Sort the nums array
    nums.sort()
 
    # Stores the running sum
    # of array elements
    sum = 0
 
    # Stores the maximum possible frequency
    ans = 0
 
    # i is the starting index of the window
    # j is the ending index of the window
    i = j = f = 0
     
    # Traverse the array nums
    for j in range(n):
     
        # Add the value of
        # current element to sum
        sum = sum + nums[j]
 
        # If the addition of sum
        # and k is sufficient to
        # make all the array elements
        # from i..j equal to nums[j]
        if ((sum + k) >= (nums[j] * (j - i + 1))):
            f = 1
            continue
 
        # Update the value of ans
        # to store the maximum
        # possible frequency so far
        if ((j - i) > ans):
            ans = j - i
 
        # Subtract the value of nums[i] from sum,
        # because the addition of sum and
        # k are not sufficient to make
        # all the array elements from i..j
        # equal to nums[j] and difference of
        # values of A[j] and A[i] is maximum,
        # so A[i] takes the maximum part of k
        # to become equal to A[j],
        # that's why it is removed
        # from current window.
        sum = sum - nums[i]
 
        # Slide the current window
        i = i + 1
         
        f = 1
 
    # Update the value of ans if required
    if(f == 1):
        ans = max(ans, j - i + 1)
    else:
        ans = max(ans, j - i)
 
    # Print the result
    print(ans)
 
# Driver Code
nums = [ 1, 3, 4 ]
k = 6
getMax(nums, k)
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# program for the above approach
using System;
 
class GFG
{
 
    // Function to find the maximum count of
    // equal elements
    static void getMax(int[] nums, int k)
    {
 
        // Size of nums array
        int n = nums.Length;
 
        // Sort the nums array
        Array.Sort(nums);
 
        // Stores the running sum
        // of array elements
        long sum = 0;
 
        // Stores the maximum possible frequency
        int ans = 0;
 
        // i is the starting index of the window
        // j is the ending index of the window
        int i, j;
        i = j = 0;
 
        // Traverse the array nums
        for (j = 0; j < n; j++)
        {
 
            // Add the value of
            // current element to sum
            sum += nums[j];
 
            // If the addition of sum
            // and k is sufficient to
            // make all the array elements
            // from i..j equal to nums[j]
            if ((long)(sum + k)
                >= ((long)nums[j] * (j - i + 1)))
                continue;
 
            // Update the value of ans
            // to store the maximum
            // possible frequency so far
            if ((j - i) > ans)
                ans = j - i;
 
            // Subtract the value of nums[i] from sum,
            // because the addition of sum and
            // k are not sufficient to make
            // all the array elements from i..j
            // equal to nums[j] and difference of
            // values of A[j] and A[i] is maximum,
            // so A[i] takes the maximum part of k
            // to become equal to A[j],
            // that's why it is removed
            // from current window.
            sum -= nums[i];
 
            // Slide the current window
            i++;
        }
 
        // Update the value of ans if required
        ans = Math.Max(ans, j - i);
 
        // Print the result
        Console.Write(ans);
 
    }
    public static void Main()
    {
        int[] nums = { 1, 3, 4 };
        int k = 6;
        getMax(nums, k);
    }
}
 
// This code is contributed by Saurabh Jaiswal


Javascript


输出
3

时间复杂度: O(N*log(N)),其中 N 是数组 nums 的大小
辅助空间: O(1)