Efficient waste bagging
Last Updated :
03 Oct, 2023
Given a set of waste at certain positions in the Geek City, each with a weight value between 1.01 and 3.00, and the constraint is that a bag can carry at most 3.00 kg at one time, return the minimum number of bags required to carry all the waste efficiently.
Note: Please note that the weight values are represented in kilograms, and the objective is to optimize the number of bags needed while adhering to the weight limit of each bag. The weight of each bag should not exceed 3.00 kg.
Examples:
Input: wasteList = 1.30 1.40 1.50 1.60
Output: bags = 2
Explanation: bag1 with 1.30 and 1.60 and bag2 with 1.40 and 1.50.
Input: wasteList = 1.40 1.70 1.50 1.50
Output: bags = 3
Explanation: bag1 with 1.50 and 1.40, bag2 with 1.40 and bag3 with 1.50.
Efficient Approach: To solve the problem using Two Pointers follow the below idea:
Basic Observations:
- As the weights in every position are in the range of 1.01 and 3.00 that means a bag can collect the waste from at max of 2 places.
- All waste items weighing 2 kg or more must be placed in separate bags.
- First, we will sort the bags in ascending order.
- Then we will check the first and last bags, if the combined weight is 3 kg or less put them together in one bag, but if is greater than 3 then check for the first and 2nd last, and so on until we find a pair less than 3.
- But as we know the last element and first element are combined and if the weight is greater than 3 then there will be no bag that can combine with the last bag to give a combined weight of less than 3 because we had sorted the wasteList.
- So whenever we get such a case we will simply use another bag to put the last waste item in it and thus increment the answer.
Below is the implementation of the above idea.
C++
#include <algorithm>
#include <iostream>
#include <vector>
int MinBags(std::vector< double >& wasteList)
{
std::sort(wasteList.begin(), wasteList.end());
int answer = 0;
int start = 0;
int end = wasteList.size() - 1;
while (start <= end) {
if (start == end) {
answer += 1;
break ;
}
if (wasteList[start] + wasteList[end] <= 3) {
start += 1;
end -= 1;
answer += 1;
continue ;
}
answer += 1;
end -= 1;
}
return answer;
}
int main()
{
std::vector< double > wasteList
= { 1.30, 1.40, 1.50, 1.60 };
std::cout << MinBags(wasteList) << std::endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
public class GFG {
public static int minBags(ArrayList<Double> wasteList) {
Collections.sort(wasteList);
int answer = 0 ;
int start = 0 ;
int end = wasteList.size() - 1 ;
while (start <= end) {
if (start == end) {
answer += 1 ;
break ;
}
if (wasteList.get(start) + wasteList.get(end) <= 3 ) {
start += 1 ;
end -= 1 ;
answer += 1 ;
continue ;
}
answer += 1 ;
end -= 1 ;
}
return answer;
}
public static void main(String[] args) {
ArrayList<Double> wasteList = new ArrayList<>();
wasteList.add( 1.30 );
wasteList.add( 1.40 );
wasteList.add( 1.50 );
wasteList.add( 1.60 );
System.out.println(minBags(wasteList));
}
}
|
Python3
def MinBags(wasteList):
wasteList.sort()
answer = 0
start = 0
end = len (wasteList) - 1
while start < = end:
if start = = end:
answer + = 1
break
if wasteList[start] + wasteList[end] < = 3 :
start + = 1
end - = 1
answer + = 1
continue
answer + = 1
end - = 1
return answer
wasteList = [ 1.30 , 1.40 , 1.50 , 1.60 ]
print (MinBags(wasteList))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static int MinBags(List< double > wasteList)
{
wasteList.Sort();
int answer = 0;
int start = 0;
int end = wasteList.Count - 1;
while (start <= end)
{
if (start == end)
{
answer += 1;
break ;
}
if (wasteList[start] + wasteList[end] <= 3)
{
start += 1;
end -= 1;
answer += 1;
continue ;
}
answer += 1;
end -= 1;
}
return answer;
}
static void Main( string [] args)
{
List< double > wasteList = new List< double > { 1.30, 1.40, 1.50, 1.60 };
Console.WriteLine(MinBags(wasteList));
}
}
|
Javascript
function MinBags(wasteList) {
wasteList.sort();
let answer = 0;
let start = 0;
let end = wasteList.length - 1;
while (start <= end) {
if (start === end) {
answer += 1;
break ;
}
if (wasteList[start] + wasteList[end] <= 3) {
start += 1;
end -= 1;
answer += 1;
continue ;
}
answer += 1;
end -= 1;
}
return answer;
}
const wasteList = [1.30, 1.40, 1.50, 1.60];
console.log(MinBags(wasteList));
|
Time Complexity: O(N),
Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...