- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a set of boxes represented as a 2D array called boxTypes, where boxTypes[i] contains two elements [number of boxes of type i, number of units per box of type i]. Now we also have another value k, which is the maximum number of boxes that can be put on that truck. We can select any boxes to put on the truck as long as the number of boxes does not cross k. We have to find the maximum total number of units that can be put on the truck.

So, if the input is like boxTypes = [[2,4],[3,3],[4,2]], k = 6, then the output will be 19, because there are

2 boxes of type 1 and each contains 4 units

3 boxes of type 2 and each contains 3 units

4 boxes of type 3 and each contains 2 units

as k = 6, we can take all boxes of type 1 and 2, and only one box of type 3, so there will be (2*4) + (3*3) + 2 = 8 + 9 +2 = 19 items.

To solve this, we will follow these steps −

sort boxTypes based on number of items present in each box

total := 0, fill := 0

for each i in boxTypes, do

if fill + i[0] <= k, then

fill := fill + i[0]

total := total + i[0] * i[1]

otherwise,

total := total + (k - fill) * i[1]

come out from loop

return total

Let us see the following implementation to get better understanding −

def solve(boxTypes, k): boxTypes.sort(key = lambda x : x[1], reverse = True) total = 0 fill = 0 for i in boxTypes: if fill + i[0] <= k: fill += i[0] total += i[0] * i[1] else: total += (k - fill) * i[1] break return total boxTypes = [[2,4],[3,3],[4,2]] k = 6 print(solve(boxTypes, k))

[[2,4],[3,3],[4,2]], 6

19

- Related Questions & Answers
- Program to find out how many boxes can be put into the godown in Python
- Program to find maximum number of package that can be bought by buyers in C++
- Problem to Find Out the Maximum Number of Coins that Can be Collected in Python
- Maximum bishops that can be placed on N*N chessboard in C++
- Maximum elements which can be crossed using given units of a and b in C++
- Find maximum number that can be formed using digits of a given number in C++
- Program to find out the number of boxes to be put into the godown in Python
- Set the maximum width that a box can be with CSS
- Set the maximum height that a box can be with CSS
- Program to find length of longest word that can be formed from given letters in python
- Maximum Number of Events That Can Be Attended in C++
- Maximum number of candies that can be bought in C
- Maximum number that can be display on Seven Segment Display using N segments in C++
- Program to find maximum number of courses we can take based on interval time in Python?
- Program to find the length of the longest, that can be made using given letters in Python

Advertisements