Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find maximum number of boxes we can fit inside another boxes in python
Suppose we have a list of boxes where each box is represented by [width, height]. We can put one box inside another box only if both the width and height of the first box are smaller than the second box. We need to find the maximum number of boxes we can nest inside each other.
Problem Example
Given the following boxes ?
| Width | Height |
|---|---|
| 12 | 12 |
| 10 | 10 |
| 6 | 6 |
| 5 | 10 |
The output will be 3, as we can fit the box [6, 6] inside [10, 10] which can be put into [12, 12] box.
Algorithm Approach
This problem can be solved using dynamic programming with binary search optimization ?
- Sort boxes by width (ascending), and by height (descending) for same widths
- Use binary search to find the longest increasing subsequence of heights
- Maintain an array to track the smallest height at each nesting level
Implementation
class Solution:
def solve(self, matrix):
# Sort by width ascending, height descending for same width
matrix = sorted(matrix, key=lambda x: (x[0], -x[1]))
n = len(matrix)
# heights[i] stores smallest height for nesting level i
heights = [float("inf")] * (n + 1)
heights[0] = float("-inf")
res = 0
for box in matrix:
cur_w, cur_h = box
# Find position to insert current height
index = self.insert_index(heights, cur_h)
if heights[index] >= cur_h:
heights[index] = cur_h
res = max(res, index)
return res
def insert_index(self, arr, this_h):
"""Binary search to find insertion position"""
left = 0
right = len(arr) - 1
result = 0
while left <= right:
mid = left + (right - left) // 2
cur_h = arr[mid]
if cur_h < this_h:
result = mid
left = mid + 1
else:
right = mid - 1
return result + 1
# Test the solution
ob = Solution()
matrix = [
[12, 12],
[10, 10],
[6, 6],
[5, 10]
]
print("Maximum nested boxes:", ob.solve(matrix))
# Let's trace the algorithm
print("\nStep-by-step:")
print("Original boxes:", matrix)
sorted_matrix = sorted(matrix, key=lambda x: (x[0], -x[1]))
print("Sorted boxes:", sorted_matrix)
Maximum nested boxes: 3 Step-by-step: Original boxes: [[12, 12], [10, 10], [6, 6], [5, 10]] Sorted boxes: [[5, 10], [6, 6], [10, 10], [12, 12]]
How It Works
The algorithm works in these steps ?
- Sorting: Sort boxes by width (ascending) and height (descending) for same widths
- Binary Search: For each box, find the optimal position to place it in the nesting sequence
- Update: Update the heights array to maintain the smallest height at each level
- Track Maximum: Keep track of the maximum nesting level achieved
Another Example
# Test with different box configurations
ob = Solution()
# Example 1: All boxes can be nested
boxes1 = [[1, 1], [2, 2], [3, 3], [4, 4]]
print("Boxes:", boxes1)
print("Max nested:", ob.solve(boxes1))
# Example 2: No boxes can be nested
boxes2 = [[5, 1], [1, 5], [3, 3]]
print("\nBoxes:", boxes2)
print("Max nested:", ob.solve(boxes2))
# Example 3: Partial nesting possible
boxes3 = [[2, 1], [3, 4], [6, 7], [8, 8]]
print("\nBoxes:", boxes3)
print("Max nested:", ob.solve(boxes3))
Boxes: [[1, 1], [2, 2], [3, 3], [4, 4]] Max nested: 4 Boxes: [[5, 1], [1, 5], [3, 3]] Max nested: 1 Boxes: [[2, 1], [3, 4], [6, 7], [8, 8]] Max nested: 3
Time Complexity
- Sorting: O(n log n)
- Binary Search: O(n log n) for n boxes
- Overall: O(n log n)
Conclusion
This solution efficiently finds the maximum number of nested boxes using dynamic programming with binary search optimization. The key insight is sorting boxes properly and using the longest increasing subsequence approach on heights.
