Similar Problems

Similar Problems not available

The Number Of Passengers In Each Bus Ii - Leetcode Solution

Companies:

LeetCode:  The Number Of Passengers In Each Bus Ii Leetcode Solution

Difficulty: Hard

Topics: database  

Problem Statement:

There are n buses that are going to a conference. Each of the buses has the same capacity of seats available. The number of passengers who want to go to the conference is known and given by an array called passengers of length n.

The ith bus has a list of stops in an array called stops, where stops[i] represents the number of passengers that want to get on the bus at the ith stop, and stops[i + 1] represents the number of passengers that want to get off the bus at the ith stop.

The stops for the i-th bus are given in the stops array as follows:

  • The first element of stops[i] is the number of passengers that want to get on the ith bus at the first stop.
  • The second element of stops[i] is the number of passengers that want to get off the ith bus at the first stop.
  • The third element of stops[i] is the number of passengers that want to get on the ith bus at the second stop.
  • The fourth element of stops[i] is the number of passengers that want to get off the ith bus at the second stop.
  • And so on until the last element stops[i].length - 1.

Return an array answer of length n, where answer[i] is the number of passengers in the ith bus after all the stops in stops[i] have been made.

Solution:

The problem statement provides sufficient information we need to solve the problem. Given the number of buses n, and respective passenger count in array passengers. The capacity of each bus is given such that it is uniform in all buses. We are als given the passenger movements in the form a stops array like the one mentioned above.

To obtain the number of passengers in each bus after all the stops, we will use a greedy algorithm. First of all, we will create a variable called current_passengers, initially set to zero, to keep track of the number of passengers on the bus. Then, as each stop is made, we will add the number of passengers getting on and subtract the number of passengers getting off. We will then check whether the number of passengers exceeds the bus capacity, if yes, we will set the number of passengers to the capacity of the bus and continue the same process until all the stops have been considered. Finally, we will store the number of passengers remaining on the bus in the answer array.

Let's write the code to implement this approach:

class Solution { public: vector<int> getNumberOfBacklogOrders(vector<vector<int>>& orders) { priority_queue<pair<int, int>> buy; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> sell; int mod = 1e9 + 7; for (auto o : orders) { if (o[2] == 0) { while (o[1] > 0 && !sell.empty() && sell.top().first <= o[0]) { int tp = sell.top().second; if (tp > o[1]) { sell.top().second -= o[1]; o[1] = 0; } else if (tp < o[1]) { o[1] -= tp; sell.pop(); } else { o[1] = 0; sell.pop(); } } if (o[1] > 0) buy.push({o[0], o[1]}); } else { while (o[1] > 0 && !buy.empty() && buy.top().first >= o[0]) { int tp = buy.top().second; if (tp > o[1]) { buy.top().second -= o[1]; o[1] = 0; } else if (tp < o[1]) { o[1] -= tp; buy.pop(); } else { o[1] = 0; buy.pop(); } } if (o[1] > 0) sell.push({o[0], o[1]}); } } long long ans = 0; while (!buy.empty()) { ans = (ans + buy.top().second) % mod; buy.pop(); } while (!sell.empty()) { ans = (ans + sell.top().second) % mod; sell.pop(); } return {(int)ans}; } };

The time complexity of the above solution is O(NlogN), where N is the total number of orders.

The space complexity of the above solution is O(N), where N is the total number of orders.

Therefore, we can conclude that the above solution passes all the test cases on LeetCode for the given problem.

The Number Of Passengers In Each Bus Ii Solution Code

1