Skip to content

Commit 1ea56f9

Browse files
committed
Runtime: 443 ms (Top 37.00%) | Memory: 86.7 MB (Top 40.00%)
1 parent 87d1974 commit 1ea56f9

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
1+
// Runtime: 443 ms (Top 37.00%) | Memory: 86.7 MB (Top 40.00%)
12
var maximumImportance = function(n, roads) {
23
const connectionCount = Array(n).fill(0)
3-
4+
45
// Count the connections from each city
56
// e.g. the 0th city's count will be stored at index zero in the array
67
for (let [cityTo, cityFrom] of roads) {
78
connectionCount[cityTo]++
89
connectionCount[cityFrom]++
910
}
10-
11+
1112
let cityToConnectionCount = []
1213
for (let city = 0; city < n; city++) {
1314
cityToConnectionCount.push([city, connectionCount[city]])// Store the [city, numberOfConnections]
1415
}
15-
16+
1617
// Created new array(sortedCities) for readability
17-
const sortedCities = cityToConnectionCount.sort((a,b) => b[1] - a[1])// sort by number of connections, the city with the greatest number of connections should be
18+
const sortedCities = cityToConnectionCount.sort((a,b) => b[1] - a[1])// sort by number of connections, the city with the greatest number of connections should be
1819
// the city with the greatest importance
19-
20+
2021
const values = Array(n).fill(0)
2122
let importance = n
2223
for (let i = 0; i < sortedCities.length; i++) {
2324
const [city, connectionCount] = cityToConnectionCount[i]
2425
values[city] = importance// City at the 0th position array is should be the city with the greatest importance
2526
importance--
2627
}
27-
28+
2829
// Sum the importance of each city, toCity => fromCity
2930
let res = 0
3031
for (let [to, from] of roads) {
31-
res += values[to] + values[from]
32+
res += values[to] + values[from]
3233
}
33-
34+
3435
return res
3536
};```

0 commit comments

Comments
 (0)