intmaximalNetworkRank(int n, int** roads, int roadsSize, int* roadsColSize) { bool connect[n][n]; int degree[n]; memset(connect, 0, sizeof(connect)); memset(degree, 0, sizeof(degree)); for (int i = 0; i < roadsSize; i++) { int x = roads[i][0], y = roads[i][1]; connect[x][y] = true; connect[y][x] = true; degree[x]++; degree[y]++; }
int maxRank = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int rank = degree[i] + degree[j] - (connect[i][j] ? 1 : 0); maxRank = MAX(maxRank, rank); } } return maxRank; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var maximalNetworkRank = function(n, roads) { const connect = newArray(n).fill(0).map(() =>newArray(n).fill(0)); const degree = newArray(n).fill(0); for (const v of roads) { connect[v[0]][v[1]] = true; connect[v[1]][v[0]] = true; degree[v[0]]++; degree[v[1]]++; }
let maxRank = 0; for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) { let rank = degree[i] + degree[j] - (connect[i][j] ? 1 : 0); maxRank = Math.max(maxRank, rank); } } return maxRank; };
classSolution { public: intmaximalNetworkRank(int n, vector<vector<int>>& roads){ vector<vector<bool>> connect(n, vector<bool>(n, false)); vector<int> degree(n); for (auto road : roads) { int x = road[0], y = road[1]; connect[x][y] = true; connect[y][x] = true; degree[x]++; degree[y]++; }
int first = -1, second = -2; vector<int> firstArr, secondArr; for (int i = 0; i < n; ++i) { if (degree[i] > first) { second = first; secondArr = firstArr; first = degree[i]; firstArr.clear(); firstArr.emplace_back(i); } elseif (degree[i] == first) { firstArr.emplace_back(i); } elseif (degree[i] > second){ secondArr.clear(); second = degree[i]; secondArr.emplace_back(i); } elseif (degree[i] == second) { secondArr.emplace_back(i); } } if (firstArr.size() == 1) { int u = firstArr[0]; for (int v : secondArr) { if (!connect[u][v]) { return first + second; } } return first + second - 1; } else { int m = roads.size(); if (firstArr.size() * (firstArr.size() - 1) / 2 > m) { return first * 2; } for (int u: firstArr) { for (int v: firstArr) { if (u != v && !connect[u][v]) { return first * 2; } } } return first * 2 - 1; } } };
publicclassSolution { publicintMaximalNetworkRank(int n, int[][] roads) { bool[][] connect = newbool[n][]; for (int i = 0; i < n; i++) { connect[i] = newbool[n]; } int[] degree = newint[n]; foreach (int[] road in roads) { int x = road[0], y = road[1]; connect[x][y] = true; connect[y][x] = true; degree[x]++; degree[y]++; }
int first = -1, second = -2; IList<int> firstArr = new List<int>(); IList<int> secondArr = new List<int>(); for (int i = 0; i < n; ++i) { if (degree[i] > first) { second = first; secondArr = new List<int>(firstArr); first = degree[i]; firstArr.Clear(); firstArr.Add(i); } elseif (degree[i] == first) { firstArr.Add(i); } elseif (degree[i] > second){ secondArr.Clear(); second = degree[i]; secondArr.Add(i); } elseif (degree[i] == second) { secondArr.Add(i); } } if (firstArr.Count == 1) { int u = firstArr[0]; foreach (int v in secondArr) { if (!connect[u][v]) { return first + second; } } return first + second - 1; } else { int m = roads.Length; if (firstArr.Count * (firstArr.Count - 1) / 2 > m) { return first * 2; } foreach (int u in firstArr) { foreach (int v in firstArr) { if (u != v && !connect[u][v]) { return first * 2; } } } return first * 2 - 1; } } }
intmaximalNetworkRank(int n, int** roads, int roadsSize, int* roadsColSize) { bool connect[n][n]; int degree[n]; memset(connect, 0, sizeof(connect)); memset(degree, 0, sizeof(degree)); for (int i = 0; i < roadsSize; i++) { int x = roads[i][0], y = roads[i][1]; connect[x][y] = true; connect[y][x] = true; degree[x]++; degree[y]++; }
int first = -1, second = -2; int firstArr[n], secondArr[n]; int firstArrSize = 0, secondArrSize = 0; for (int i = 0; i < n; ++i) { if (degree[i] > first) { second = first; secondArrSize = firstArrSize; memcpy(secondArr, firstArr, sizeof(int) * firstArrSize); first = degree[i]; firstArrSize = 0; firstArr[firstArrSize++] = i; } elseif (degree[i] == first) { firstArr[firstArrSize++] = i; } elseif (degree[i] > second){ secondArrSize = 0; second = degree[i]; secondArr[secondArrSize++] = i; } elseif (degree[i] == second) { secondArr[secondArrSize++] = i; } } if (firstArrSize == 1) { int u = firstArr[0]; for (int i = 0; i < secondArrSize; i++) { int v = secondArr[i]; if (!connect[u][v]) { return first + second; } } return first + second - 1; } else { if (firstArrSize * (firstArrSize - 1) / 2 > roadsSize) { return first * 2; } for (int i = 0; i < firstArrSize; i++) { int u = firstArr[i]; for (int j = i + 1; j < firstArrSize; j++) { int v = firstArr[j]; if (!connect[u][v]) { return first * 2; } } } return first * 2 - 1; } }
var maximalNetworkRank = function(n, roads) { const connect = newArray(n).fill(0).map(() =>newArray(n).fill(0)); const degree = newArray(n).fill(0); for (const road of roads) { let x = road[0], y = road[1]; connect[x][y] = true; connect[y][x] = true; degree[x]++; degree[y]++; }
let first = -1, second = -2; let firstArr = []; let secondArr = []; for (let i = 0; i < n; ++i) { if (degree[i] > first) { second = first; secondArr = [...firstArr]; first = degree[i]; firstArr = [i]; } elseif (degree[i] === first) { firstArr.push(i); } elseif (degree[i] > second){ secondArr = []; second = degree[i]; secondArr.push(i); } elseif (degree[i] === second) { secondArr.push(i); } } if (firstArr.length === 1) { const u = firstArr[0]; for (const v of secondArr) { if (!connect[u][v]) { return first + second; } } return first + second - 1; } else { const m = roads.length; if (firstArr.length * (firstArr.length - 1) / 2 > m) { return first * 2; } for (const u of firstArr) { for (const v of firstArr) { if (u !== v && !connect[u][v]) { return first * 2; } } } return first * 2 - 1; } };