📌  相关文章
📜  最小化子集的数量,最大和最小元素之间的差异不超过K

📅  最后修改于: 2021-04-29 04:54:39             🧑  作者: Mango

给定的阵列ARR []和一个整数K,任务是给定的数组成具有最大和最小元素≤k之间的差的子集最小数目分开。

例子:

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

  1. 以升序对数组进行排序。
  2. 遍历数组,将currMin设置为数组的第一个元素,并在遍历所有元素的情况下继续更新currMax
  3. 如果在任何索引处, currMaxcurrMin之间的差超过K,则将answer递增1,并将currMaxcurrMin设置为arr [i]。
  4. 最后,返回答案

下面是上述方法的实现:

C++
// C++ Program to implement
// above approach
#include 
using namespace std;
 
// Function to find the minimum count
// of subsets of required type
int findCount(int arr[], int N, int K)
{
    sort(arr, arr + N);
 
    // Stores the result
    int result = 1;
 
    // Store the maximum and minimum
      // element of the current subset
    int cur_max = arr[0];
    int cur_min = arr[0];
   
    for (int i = 1; i < N; i++) {
       
        // Update current maximum
        cur_max = arr[i];
       
        // If difference exceeds K
        if (cur_max - cur_min > K) {
           
            // Update count
            result++;
 
            // Update maximum and minimum
            // to the current subset
            cur_max = arr[i];
            cur_min = arr[i];
        }
    }
   
    return result;
}
 
// Driver Code
int main()
{
    int arr[] = { 1,10, 8, 3, 9 };
    int K = 3;
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << findCount(arr, N, K);
 
    return 0;
}


Java
// Java program to implement
// above approach
import java.util.*;
 
class GFG{
 
// Function to find the minimum count
// of subsets of required type
static int findCount(int arr[], int N, int K)
{
    Arrays.sort(arr);
 
    // Stores the result
    int result = 1;
 
    // Store the maximum and minimum
    // element of the current subset
    int cur_max = arr[0];
    int cur_min = arr[0];
 
    for(int i = 1; i < N; i++)
    {
         
        // Update current maximum
        cur_max = arr[i];
     
        // If difference exceeds K
        if (cur_max - cur_min > K)
        {
         
            // Update count
            result++;
 
            // Update maximum and minimum
            // to the current subset
            cur_max = arr[i];
            cur_min = arr[i];
        }
    }
    return result;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 10, 8, 3, 9 };
    int K = 3;
    int N = arr.length;
     
    System.out.print(findCount(arr, N, K));
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program to implement
# the above approach
 
# Function to find the minimum count
# of subsets of required type
def findCount(arr, N, K):
 
    arr.sort()
 
    # Stores the result
    result = 1
 
    # Store the maximum and minimum
    # element of the current subset
    cur_max = arr[0]
    cur_min = arr[0]
 
    for i in range(1, N):
 
        # Update current maximum
        cur_max = arr[i]
 
        # If difference exceeds K
        if(cur_max - cur_min > K):
 
            # Update count
            result += 1
 
            # Update maximum and minimum
            # to the current subset
            cur_max = arr[i]
            cur_min = arr[i]
 
    return result
 
# Driver Code
arr = [ 1, 10, 8, 3, 9 ]
K = 3
N = len(arr)
 
# Function call
print(findCount(arr, N, K))
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// above approach
using System;
class GFG{
 
// Function to find the minimum count
// of subsets of required type
static int findCount(int []arr,
                     int N, int K)
{
    Array.Sort(arr);
 
    // Stores the result
    int result = 1;
 
    // Store the maximum and minimum
    // element of the current subset
    int cur_max = arr[0];
    int cur_min = arr[0];
 
    for(int i = 1; i < N; i++)
    {
         
        // Update current maximum
        cur_max = arr[i];
     
        // If difference exceeds K
        if (cur_max - cur_min > K)
        {
         
            // Update count
            result++;
 
            // Update maximum and minimum
            // to the current subset
            cur_max = arr[i];
            cur_min = arr[i];
        }
    }
    return result;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 10, 8, 3, 9 };
    int K = 3;
    int N = arr.Length;
     
    Console.Write(findCount(arr, N, K));
}
}
 
// This code is contributed by gauravrajput1


输出:
2


时间复杂度: O(NLog(N))
辅助空间: O(1)