Flight Discount
You are given an integer n representing the number of cities numbered from 1 to n. You are also given a 2D integer array flights where flights[i] = [u, v, price] represents a directed flight from city u to city v costing price.
Your task is to find the minimum total price to travel from city 1 (Syrjälä) to city n (Metsälä). You have exactly one discount coupon that you can use on one flight to halve its price. If you use the coupon on a flight costing x, its new price becomes ⌊x / 2⌋ (rounded down to the nearest integer).
You can assume there is always a valid route from city 1 to city n.
Example 1:
Input: n = 3, flights = [[1, 2, 3], [2, 3, 1], [1, 3, 7], [2, 1, 5]]
Output: 2
Explanation:
The normal route 1 -> 2 -> 3 costs 3 + 1 = 4. If we use the coupon on the flight 1 -> 2, its cost becomes ⌊3/2⌋ = 1. The total cost is 1 + 1 = 2.
Example 2:
Input: n = 4, flights = [[1, 2, 10], [2, 4, 10], [1, 3, 100], [3, 4, 1]]
Output: 15
Explanation:
Route 1 -> 2-> 4 costs 10 + 10. Using the coupon on either flight makes it 5 + 10 = 15.
Route 1 -> 3 -> 4 costs 100 + 1. Using the coupon on the first flight makes it 50 + 1 = 51.
The minimum cost is 15.
Now Your Turn!
Pick the correct output for the given inputInput: n = 3, flights = [[1, 2, 10], [2, 3, 100], [1, 3, 60]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 2 <= n <= 105
- 1 <= flights.length <= 2 x 105
- flights[i].length == 3
- 1 <= flights[i][0], flights[i][1] <= n
- 1 <= flights[i][2] <= 109