- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find minimum number of rocketships needed for rescue in Python
Suppose we have a list of numbers called weights this is representing peoples' weights and a value limit determines the weight limit of one rocket ship. Now each rocketship can take at most two people. We have to find the minimum number of rocket ships it would take to rescue everyone to Planet.
So, if the input is like weights = [300, 400, 300], limit = 600, then the output will be 2, as it will take one rocket ship to take the two people whose weights are 300 each, and another to take the person whose weight is 400.
To solve this, we will follow these steps −
sort the list weights
cnt := 0
while weights is non-empty, do
x := delete last element from weights
if weights is not empty and weights[0] <= limit − x, then
delete first element from weights
cnt := cnt + 1
return cnt
Let us see the following implementation to get better understanding −
Example(Python)
class Solution: def solve(self, weights, limit): weights.sort() cnt = 0 while weights: x = weights.pop() if weights and weights[0] <= limit - x: weights.pop(0) cnt += 1 return cnt ob = Solution() weights = [300, 400, 300] limit = 600 print(ob.solve(weights, limit))
Input
[300, 400, 300], 600
Output
2
- Related Articles
- Program to check minimum number of characters needed to make string palindrome in Python
- Python Program for Find minimum sum of factors of number
- Program to find minimum swaps needed to group all 1s together in Python
- Program to find minimum element addition needed to get target sum in Python
- C++ program to find minimum number of steps needed to move from start to end
- C++ program to find minimum how many operations needed to make number 0
- Program to find minimum costs needed to fill fruits in optimized way in Python
- Program to find minimum number of swaps needed to arrange all pair of socks together in C++
- Program to find minimum amount needed to be paid all good performers in Python
- Program to find minimum operations needed to make two arrays sum equal in Python
- C++ program to find minimum number of punches are needed to make way to reach target
- Program to find number of coins needed to make the changes in Python
- Program to find minimum jump needed to return from a folder to home in Python
- C++ program to count minimum number of operations needed to make number n to 1
- Find the minimum number of moves needed to move from one cell of matrix to another in Python
