📜  携带所有礼物所需的最小包装盒

📅  最后修改于: 2021-05-04 12:50:43             🧑  作者: Mango

给定包含礼物重量的数组,以及表示一个盒子可以容纳的最大重量的整数K(所有盒子都是统一的)。每个箱子最多可同时携带2个礼物,前提是这些礼物的重量之和最多不超过箱子的限制。任务是找到携带所有礼物所需的最少盒子
注意:保证每件礼物都可以装在一个盒子里。

例子:

方法:如果最重的礼物可以与最轻的礼物共享一个盒子,则可以这样做。否则,最重的礼物无法与任何人配对,因此会得到一个单独的盒子。

之所以起作用,是因为如果最轻的礼物可以与任何人配对,那么它也可能与最重的礼物配对。

假设A [i]是当前最轻的礼物,而A [j]最重的礼物

然后,如果最重的礼物可以与最轻的礼物共享一个盒子(如果A [j] + A [i] <= K ),那么这样做,否则,最重的礼物将得到一个单独的盒子。

下面是上述方法的实现:

C++
// CPP implementation of above approach
#include 
using namespace std;
  
// Function to return number of boxes
int numBoxes(int A[], int n, int K)
{
    // Sort the boxes in ascending order
    sort(A, A + n);
  
    // Try to fit smallest box with
    // current heaviest box (from right
    // side)
    int i = 0, j = n - 1;
    int ans = 0;
    while (i <= j) {
        ans++;
        if (A[i] + A[j] <= K)
            i++;
        j--;
    }
  
    return ans;
}
  
// Driver program
int main()
{
    int A[] = { 3, 2, 2, 1 }, K = 3;
    int n = sizeof(A) / sizeof(A[0]);
    cout << numBoxes(A, n, K);
    return 0;
}
  
// This code is written by Sanjit_Prasad


Java
// Java implementation of above approach
import java.util.*;
  
class solution
{
  
// Function to return number of boxes
static int numBoxes(int A[], int n, int K)
{
    // Sort the boxes in ascending order
    Arrays.sort(A);
  
    // Try to fit smallest box with
    // current heaviest box (from right
    // side)
    int i = 0, j = n - 1;
    int ans = 0;
    while (i <= j) {
        ans++;
        if (A[i] + A[j] <= K)
            i++;
        j--;
    }
  
    return ans;
}
  
// Driver program
public static void main(String args[])
{
    int A[] = { 3, 2, 2, 1 }, K = 3;
    int n = A.length;
    System.out.println(numBoxes(A, n, K));
  
}
}
  
//THis code is contributed by
// Surendra_Gangwar


Python3
# Python3 implementation of 
# above approach 
  
# Function to return number of boxes 
def numBoxex(A,n,K):
  
    # Sort the boxes in ascending order 
    A.sort()
    # Try to fit smallest box with current 
    # heaviest box (from right side) 
    i =0
    j = n-1
    ans=0
    while i<=j:
        ans +=1
        if A[i]+A[j] <=K:
            i+=1
        j-=1
  
    return ans
  
# Driver code
if __name__=='__main__':
    A = [3, 2, 2, 1]
    K= 3
    n = len(A)
    print(numBoxex(A,n,K))
  
# This code is contributed by 
# Shrikant13


C#
// C# implementation of above approach
using System;
  
class GFG
{
      
// Function to return number of boxes
static int numBoxes(int []A, int n, int K)
{
    // Sort the boxes in ascending order
    Array.Sort(A);
  
    // Try to fit smallest box with
    // current heaviest box (from right
    // side)
    int i = 0, j = (n - 1);
    int ans = 0;
    while (i <= j) 
    {
        ans++;
        if (A[i] + A[j] <= K)
            i++;
        j--;
    }
  
    return ans;
}
  
// Driver Code
static public void Main ()
{
    int []A = { 3, 2, 2, 1 };
    int K = 3;
    int n = A.Length;
    Console.WriteLine(numBoxes(A, n, K));
}
}
  
// This code is contributed by ajit


PHP


输出:

3

时间复杂度: O(N * log(N)),其中N是数组的长度。