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 minimum number of rocketships needed for rescue in Python
Suppose we have a list of numbers called weights representing people's weights and a value limit that determines the weight limit of one rocket ship. Each rocket ship can take at most two people. We need to find the minimum number of rocket ships required to rescue everyone.
So, if the input is like weights = [300, 400, 300], limit = 600, then the output will be 2. One rocket ship takes the two people with weights 300 each (total 600), and another takes the person with weight 400.
Algorithm Approach
To solve this problem efficiently, we use a greedy approach with two pointers ?
Sort the weights in ascending order
Use two pointers: one at the lightest person (left) and one at the heaviest person (right)
For each rocket ship, always include the heaviest remaining person
If the lightest person can also fit with the heaviest, include them both
Count the number of rocket ships needed
Implementation
def min_rocketships(weights, limit):
weights.sort()
left = 0
right = len(weights) - 1
ships = 0
while left <= right:
# Always take the heaviest person
if left == right:
# Only one person left
ships += 1
break
elif weights[left] + weights[right] <= limit:
# Both lightest and heaviest can go together
left += 1
# Heaviest person goes in this ship
right -= 1
ships += 1
return ships
# Test the function
weights = [300, 400, 300]
limit = 600
result = min_rocketships(weights, limit)
print(f"Minimum rocket ships needed: {result}")
Minimum rocket ships needed: 2
Step-by-Step Execution
Let's trace through the example with weights = [300, 400, 300] and limit = 600 ?
def min_rocketships_verbose(weights, limit):
weights.sort()
print(f"Sorted weights: {weights}")
left = 0
right = len(weights) - 1
ships = 0
while left <= right:
print(f"Ship {ships + 1}: Checking positions {left} and {right}")
print(f"Lightest: {weights[left]}, Heaviest: {weights[right]}")
if left == right:
print(f"Only one person left: {weights[left]}")
ships += 1
break
elif weights[left] + weights[right] <= limit:
print(f"Both can fit: {weights[left]} + {weights[right]} = {weights[left] + weights[right]} <= {limit}")
left += 1
else:
print(f"Only heaviest fits: {weights[right]} (total would be {weights[left] + weights[right]})")
right -= 1
ships += 1
print(f"Ship {ships} dispatched\n")
return ships
# Test with example
weights = [300, 400, 300]
limit = 600
result = min_rocketships_verbose(weights, limit)
print(f"Total ships needed: {result}")
Sorted weights: [300, 300, 400] Ship 1: Checking positions 0 and 2 Lightest: 300, Heaviest: 400 Only heaviest fits: 400 (total would be 700) Ship 1 dispatched Ship 2: Checking positions 0 and 1 Lightest: 300, Heaviest: 300 Both can fit: 300 + 300 = 600 <= 600 Ship 2 dispatched Total ships needed: 2
Key Points
Time Complexity: O(n log n) due to sorting
Space Complexity: O(1) excluding input space
Greedy Strategy: Always pair the lightest available person with the heaviest
Two-Pointer Technique: Efficiently explores all valid pairings
Alternative Test Cases
# Test with different scenarios
test_cases = [
([100, 200, 300, 400, 500], 400),
([50, 50, 50, 50], 100),
([100], 200),
([200, 200, 200], 200)
]
for i, (weights, limit) in enumerate(test_cases, 1):
result = min_rocketships(weights, limit)
print(f"Test {i}: weights={weights}, limit={limit} ? {result} ships")
Test 1: weights=[100, 200, 300, 400, 500], limit=400 ? 3 ships Test 2: weights=[50, 50, 50, 50], limit=100 ? 2 ships Test 3: weights=[100], limit=200 ? 1 ships Test 4: weights=[200, 200, 200], limit=200 ? 3 ships
Conclusion
The greedy two-pointer approach efficiently solves the rocket ship rescue problem by always pairing the lightest and heaviest people when possible. This ensures the minimum number of ships while respecting the weight limit constraint.
