## Similar Problems

Similar Problems not available

# Last Person To Fit In The Bus - Leetcode Solution

LeetCode: Last Person To Fit In The Bus Leetcode Solution

Difficulty: Medium

Topics: database

Problem Statement:

There are n people who want to take the bus, and their weight is represented by an integer array people where people[i] is the weight of the ith person.

A bus has a weight limit of maxCapacity units of weight.

The driver will take the bus on a route, where there are m bus stops. At the ith stop, some number of people (ai) will leave the bus, and some number of people (bi) will enter the bus.

The bus can only leave the stop if it's not overloaded (i.e. the sum of the weights of all the people on the bus and the weight of the driver must be less than or equal to maxCapacity). If the bus is overloaded, some people need to leave the bus and wait for the next one.

The bus is empty at the first bus stop.

Implement a function to find the index of the last stop where the bus can stop without being overloaded. Return -1 if the bus can't stop at any stop.

Input:

- An integer array people of length n (1 ≤ n ≤ 10^4) representing the weight of people
- An integer maxCapacity (1 ≤ maxCapacity ≤ 10^5) representing the maximum weight capacity of the bus
- A list of tuples stops where each tuple contains two integers (ai, bi) representing the number of people who will leave and enter the bus respectively at the ith stop (0 ≤ ai, bi ≤ 10^4)

Output:

- An integer representing the index of the last stop where the bus can stop without being overloaded. Return -1 if the bus can't stop at any stop.

Example: Input: people = [1,2,1,1,1], maxCapacity = 3, stops = [(0, 1), (2, 1), (4, 0)] Output: 2 Explanation: At the first stop, the number of people on the bus is 1. At the second stop, the number of people on the bus is 2 + 1 - 2 = 1. At the third stop, the number of people on the bus is 1 + 1 - 4 = -2, which means the bus is overloaded.

Solution:

To solve this problem, we need to simulate the process of the bus going through all the stops and check the number of people on the bus at each stop. If the number of people on the bus exceeds the maximum capacity, some people need to leave the bus.

We can keep track of the number of people on the bus using a variable busCapacity. We can loop through all the stops and update the busCapacity accordingly. If the busCapacity exceeds the maximum capacity, we need to find out how many people need to leave the bus. We can do this by keeping track of the total number of people who entered the bus and subtracting it from the total number of people who left the bus.

If the bus is overloaded at any stop, we can return the index of the previous stop as that will be the last stop where the bus was not overloaded.

Here is the Python implementation of the solution:

def findLastStop(people, maxCapacity, stops): busCapacity = 0 lastStop = -1 for i in range(len(stops)): # update number of people on the bus busCapacity += stops[i][1] - stops[i][0] # check if the bus is overloaded if busCapacity > maxCapacity: # calculate how many people need to leave the bus totalPeople = sum(x[1] for x in stops[:i+1]) totalPeople -= sum(x[0] for x in stops[:i+1]) needToLeave = busCapacity - maxCapacity # find the index of the last stop where the bus was not overloaded for j in range(i, -1, -1): if stops[j][1] > needToLeave: lastStop = j break needToLeave -= stops[j][1] needToLeave += stops[j][0] break else: lastStop = i return lastStop

This implementation has a time complexity of O(n^2), where n is the number of stops. This is because in the worst case, we need to loop through all the previous stops to find the index of the last stop where the bus was not overloaded. However, this is not a problem for small inputs as n is at most 10^4.

Alternatively, we can use a more efficient approach by keeping track of the running sum of the number of people who entered the bus and the number of people who left the bus. We can then use two pointers to find the index of the last stop where the bus was not overloaded. Here is the Python implementation of this solution:

def findLastStop(people, maxCapacity, stops): busCapacity = 0 lastStop = -1 totalPeople = 0 for i in range(len(stops)): # update number of people on the bus busCapacity += stops[i][1] - stops[i][0] # update total number of people totalPeople += stops[i][1] - stops[i][0] # check if the bus is overloaded if busCapacity > maxCapacity: # find the index of the last stop where the bus was not overloaded while busCapacity > maxCapacity: totalPeople -= stops[lastStop+1][1] - stops[lastStop+1][0] busCapacity -= stops[lastStop+1][1] - stops[lastStop+1][0] lastStop += 1 # check if the bus can leave at the current stop if i < len(stops) - 1: if totalPeople + stops[i+1][1] - stops[i+1][0] > maxCapacity: return -1 else: lastStop = i return lastStop

This implementation has a time complexity of O(n), where n is the number of stops. This is because we only need to loop through all the stops once and use two pointers to find the index of the last stop where the bus was not overloaded.

## Last Person To Fit In The Bus Solution Code

`1`