📌  相关文章
📜  携带所有礼物所需的最少箱子

📅  最后修改于: 2021-10-27 06:23:29             🧑  作者: 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


Javascript


输出:

3

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程