📌  相关文章
📜  至少要挑选 K 双相同颜色的袜子

📅  最后修改于: 2021-09-04 11:43:47             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,其中arr[i]代表颜色为i的袜子的数量和一个整数K ,任务是找到需要挑选的最少袜子数量以获得至少 K对相同颜色的袜子。

例子:

方法:根据以下观察可以解决给定的问题:

  • 根据 Pigeonhole 的原理,即在最坏的情况下,如果已经挑选了N 只不同颜色的袜子,那么下一次挑选的袜子将形成一对匹配的袜子。
  • 假设一个人选择了N 只不同颜色的袜子,那么对于每双(K – 1)对,需要选择两只袜子,一只用来组成一对,另一只用来保持N 只不同颜色的袜子,对于最后一双,有只需要挑选任何颜色的袜子即可。

因此,我们的想法是找到可以由相同颜色形成的对的总数,如果总数最多为 K,则打印(2*K + N – 1)作为要挑选的对的最小数量。否则,打印“-1”,因为没有足够的袜子来形成K对。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the minimum
// number of socks to be picked
int findMin(int arr[], int N, int k)
{
    // Stores the total count
    // of pairs of socks
    int pairs = 0;
 
    // Find the total count of pairs
    for (int i = 0; i < N; i++) {
        pairs += arr[i] / 2;
    }
 
    // If K is greater than pairs
    if (k > pairs)
        return -1;
 
    // Otherwise
    else
        return 2 * k + N - 1;
}
 
int main()
{
    int arr[3] = { 4, 5, 6 };
    int K = 3;
    cout << findMin(arr, 3, K);
    return 0;
}
 
// This code is contributed by RohitOberoi.


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to count the minimum
    // number of socks to be picked
    public static int findMin(
        int[] arr, int N, int k)
    {
        // Stores the total count
        // of pairs of socks
        int pairs = 0;
 
        // Find the total count of pairs
        for (int i = 0; i < N; i++) {
            pairs += arr[i] / 2;
        }
 
        // If K is greater than pairs
        if (k > pairs)
            return -1;
 
        // Otherwise
        else
            return 2 * k + N - 1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 4, 5, 6 };
        int K = 3;
        int N = arr.length;
        System.out.println(findMin(arr, N, K));
    }
}


C#
// C# program for the above approach
 
using System;
 
class GFG {
 
    // Function to count the minimum
    // number of socks to be picked
    public static int findMin(int[] arr, int N, int k)
    {
        // Stores the total count
        // of pairs of socks
        int pairs = 0;
 
        // Find the total count of pairs
        for (int i = 0; i < N; i++) {
            pairs += arr[i] / 2;
        }
 
        // If K is greater than pairs
        if (k > pairs)
            return -1;
 
        // Otherwise
        else
            return 2 * k + N - 1;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] arr = { 4, 5, 6 };
        int K = 3;
        int N = arr.Length;
        Console.WriteLine(findMin(arr, N, K));
    }
}
 
// This code is contributed by ukasp.


Python3
# Python program for the above approach
 
# Function to count the minimum
# number of socks to be picked
def findMin(arr, N, k):
   
    # Stores the total count
    # of pairs of socks
    pairs = 0
     
    # Find the total count of pairs
    for i in range(N):
        pairs += arr[i] / 2
     
    # If K is greater than pairs
    if (k > pairs):
        return -1
 
    # Otherwise
    else:
        return 2 * k + N - 1
         
arr = [4, 5, 6]
k = 3
print(findMin(arr, 3, k));
     
# This code is contributed by SoumikMondal.


Javascript


输出:
8

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live