You are working as a cab driver. Your car moves in a straight line and moves toward the forward direction only. Initially, you have ‘C’ empty seats for the passengers.
Now, you are given ‘N’ number of trips that you have to make. In each trip, you are given three integers ‘Num’, ‘pickPoint’, and ‘dropPoint’ denoting that there are ‘Num’ numbers of passengers standing at 'pickpoint’ and you have to drop them at 'droppoint’.
Your task is to find if it is possible to pick up and drop off all the passengers of all the given trips or not.
Note :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.
Output Format :
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.
Note :
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.
2
3 2
2 1 5
3 5 7
4 2
2 1 5
3 3 7
True
False
Test Case 1 :
At point 1 :
We have 2 passengers. So, two seats will be filled.
Now Empty seats: 1
At point 5:
Two passengers descend from the car. So Empty seats = 3.
At the same time, 3 passengers ascend on the car. So Empty seats = 0.
At point 7:
All three passengers descend from the car. Now empty seats = 3.
So, it is possible to complete the trips, hence print True.

Test Case 2 :
The capacity of car: 4. At position 3, we have one seat empty but 3 passengers to pick, so this trip is not possible hence print False.
2
5 2
2 1 5
3 3 7
11 3
3 2 7
3 7 9
8 3 9
True
True
Think of the number of passengers changed at any time.
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
O( N*log(N) ), where ‘N’ is the number of trips.
We are storing every trip value in a hash map that will take O(N*log(N)) time and then iterating through the hash map that will cost O(N) time. So the overall time complexity is O(N) + O(N*log(N)) = O(N*log(N)).
O(N), where ‘N’ is the number of trips.
We are using a hash map to store every trip’s details, making space complexity O(N).