Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 058da14

Browse files
committedApr 23, 2025
Add solution #1615
1 parent 1255f3b commit 058da14

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,421 LeetCode solutions in JavaScript
1+
# 1,422 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1245,6 +1245,7 @@
12451245
1610|[Maximum Number of Visible Points](./solutions/1610-maximum-number-of-visible-points.js)|Hard|
12461246
1611|[Minimum One Bit Operations to Make Integers Zero](./solutions/1611-minimum-one-bit-operations-to-make-integers-zero.js)|Hard|
12471247
1614|[Maximum Nesting Depth of the Parentheses](./solutions/1614-maximum-nesting-depth-of-the-parentheses.js)|Easy|
1248+
1615|[Maximal Network Rank](./solutions/1615-maximal-network-rank.js)|Medium|
12481249
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12491250
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12501251
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* 1615. Maximal Network Rank
3+
* https://leetcode.com/problems/maximal-network-rank/
4+
* Difficulty: Medium
5+
*
6+
* There is an infrastructure of n cities with some number of roads connecting these cities.
7+
* Each roads[i] = [ai, bi] indicates that there is a bidirectional road between cities ai and bi.
8+
*
9+
* The network rank of two different cities is defined as the total number of directly connected
10+
* roads to either city. If a road is directly connected to both cities, it is only counted once.
11+
*
12+
* The maximal network rank of the infrastructure is the maximum network rank of all pairs of
13+
* different cities.
14+
*
15+
* Given the integer n and the array roads, return the maximal network rank of the entire
16+
* infrastructure.
17+
*/
18+
19+
/**
20+
* @param {number} n
21+
* @param {number[][]} roads
22+
* @return {number}
23+
*/
24+
var maximalNetworkRank = function(n, citiesConnections) {
25+
const cityRoadCounts = new Array(n).fill(0);
26+
const directConnections = new Set();
27+
28+
for (const [cityA, cityB] of citiesConnections) {
29+
cityRoadCounts[cityA]++;
30+
cityRoadCounts[cityB]++;
31+
directConnections.add(`${Math.min(cityA, cityB)}-${Math.max(cityA, cityB)}`);
32+
}
33+
34+
let maxNetworkRank = 0;
35+
36+
for (let cityA = 0; cityA < n; cityA++) {
37+
for (let cityB = cityA + 1; cityB < n; cityB++) {
38+
let networkRank = cityRoadCounts[cityA] + cityRoadCounts[cityB];
39+
if (directConnections.has(`${cityA}-${cityB}`)
40+
|| directConnections.has(`${cityB}-${cityA}`)) {
41+
networkRank--;
42+
}
43+
maxNetworkRank = Math.max(maxNetworkRank, networkRank);
44+
}
45+
}
46+
47+
return maxNetworkRank;
48+
};

0 commit comments

Comments
 (0)
Please sign in to comment.