Ninja decided to visit the new Buffet Restaurant in the neighbourhood. There are a total of ‘N’ different dishes, numbered ‘0 to N-1’. But there is a restriction. The ninja cannot make orders on his own will. There are ‘M’ types of orders that he can make. Each Order ‘i’ consists of three values represented by array ‘C’, ‘L’ and ‘R’, which means Ninja can order all dishes numbered ‘L’ to ‘R’ with the cost of ‘C’ Ninja currency.
Ninja wants to taste all ‘N’ dishes, but he wants to spend the minimum amount of Ninja Currency.
Can you help Ninja figure out the minimum amount of Ninja Currency he needs to spend to taste all dishes.
You can assume that for given constraints, answers always exist.
Example :N = 4
M = 3
A = [ [ 0, 2, 1 ] , [ 1, 2, 4 ] , [ 2, 3, 3 ] ]
If Ninja chooses [ 0, 2, 1 ] and [ 2, 3, 3 ], he can taste each dish for 4 Ninja Currency.
The first line contains an integer 'T', denoting the number of test cases.
Then the test cases follow:
The first line of each test case contains two space-separated integers, 'N' and 'M', denoting the total number of dishes and the total number of different orders.
The following ‘M’ line contains three integers ‘Li’, ‘Ri’ and ‘Ci’, denoting Starting point of food, End Point of food and Cost of ordering from ‘Li’ to ‘Ri’.
Output Format :
Print an integer denoting the minimum Ninja Currency required to taste all dishes for each test case.
Print the output of each test case in a new line.
Note :
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= 'T' <= 10
1 <= 'N' <= 10^5
1 <= 'M' <= 10^5
0 <= 'L[i]' <= ‘R[i]’ <= ‘N-1’
1 <= ‘C[i]’ <= 10^4
Sum of 'N' overall test cases doesn’t exceed 10^5.
Time Limit: 1 sec
2
5 3
0 3 1
0 4 3
4 4 1
5 2
0 2 4
1 4 4
2
8
For test case 1:
If Ninja choose [ 0, 3, 1 ] and [ 4, 4, 1 ], the total cost equals 2. No other choice can offer less price to Ninja.
For test case 2:
Ninja only has one choice to select both orders to feed him all dishes, so the total Ninja Currency used is 8.
1
5 3
0 4 5
0 4 4
0 4 2
2
Can you find the minimum cost required to cover all previous dishes before ‘i-th’ dish.
APPROACH :
ALGORITHM :
O(M^2), where ‘M’ is the size of array ‘L.’
We are iterating over array ‘L’ inside another iteration which equals O(M * M).
O(M), where ‘M’ is the size of array ‘L.’
We will use an extra array of ‘VALUES’ which takes O(3 * M) space which is O(M).