Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
There is an infrastructure of n cities with some number of roads connecting these cities. Each roads[i] = [ai, bi] indicates that there is a bidirectional road between cities ai and bi.
The network rank of two different cities is defined as the total number of directly connected roads to either city. If a road is directly connected to both cities, it is only counted once.
The maximal network rank of the infrastructure is the maximum network rank of all pairs of different cities.
Given the integer n and the array roads, return the maximal network rank of the entire infrastructure.
Example 1:
Input: n = 4, roads = [[0,1],[0,3],[1,2],[1,3]] Output: 4 Explanation: The network rank of cities 0 and 1 is 4 as there are 4 roads that are connected to either 0 or 1. The road between 0 and 1 is only counted once.
Example 2:
Input: n = 5, roads = [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]] Output: 5 Explanation: There are 5 roads that are connected to cities 1 or 2.
Example 3:
Input: n = 8, roads = [[0,1],[1,2],[2,3],[2,4],[5,6],[5,7]] Output: 5 Explanation: The network rank of 2 and 5 is 5. Notice that all the cities do not have to be connected.
Constraints:
2 <= n <= 1000 <= roads.length <= n * (n - 1) / 2roads[i].length == 20 <= ai, bi <= n-1ai != biProblem summary: There is an infrastructure of n cities with some number of roads connecting these cities. Each roads[i] = [ai, bi] indicates that there is a bidirectional road between cities ai and bi. The network rank of two different cities is defined as the total number of directly connected roads to either city. If a road is directly connected to both cities, it is only counted once. The maximal network rank of the infrastructure is the maximum network rank of all pairs of different cities. Given the integer n and the array roads, return the maximal network rank of the entire infrastructure.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
4 [[0,1],[0,3],[1,2],[1,3]]
5 [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]]
8 [[0,1],[1,2],[2,3],[2,4],[5,6],[5,7]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1615: Maximal Network Rank
class Solution {
public int maximalNetworkRank(int n, int[][] roads) {
int[][] g = new int[n][n];
int[] cnt = new int[n];
for (var r : roads) {
int a = r[0], b = r[1];
g[a][b] = 1;
g[b][a] = 1;
++cnt[a];
++cnt[b];
}
int ans = 0;
for (int a = 0; a < n; ++a) {
for (int b = a + 1; b < n; ++b) {
ans = Math.max(ans, cnt[a] + cnt[b] - g[a][b]);
}
}
return ans;
}
}
// Accepted solution for LeetCode #1615: Maximal Network Rank
func maximalNetworkRank(n int, roads [][]int) (ans int) {
g := make([][]int, n)
cnt := make([]int, n)
for i := range g {
g[i] = make([]int, n)
}
for _, r := range roads {
a, b := r[0], r[1]
g[a][b], g[b][a] = 1, 1
cnt[a]++
cnt[b]++
}
for a := 0; a < n; a++ {
for b := a + 1; b < n; b++ {
ans = max(ans, cnt[a]+cnt[b]-g[a][b])
}
}
return
}
# Accepted solution for LeetCode #1615: Maximal Network Rank
class Solution:
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
g = defaultdict(set)
for a, b in roads:
g[a].add(b)
g[b].add(a)
ans = 0
for a in range(n):
for b in range(a + 1, n):
if (t := len(g[a]) + len(g[b]) - (a in g[b])) > ans:
ans = t
return ans
// Accepted solution for LeetCode #1615: Maximal Network Rank
struct Solution;
impl Solution {
fn maximal_network_rank(n: i32, roads: Vec<Vec<i32>>) -> i32 {
let n = n as usize;
let mut adj: Vec<Vec<bool>> = vec![vec![false; n]; n];
let mut size: Vec<usize> = vec![0; n];
for road in roads {
let i = road[0] as usize;
let j = road[1] as usize;
adj[i][j] = true;
adj[j][i] = true;
size[i] += 1;
size[j] += 1;
}
let mut res = 0;
for i in 0..n {
for j in i + 1..n {
res = res.max(size[i] + size[j] - if adj[i][j] { 1 } else { 0 });
}
}
res as i32
}
}
#[test]
fn test() {
let n = 4;
let roads = vec_vec_i32![[0, 1], [0, 3], [1, 2], [1, 3]];
let res = 4;
assert_eq!(Solution::maximal_network_rank(n, roads), res);
let n = 5;
let roads = vec_vec_i32![[0, 1], [0, 3], [1, 2], [1, 3], [2, 3], [2, 4]];
let res = 5;
assert_eq!(Solution::maximal_network_rank(n, roads), res);
let n = 8;
let roads = vec_vec_i32![[0, 1], [1, 2], [2, 3], [2, 4], [5, 6], [5, 7]];
let res = 5;
assert_eq!(Solution::maximal_network_rank(n, roads), res);
}
// Accepted solution for LeetCode #1615: Maximal Network Rank
function maximalNetworkRank(n: number, roads: number[][]): number {
const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
const cnt: number[] = Array(n).fill(0);
for (const [a, b] of roads) {
g[a][b] = 1;
g[b][a] = 1;
++cnt[a];
++cnt[b];
}
let ans = 0;
for (let a = 0; a < n; ++a) {
for (let b = a + 1; b < n; ++b) {
ans = Math.max(ans, cnt[a] + cnt[b] - g[a][b]);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.