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 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 contains [start_x, end_x, num_passengers], and we also have a capacity value. Each requested trip asks to pick up num_passengers passenger at start_x and drop them off at end_x. We have a car with the given capacity that starts at position x = 0. The car can only move to the right side, and we need to check whether we can pick up and drop off everyone without exceeding the capacity.
So, if the input is like trips = [[1, 25, 2], [3, 4, 3], [5, 12, 3]] and capacity = 6, then the output will be True.
Algorithm
To solve this problem, we will follow these steps ?
events:= a new list-
for each set
(sx, ex, np)in trips, doinsert pair
(sx, np)at the end of eventsinsert pair
(ex, -np)at the end of events
carrying:= 0-
for each pair
(loc, delta)in the list of events (in sorted order), docarrying:= carrying + delta-
if carrying > capacity, then
return False
return True
Example
Let us see the following implementation to get better understanding ?
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))
True
How It Works
The algorithm uses an event-based approach:
Events Creation: For each trip, we create two events - pickup at start location (positive passengers) and drop-off at end location (negative passengers)
Sorting: Events are sorted by location to simulate the car's movement from left to right
Capacity Check: At each event, we update the current passenger count and check if it exceeds capacity
Step-by-Step Execution
trips = [[1, 25, 2], [3, 4, 3], [5, 12, 3]]
capacity = 6
# Events created: [(1, 2), (25, -2), (3, 3), (4, -3), (5, 3), (12, -3)]
# After sorting: [(1, 2), (3, 3), (4, -3), (5, 3), (12, -3), (25, -2)]
events = []
for sx, ex, np in trips:
events.append((sx, np))
events.append((ex, -np))
print("Events:", sorted(events))
carrying = 0
for loc, delta in sorted(events):
carrying += delta
print(f"Location {loc}: carrying = {carrying}")
if carrying > capacity:
print("Capacity exceeded!")
break
Events: [(1, 2), (3, 3), (4, -3), (5, 3), (12, -3), (25, -2)] Location 1: carrying = 2 Location 3: carrying = 5 Location 4: carrying = 2 Location 5: carrying = 5 Location 12: carrying = 2 Location 25: carrying = 0
Conclusion
This event-based solution efficiently determines if all passengers can be transported within capacity constraints. The algorithm has O(n log n) time complexity due to sorting, where n is the number of trips.
