📌  相关文章
📜  将数组拆分为最小对子集,最大对数总和最多为K

📅  最后修改于: 2021-04-22 04:10:15             🧑  作者: Mango

给定大小为N且整数K的数组arr [] ,任务是将数组划分为最小数量的子集,以使每个子集中的最大对和小于或等于K。

例子:

方法:可以使用两个指针技术解决该问题。想法是对数组进行分区,以使每个子集的最大对和最小。请按照以下步骤解决问题:

  • 对给定的数组进行排序。
  • 初始化一个变量,例如res,以存储满足给定条件的最小子集数。
  • 初始化两个变量,例如startend ,分别存储已排序数组的start和end索引。
  • 遍历排序后的数组,并检查arr [start] + arr [end]≤K 。如果确定为true,则将start的值增加1
  • 否则,将end的值减1 ,并将res的值减1
  • 最后,打印res的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above appraoch
 
#include 
using namespace std;
 
// Function to get the minimum
// count of subsets that satisfy
// the given condition
int cntMinSub(int arr[],
              int N, int K)
{
    // Store the minimum count
    // of subsets that satisfy
    // the given condition
    int res = 0;
 
    // Stores start index
    // of the sorted array.
    int start = 0;
 
    // Stores end index
    // of the sorted array
    int end = N - 1;
 
    // Sort the given array
    sort(arr, arr + N);
 
    // Traverse the array
    while (end - start > 1) {
        if (arr[start] + arr[end]
            <= K) {
            start++;
        }
        else {
            res++;
            end--;
        }
    }
 
    // If only two elements
    // of sorted array left
    if (end - start == 1) {
        if (arr[start] + arr[end]
            <= K) {
            res++;
            start++;
            end--;
        }
        else {
            res++;
            end--;
        }
    }
 
    // If only one elements
    // left in the array
    if (start == end) {
        res++;
    }
 
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 6, 8, 10, 20, 25 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 26;
    cout << cntMinSub(arr, N, K);
}


Java
// Java program to implement
// the above appraoch
import java.util.*;
class GFG{
 
// Function to get the minimum
// count of subsets that satisfy
// the given condition
static int cntMinSub(int arr[],
                     int N, int K)
{
  // Store the minimum count
  // of subsets that satisfy
  // the given condition
  int res = 0;
 
  // Stores start index
  // of the sorted array.
  int start = 0;
 
  // Stores end index
  // of the sorted array
  int end = N - 1;
 
  // Sort the given array
  Arrays.sort(arr);
 
  // Traverse the array
  while (end - start > 1)
  {
    if (arr[start] +
        arr[end] <= K)
    {
      start++;
    }
    else
    {
      res++;
      end--;
    }
  }
 
  // If only two elements
  // of sorted array left
  if (end - start == 1)
  {
    if (arr[start] +
        arr[end] <= K)
    {
      res++;
      start++;
      end--;
    }
    else
    {
      res++;
      end--;
    }
  }
 
  // If only one elements
  // left in the array
  if (start == end)
  {
    res++;
  }
 
  return res;
}
 
// Driver Code
public static void main(String[] args)
{
  int arr[] = {2, 6, 8, 10, 20, 25};
  int N = arr.length;
  int K = 26;
  System.out.print(cntMinSub(arr, N, K));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program to implement
# the above appraoch
 
# Function to get the minimum
# count of subsets that satisfy
# the given condition
def cntMinSub(arr, N, K):
     
    # Store the minimum count
    # of subsets that satisfy
    # the given condition
    res = 0
 
    # Stores start index
    # of the sorted array.
    start = 0
 
    # Stores end index
    # of the sorted array
    end = N - 1
 
    # Sort the given array
    arr = sorted(arr)
 
    # Traverse the array
    while (end - start > 1):
        if (arr[start] + arr[end] <= K):
            start += 1
        else:
            res += 1
            end -= 1
 
    # If only two elements
    # of sorted array left
    if (end - start == 1):
        if (arr[start] + arr[end] <= K):
            res += 1
            start += 1
            end -= 1
        else:
            res += 1
            end -= 1
             
    # If only one elements
    # left in the array
    if (start == end):
        res += 1
 
    return res
 
# Driver Code
if __name__ == '__main__':
 
    arr = [ 2, 6, 8, 10, 20, 25 ]
    N = len(arr)
    K = 26
     
    print(cntMinSub(arr, N, K))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above appraoch
using System;
class GFG{
 
// Function to get the minimum
// count of subsets that satisfy
// the given condition
static int cntMinSub(int []arr,
                     int N, int K)
{
  // Store the minimum count
  // of subsets that satisfy
  // the given condition
  int res = 0;
 
  // Stores start index
  // of the sorted array.
  int start = 0;
 
  // Stores end index
  // of the sorted array
  int end = N - 1;
 
  // Sort the given array
  Array.Sort(arr);
 
  // Traverse the array
  while (end - start > 1)
  {
    if (arr[start] +
        arr[end] <= K)
    {
      start++;
    }
    else
    {
      res++;
      end--;
    }
  }
 
  // If only two elements
  // of sorted array left
  if (end - start == 1)
  {
    if (arr[start] +
        arr[end] <= K)
    {
      res++;
      start++;
      end--;
    }
    else
    {
      res++;
      end--;
    }
  }
 
  // If only one elements
  // left in the array
  if (start == end)
  {
    res++;
  }
 
  return res;
}
 
// Driver Code
public static void Main(String[] args)
{
  int []arr = {2, 6, 8, 10, 20, 25};
  int N = arr.Length;
  int K = 26;
  Console.Write(cntMinSub(arr, N, K));
}
}
 
// This code is contributed by 29AjayKumar


输出
3

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