最小化总重量为 W 的背包的数量,以存储包含大于 W/3 的元素的数组
给定一个数组arr[]和权重W 。任务是最小化存储数组所有元素所需的背包数量。一个背包最多可以存储W的总重量。
注意:数组的每个整数都大于 (W/3)。
例子:
Input: arr[] = {150, 150, 150, 150, 150}, W = 300
Output: 3
Explanation: Minimum of 3 Knapsacks are required to store all elements
Knapsack 1 – {150, 150}, Knapsack 2 – {150, 150}, Knapsack 3 – {150}. The weight of each knapsack is <= W.
Input: arr[] = {130, 140, 150, 160}, W = 300
Output: 2
Explanation: The knapsacks can be filled as {130, 150}, {140, 160}.
方法:这个问题可以通过使用两点方法和排序来解决。请按照以下步骤解决给定的问题。
- 以非降序对数组进行排序。
- 由于数组包含值大于W/3的元素,因此任何背包都不能包含两个以上的元素。
- 维护两个指针L和R 。最初L = 0 , R = N-1 。
- 为值L <= R维护一个 while 循环。
- 对于每个L和R检查值arr[L] + A[R] <= W 。如果这是真的,那么可以将这些积木放在同一个背包中。将L增加1并将R减少1 。
- 否则,背包将有一个值为arr[i]的元素。将R减少1 。
- 每个有效步骤的增量答案。
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to calculate
// minimum knapsacks required to
int minimumKnapsacks(int A[], int N, int W)
{
// Variable to store
// the knapsacks required
int ans = 0;
// Maintain two pointers L and R
int L = 0, R = N - 1;
// Sort the array in
// non-decreasing order
sort(A, A + N);
// Maintain a while loop
while (L <= R) {
// Check if there two elements
// can be stored in a
// single knapsack
if (A[L] + A[R] <= W) {
// Increment the answer
ans++;
// Decrease the right pointer
R--;
// Increase the left pointer
L++;
}
else {
// A single knapsack will be required
// to store the Right element
R--;
ans++;
}
}
return ans;
}
// Driver Code
int main()
{
int W = 300;
int arr[] = { 130, 140, 150, 160 };
// To store the size of arr[]
int N = sizeof(arr) / sizeof(arr[0]);
// Print the answer
cout << minimumKnapsacks(arr, N, W);
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to calculate
// minimum knapsacks required to
static int minimumKnapsacks(int A[], int N, int W)
{
// Variable to store
// the knapsacks required
int ans = 0;
// Maintain two pointers L and R
int L = 0, R = N - 1;
// Sort the array in
// non-decreasing order
Arrays.sort(A);
// Maintain a while loop
while (L <= R) {
// Check if there two elements
// can be stored in a
// single knapsack
if (A[L] + A[R] <= W) {
// Increment the answer
ans++;
// Decrease the right pointer
R--;
// Increase the left pointer
L++;
}
else {
// A single knapsack will be required
// to store the Right element
R--;
ans++;
}
}
return ans;
}
// Driver code
public static void main (String args[])
{
int W = 300;
int arr[] = { 130, 140, 150, 160 };
// To store the size of arr[]
int N = arr.length;
// Print the answer
System.out.println(minimumKnapsacks(arr, N, W));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python implementation for the above approach
# Function to calculate
# minimum knapsacks required to
def minimumKnapsacks(A, N, W):
# Variable to store
# the knapsacks required
ans = 0;
# Maintain two pointers L and R
L = 0
R = N - 1;
# Sort the array in
# non-decreasing order
A.sort();
# Maintain a while loop
while (L <= R):
# Check if there two elements
# can be stored in a
# single knapsack
if (A[L] + A[R] <= W):
# Increment the answer
ans += 1
# Decrease the right pointer
R -= 1
# Increase the left pointer
L += 1
else:
# A single knapsack will be required
# to store the Right element
R -= 1
ans += 1
return ans;
# Driver Code
W = 300;
arr = [ 130, 140, 150, 160 ]
# To store the size of arr[]
N = len(arr);
# Print the answer
print(minimumKnapsacks(arr, N, W))
# This code is contributed by saurabh_jaiswal.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to calculate
// minimum knapsacks required to
static int minimumKnapsacks(int []A, int N, int W)
{
// Variable to store
// the knapsacks required
int ans = 0;
// Maintain two pointers L and R
int L = 0, R = N - 1;
// Sort the array in
// non-decreasing order
Array.Sort(A);
// Maintain a while loop
while (L <= R) {
// Check if there two elements
// can be stored in a
// single knapsack
if (A[L] + A[R] <= W) {
// Increment the answer
ans++;
// Decrease the right pointer
R--;
// Increase the left pointer
L++;
}
else {
// A single knapsack will be required
// to store the Right element
R--;
ans++;
}
}
return ans;
}
// Driver code
public static void Main ()
{
int W = 300;
int []arr = { 130, 140, 150, 160 };
// To store the size of arr[]
int N = arr.Length;
// Print the answer
Console.Write(minimumKnapsacks(arr, N, W));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
2
时间复杂度: O(N*log(N))
辅助空间: O(1)