Skip to content

Commit 0c9ef97

Browse files
committed
graphs medium
1 parent 6dd55e2 commit 0c9ef97

File tree

1 file changed

+262
-0
lines changed

1 file changed

+262
-0
lines changed

13. Graphs/03. LeetCode Medium.ipynb

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,221 @@
10151015
"Solution().getAncestors(n, edgeList)"
10161016
]
10171017
},
1018+
{
1019+
"attachments": {},
1020+
"cell_type": "markdown",
1021+
"metadata": {},
1022+
"source": [
1023+
"#### 16. [2285]. Maximum Total Importance of Roads"
1024+
]
1025+
},
1026+
{
1027+
"cell_type": "code",
1028+
"execution_count": 387,
1029+
"metadata": {},
1030+
"outputs": [],
1031+
"source": [
1032+
"import collections\n",
1033+
"\n",
1034+
"class Solution:\n",
1035+
" def maximumImportance(self, n: int, roads: List[List[int]]) -> int:\n",
1036+
" indegree = collections.defaultdict(int)\n",
1037+
" for a,b in roads:\n",
1038+
" indegree[b]+=1\n",
1039+
" indegree[a]+=1\n",
1040+
" return sum(map(lambda x:x[0]*x[1] , zip(sorted(indegree.values(), reverse=True), list(range(n,0,-1)))))"
1041+
]
1042+
},
1043+
{
1044+
"cell_type": "code",
1045+
"execution_count": 388,
1046+
"metadata": {},
1047+
"outputs": [
1048+
{
1049+
"data": {
1050+
"text/plain": [
1051+
"20"
1052+
]
1053+
},
1054+
"execution_count": 388,
1055+
"metadata": {},
1056+
"output_type": "execute_result"
1057+
}
1058+
],
1059+
"source": [
1060+
"n = 5; roads = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]\n",
1061+
"n = 5; roads = [[0,3],[2,4],[1,3]]\n",
1062+
"Solution().maximumImportance(n, roads)"
1063+
]
1064+
},
1065+
{
1066+
"attachments": {},
1067+
"cell_type": "markdown",
1068+
"metadata": {},
1069+
"source": [
1070+
"#### 17. 2368. Reachable Nodes With Restrictions"
1071+
]
1072+
},
1073+
{
1074+
"cell_type": "code",
1075+
"execution_count": 415,
1076+
"metadata": {},
1077+
"outputs": [],
1078+
"source": [
1079+
"class Solution:\n",
1080+
" def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:\n",
1081+
" graph = {i:set() for i in range(n)}\n",
1082+
" d = {i:1 for i in restricted}\n",
1083+
" for a,b in edges:\n",
1084+
" graph[a].add(b)\n",
1085+
" graph[b].add(a)\n",
1086+
" stack = collections.deque()\n",
1087+
" stack.append(0)\n",
1088+
" visited = {0:1}\n",
1089+
" res = 1\n",
1090+
" while stack:\n",
1091+
" node = stack.popleft()\n",
1092+
" for keys in graph[node]:\n",
1093+
" if d.get(keys, None) is None:\n",
1094+
" if visited.get(keys,None) is None:\n",
1095+
" stack.append(keys)\n",
1096+
" res += 1\n",
1097+
" visited[keys] = 1 \n",
1098+
" return res\n",
1099+
" "
1100+
]
1101+
},
1102+
{
1103+
"cell_type": "code",
1104+
"execution_count": null,
1105+
"metadata": {},
1106+
"outputs": [],
1107+
"source": [
1108+
"class Solution:\n",
1109+
" def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:\n",
1110+
" neighbors = collections.defaultdict(list)\n",
1111+
" for node_a, node_b in edges:\n",
1112+
" neighbors[node_a].append(node_b)\n",
1113+
" neighbors[node_b].append(node_a)\n",
1114+
" seen = [False] * n\n",
1115+
" for node in restricted:\n",
1116+
" seen[node] = True\n",
1117+
" \n",
1118+
" ans = 0\n",
1119+
" queue = collections.queue([0])\n",
1120+
" seen[0] = True\n",
1121+
" while queue:\n",
1122+
" curr_node = queue.popleft()\n",
1123+
" ans += 1\n",
1124+
" for next_node in neighbors[curr_node]:\n",
1125+
" if not seen[next_node]:\n",
1126+
" seen[next_node] = True\n",
1127+
" queue.append(next_node)\n",
1128+
" return ans "
1129+
]
1130+
},
1131+
{
1132+
"cell_type": "code",
1133+
"execution_count": 416,
1134+
"metadata": {},
1135+
"outputs": [
1136+
{
1137+
"data": {
1138+
"text/plain": [
1139+
"3"
1140+
]
1141+
},
1142+
"execution_count": 416,
1143+
"metadata": {},
1144+
"output_type": "execute_result"
1145+
}
1146+
],
1147+
"source": [
1148+
"n = 7; edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]]; restricted = [4,5]\n",
1149+
"n = 7; edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]]; restricted = [4,2,1]\n",
1150+
"Solution().reachableNodes(n, edges, restricted)"
1151+
]
1152+
},
1153+
{
1154+
"attachments": {},
1155+
"cell_type": "markdown",
1156+
"metadata": {},
1157+
"source": [
1158+
"#### 18. 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance"
1159+
]
1160+
},
1161+
{
1162+
"cell_type": "code",
1163+
"execution_count": 447,
1164+
"metadata": {},
1165+
"outputs": [],
1166+
"source": [
1167+
"class Solution:\n",
1168+
" def findTheCity(self, n, edges, maxd):\n",
1169+
" dis = [[float('inf')] * n for _ in range(n)]\n",
1170+
" for i, j, w in edges:\n",
1171+
" dis[i][j] = dis[j][i] = w\n",
1172+
" for i in range(n):\n",
1173+
" dis[i][i] = 0\n",
1174+
" for k in range(n):\n",
1175+
" for i in range(n):\n",
1176+
" for j in range(n):\n",
1177+
" dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j])\n",
1178+
" res = {sum(d <= maxd for d in dis[i]): i for i in range(n)}\n",
1179+
" return res[min(res)] "
1180+
]
1181+
},
1182+
{
1183+
"cell_type": "code",
1184+
"execution_count": 448,
1185+
"metadata": {},
1186+
"outputs": [
1187+
{
1188+
"data": {
1189+
"text/plain": [
1190+
"3"
1191+
]
1192+
},
1193+
"execution_count": 448,
1194+
"metadata": {},
1195+
"output_type": "execute_result"
1196+
}
1197+
],
1198+
"source": [
1199+
"n = 4; edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]]; distanceThreshold = 4\n",
1200+
"Solution().findTheCity(n, edges, distanceThreshold)"
1201+
]
1202+
},
1203+
{
1204+
"attachments": {},
1205+
"cell_type": "markdown",
1206+
"metadata": {},
1207+
"source": [
1208+
"#### 19."
1209+
]
1210+
},
1211+
{
1212+
"cell_type": "code",
1213+
"execution_count": null,
1214+
"metadata": {},
1215+
"outputs": [],
1216+
"source": []
1217+
},
1218+
{
1219+
"cell_type": "code",
1220+
"execution_count": null,
1221+
"metadata": {},
1222+
"outputs": [],
1223+
"source": []
1224+
},
1225+
{
1226+
"attachments": {},
1227+
"cell_type": "markdown",
1228+
"metadata": {},
1229+
"source": [
1230+
"#### 20."
1231+
]
1232+
},
10181233
{
10191234
"cell_type": "code",
10201235
"execution_count": null,
@@ -1029,6 +1244,14 @@
10291244
"outputs": [],
10301245
"source": []
10311246
},
1247+
{
1248+
"attachments": {},
1249+
"cell_type": "markdown",
1250+
"metadata": {},
1251+
"source": [
1252+
"#### 21."
1253+
]
1254+
},
10321255
{
10331256
"cell_type": "code",
10341257
"execution_count": null,
@@ -1043,13 +1266,36 @@
10431266
"outputs": [],
10441267
"source": []
10451268
},
1269+
{
1270+
"attachments": {},
1271+
"cell_type": "markdown",
1272+
"metadata": {},
1273+
"source": [
1274+
"#### 22."
1275+
]
1276+
},
1277+
{
1278+
"cell_type": "code",
1279+
"execution_count": null,
1280+
"metadata": {},
1281+
"outputs": [],
1282+
"source": []
1283+
},
10461284
{
10471285
"cell_type": "code",
10481286
"execution_count": null,
10491287
"metadata": {},
10501288
"outputs": [],
10511289
"source": []
10521290
},
1291+
{
1292+
"attachments": {},
1293+
"cell_type": "markdown",
1294+
"metadata": {},
1295+
"source": [
1296+
"#### 23."
1297+
]
1298+
},
10531299
{
10541300
"cell_type": "code",
10551301
"execution_count": null,
@@ -1064,6 +1310,14 @@
10641310
"outputs": [],
10651311
"source": []
10661312
},
1313+
{
1314+
"attachments": {},
1315+
"cell_type": "markdown",
1316+
"metadata": {},
1317+
"source": [
1318+
"#### 24."
1319+
]
1320+
},
10671321
{
10681322
"cell_type": "code",
10691323
"execution_count": null,
@@ -1078,6 +1332,14 @@
10781332
"outputs": [],
10791333
"source": []
10801334
},
1335+
{
1336+
"attachments": {},
1337+
"cell_type": "markdown",
1338+
"metadata": {},
1339+
"source": [
1340+
"#### 25."
1341+
]
1342+
},
10811343
{
10821344
"cell_type": "code",
10831345
"execution_count": null,

0 commit comments

Comments
 (0)