Hungry Ninja

Hard
0/120
Average time to solve is 40m
profile
Contributed by
0 upvote
Asked in company
SAP Labs

Problem statement

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.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
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.
Constraints :
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
Sample Input 1 :
2
5 3
0 3 1
0 4 3
4 4 1
5 2
0 2 4
1 4 4
Sample Output 1 :
2
8
Explanation for Sample Input 1 :
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.
Sample Input 2 :
1
5 3
0 4 5
0 4 4
0 4 2
Sample Output 2 :
2
Hint

Can you find the minimum cost required to cover all previous dishes before ‘i-th’ dish.

Approaches (2)
Bruteforce

 

APPROACH :

 

  • Let us try to solve this problem for each range separately, i.e. minimum cost required to eat till ‘ith’ dish (where the cost of eating i is included).
  • We will try a dynamic programming approach to find the minimum cost for each ‘ith’ range. For each range ‘DP[i]’ stores the minimum cost to eat range ‘i’.
  • Now we will sort all ranges based on the starting point of their start, for each range only the ranges that start before the current can contribute to minimum cost. All other ranges that have starting points larger than or equal to the current range would require the same smaller ranges to reach their starting point.
  • Now all the ranges starting from ‘1’ have ‘DP[i]’ as ‘C[i]’ as ‘0’ is the starting point of overall ranges.
    • After getting ‘DP[i]’ (the minimum cost to reach ‘ith’ range), we will iterate over all following ranges that can be reached using our current range, now we will update all ‘DP[j]’ (where ‘j’ is the range which can be reached after reaching ‘ith’ range) as the minimum of ‘DP[j]’ and ‘C[j] + DP[i]’, Cost of eating ‘jth’ range + minimum cost to eat ‘ith’ range.
  • To all ‘i’ we will always have the minimum cost to eat ‘ith’ range in ‘DP[i]’.
  • For all ‘i’ which can cover till ‘N-1’ we will choose one with minimum ‘DP[i]’ as ‘ANS’.


 

ALGORITHM :

 

  • Create an array ‘DP’ of size ‘M’ and initialise all ‘DP’ values with infinity (very large) value and variable ‘ANS’ as infinity.
  • Bind values of arrays ‘L’, ‘R’ and ‘C’ in array ‘VALUE’ and sort the ‘VALUE’ array in ascending order based on ‘L[i]’.
  • Start iterating over the ‘VALUE’ array, if ‘L[i]’ is 0, set ‘DP[i]’ to ‘C[i]’.
    • Start another loop, where ‘j = i + 1’ and run until ‘L[j] <= R[i] + 1’, and update ‘DP[j]’ to the minimum value of ‘DP[j]’ or ‘DP[i] + C[j]’.
  • Iterate over ‘DP’ if ‘R[i]’ equals to ‘N-1’, set ‘ANS’ as the minimum of ‘ANS’ or ‘DP[i]’
  • return ‘ANS’
Time Complexity

 

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). 

Space Complexity

 

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).


 

Code Solution
(100% EXP penalty)
Hungry Ninja
Full screen
Console