- 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 check whether we can pick up and drop every passenger in given list in Python
Suppose we have a matrix called requested_trips where each row containing [start_x, end_x, num_passengers], and we also have a capacity value. Now each requested trip asks to pick up num_passengers passenger at start_x and drop them off at end_x. We also have a car with the capacity that is given, and start at position x = 0. We want to to pick up every passenger and can only move right side, we have to check whether we can pick up and drop off everyone.
So, if the input is like trips = [[1, 25, 2], [3, 4, 3],[5, 12, 3]] capacity = 6, then the output will be True
To solve this, we will follow these steps −
events := a new list
for each set (sx, ex, np) in trips, do
insert pair (sx, np) at the end of events
insert pair (ex, −np) at the end of events
carrying := 0
for each pair (loc, delta) in the list of events (in sorted order), do
carrying := carrying + delta
if carrying > capacity, then
return False
return True
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, trips, capacity): events = [] for sx, ex, np in trips: events.append((sx, np)) events.append((ex, -np)) carrying = 0 for loc, delta in sorted(events): carrying += delta if carrying > capacity: return False return True ob = Solution() trips = [ [1, 25, 2], [3, 4, 3], [5, 12, 3] ] capacity = 6 print(ob.solve(trips, capacity))
Input
trips = [ [1, 25, 2], [3, 4, 3], [5, 12, 3] ] capacity = 6
Output
True
- Related Articles
- Python program to check whether we can pile up cubes or not
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Program to find minimum cost to pick up gold in given two locations in Python
- Program to check whether given list is in valid state or not in Python
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check whether we can form 24 by placing operators in python
- Program to check whether we can reach last position from index 0 in Python
- Program to check whether we can get N queens solution or not in Python
- Program to check whether we can convert string in K moves or not using Python
- Program to check whether we can eat favorite candy on our favorite day in Python
- Program to check whether we can split a string into descending consecutive values in Python
- Program to check whether we can partition a list with k-partitions of equal sum in C++
- Program to check whether list is alternating increase and decrease or not in Python
