


You have a special type of car containing any number of seats.
The first line contains a single integer ‘T’, denoting the number of test cases.
Each test case’s first line contains ‘C’ and ‘N’, denoting the car’s capacity and the number of trips you have to make.
The next ‘N’ lines contain 3 integers, each denoting the number of passengers, pick up point, and drop point of the trip respectively.
For each test case, print “True” if possible to make the trip and “False” otherwise.
Print the output of each test case in a separated line.
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= C <= 10^5
1 <= N <= 10^3
0 <= passengers <= 100
0 <= pickPoint, dropPoint <= 1000
Where ‘T’ is the number of test cases, ‘C’ is the car’s capacity, and “N’ is the number of trips you have to make.
Time limit: 1 sec.
The simple idea is that we will keep a record of the number of passengers changed at every time in a hash map ‘passengers’ where the key represents the time at which the number of passengers changed and the value at that key represents the count of passengers changed.
For Example- If we have trips = [ 2, 1,4 ], [ 2, 2,5 ] then at time = 1, no of passengers changed and increased by 2. So passengers[ 1 ] = 2. At time = 4, these passengers reach their destination so at time = 4, no of passengers get reduced by 2. So passengers[ 4 ] = -2.
Similarly passengers[ 2 ] = 2 and passengers[ 5 ] = -2.
In the hash map, we will increment the value at the pickup point of the ‘i-th’ trip and decrement the value at the drop point of the ‘i-th’ trip by the number of passengers in the ‘i-th’ trip.
As in the hash map, keys are stored in sorted order. So adding the values of this hash map will give the number of passengers at every key time. If no of the passengers exceeds the capacity of the car at any time, we will return false.
Algorithm
The range for staring and ending location varies from 0 to 1000 which is not a large range.
We will solve the problem as we did in the previous approach, but instead of using a hash map, we will take a 1-D array ‘passengers’ where passengers[ i ] denotes the number of passengers changed at ‘i-th’ time.
By doing this, we can update every trip detail in a constant time which was taking (log(N)) time in case of hash map.
Algorithm