
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find out how many boxes can be put into the godown in Python
Suppose, we have two arrays containing integers. One list contains the height of some unit width boxes and another array contains the height of rooms in the godown. The rooms are numbered 0...n, and the height of the rooms is provided in their respective indexes in the array godown. We have to find out the number of boxes that can be pushed into the godown. a few things have to be kept in mind,
The boxes can’t be put one on another.
The order of the boxes can be changed.
The boxes are put into the godown from any side, it can either be from the left or the right. If a box is taller than the height of the room, then the box along with all the boxes to its right cannot be pushed into the godown.
So, if the input is like boxes = [4, 5, 6], godown = [4, 5, 6, 7], then the output will be 3 All three boxes given as input can be put into the godown.
To solve this, we will follow these steps −
sort the list boxes in a descending order
l := 0
r := size of godown - 1
bi := 0
ret := 0
while bi < size of boxes and l <= r, do
if godown[l] > godown[r], then
if boxes[bi] <= godown[l], then
ret := ret + 1
l := l + 1
otherwise,
if boxes[bi] <= godown[r], then
ret := ret + 1
r := r - 1
bi := bi + 1
return ret
Example (Python)
Let us see the following implementation to get better understanding −
def solve(boxes, godown): boxes.sort(reverse = True) l, r = 0, len(godown) - 1 bi, ret = 0, 0 while bi < len(boxes) and l <= r: if godown[l] > godown[r]: if boxes[bi] <= godown[l]: ret += 1 l += 1 else: if boxes[bi] <= godown[r]: ret += 1 r -= 1 bi += 1 return ret print(solve([4, 5, 6], [4, 5, 6, 7]))
Input
[4, 5, 6], [4, 5, 6, 7]
Output
3
- Related Articles
- Program to find out the number of boxes to be put into the godown in Python
- Program to find out how many transfer requests can be satisfied in Python
- How many types of JDialog boxes can be created in Java?
- Python Program to find out how many cubes are cut
- Program to find maximum units that can be put on a truck in Python
- Program to find maximum number of boxes we can fit inside another boxes in python
- Program to find out number of blocks that can be covered in Python
- The boxes are to be filled with apples in a heap. If 24 apples are put in a box then 27 boxes are needed. If 36 apples are filled in a box how many boxes will be needed?
- Python Program to find out the number of rooms in which a prize can be hidden
- Program to count how many ways we can divide the tree into two trees in Python
- Program to count how many ways we can cut the matrix into k pieces in python
- Python Program to find out how many times the balls will collide in a circular tube
- Program to find how many ways we can climb stairs in Python
- Program to find maximum how many water bottles we can drink in Python
- Program to find how many total amount of rain we can catch in Python
