-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2093-minimum-cost-to-reach-city-with-discounts.js
More file actions
59 lines (51 loc) · 2.03 KB
/
2093-minimum-cost-to-reach-city-with-discounts.js
File metadata and controls
59 lines (51 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* 2093. Minimum Cost to Reach City With Discounts
* https://leetcode.com/problems/minimum-cost-to-reach-city-with-discounts/
* Difficulty: Medium
*
* A series of highways connect n cities numbered from 0 to n - 1. You are given a 2D
* integer array highways where highways[i] = [city1i, city2i, tolli] indicates that
* there is a highway that connects city1i and city2i, allowing a car to go from city1i
* to city2i and vice versa for a cost of tolli.
*
* You are also given an integer discounts which represents the number of discounts you have.
* You can use a discount to travel across the ith highway for a cost of tolli / 2 (integer
* division). Each discount may only be used once, and you can only use at most one discount
* per highway.
*
* Return the minimum total cost to go from city 0 to city n - 1, or -1 if it is not possible
* to go from city 0 to city n - 1.
*/
/**
* @param {number} n
* @param {number[][]} highways
* @param {number} discounts
* @return {number}
*/
var minimumCost = function(n, highways, discounts) {
const graph = Array.from({ length: n }, () => []);
for (const [a, b, c] of highways) {
graph[a].push([b, c]);
graph[b].push([a, c]);
}
const pq = new PriorityQueue((a, b) => a[0] - b[0]);
pq.enqueue([0, 0, 0]);
const visited = Array.from({ length: n }, () => new Array(discounts + 1).fill(Infinity));
visited[0][0] = 0;
while (!pq.isEmpty()) {
const [cost, city, discountUsed] = pq.dequeue();
if (city === n - 1) return cost;
for (const [nextCity, weight] of graph[city]) {
if (cost + weight < visited[nextCity][discountUsed]) {
pq.enqueue([cost + weight, nextCity, discountUsed]);
visited[nextCity][discountUsed] = cost + weight;
}
if (discountUsed < discounts
&& cost + Math.floor(weight / 2) < visited[nextCity][discountUsed + 1]) {
pq.enqueue([cost + Math.floor(weight / 2), nextCity, discountUsed + 1]);
visited[nextCity][discountUsed + 1] = cost + Math.floor(weight / 2);
}
}
}
return -1;
};