Skip to content

Commit 12b3fde

Browse files
committed
Graph medium
1 parent 8a18fb8 commit 12b3fde

File tree

2 files changed

+313
-6
lines changed

2 files changed

+313
-6
lines changed

13. Graphs/03. LeetCode Medium.ipynb

Lines changed: 280 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,283 @@
525525
" print(\"asdf\")"
526526
]
527527
},
528+
{
529+
"attachments": {},
530+
"cell_type": "markdown",
531+
"metadata": {},
532+
"source": [
533+
"#### 9. 1584. Min Cost to Connect All Points"
534+
]
535+
},
536+
{
537+
"cell_type": "code",
538+
"execution_count": 63,
539+
"metadata": {},
540+
"outputs": [],
541+
"source": [
542+
"class Solution:\n",
543+
" def minCostConnectPoints(self, points: List[List[int]]) -> int:\n",
544+
" d, res = {(x, y): float('inf') if i else 0 for i, (x, y) in enumerate(points)}, 0\n",
545+
" #print(d)\n",
546+
" #print()\n",
547+
" while d:\n",
548+
" x, y = min(d, key=d.get) # obtain the current minimum edge\n",
549+
" res += d.pop((x, y)) # and remove the corresponding point\n",
550+
" for x1, y1 in d: # for the rest of the points, update the minimum manhattan distance\n",
551+
" d[(x1, y1)] = min(d[(x1, y1)], abs(x-x1)+abs(y-y1))\n",
552+
" #print(x,y,d,res)\n",
553+
" #rint()\n",
554+
" return res"
555+
]
556+
},
557+
{
558+
"cell_type": "code",
559+
"execution_count": 64,
560+
"metadata": {},
561+
"outputs": [
562+
{
563+
"data": {
564+
"text/plain": [
565+
"20"
566+
]
567+
},
568+
"execution_count": 64,
569+
"metadata": {},
570+
"output_type": "execute_result"
571+
}
572+
],
573+
"source": [
574+
"points = [[0,0],[2,2],[3,10],[5,2],[7,0]]\n",
575+
"#points = [[3,12],[-2,5],[-4,1]]\n",
576+
"Solution().minCostConnectPoints(points)"
577+
]
578+
},
579+
{
580+
"attachments": {},
581+
"cell_type": "markdown",
582+
"metadata": {},
583+
"source": [
584+
"#### 10. [547]. Number of Provinces"
585+
]
586+
},
587+
{
588+
"cell_type": "code",
589+
"execution_count": 135,
590+
"metadata": {},
591+
"outputs": [],
592+
"source": [
593+
"class Solution:\n",
594+
" def findCircleNum(self, isConnected: List[List[int]]) -> int:\n",
595+
" N = len(isConnected)\n",
596+
" seen = set()\n",
597+
" def dfs(node):\n",
598+
" for nei, adj in enumerate(isConnected[node]):\n",
599+
" if adj and nei not in seen:\n",
600+
" seen.add(nei)\n",
601+
" dfs(nei)\n",
602+
" \n",
603+
" ans = 0\n",
604+
" for i in range(N):\n",
605+
" if i not in seen:\n",
606+
" dfs(i)\n",
607+
" ans += 1\n",
608+
" return ans"
609+
]
610+
},
611+
{
612+
"cell_type": "code",
613+
"execution_count": 136,
614+
"metadata": {},
615+
"outputs": [
616+
{
617+
"data": {
618+
"text/plain": [
619+
"1"
620+
]
621+
},
622+
"execution_count": 136,
623+
"metadata": {},
624+
"output_type": "execute_result"
625+
}
626+
],
627+
"source": [
628+
"isConnected = [[1,1,0],[1,1,0],[0,0,1]]\n",
629+
"isConnected = [[1,0,0],[0,1,0],[0,0,1]]\n",
630+
"isConnected = [[1,1,1],[1,1,1],[1,1,1]]\n",
631+
"Solution().findCircleNum(isConnected)"
632+
]
633+
},
634+
{
635+
"attachments": {},
636+
"cell_type": "markdown",
637+
"metadata": {},
638+
"source": [
639+
"#### 11."
640+
]
641+
},
642+
{
643+
"cell_type": "code",
644+
"execution_count": null,
645+
"metadata": {},
646+
"outputs": [],
647+
"source": []
648+
},
649+
{
650+
"cell_type": "code",
651+
"execution_count": null,
652+
"metadata": {},
653+
"outputs": [],
654+
"source": []
655+
},
656+
{
657+
"attachments": {},
658+
"cell_type": "markdown",
659+
"metadata": {},
660+
"source": [
661+
"#### 12."
662+
]
663+
},
664+
{
665+
"cell_type": "code",
666+
"execution_count": null,
667+
"metadata": {},
668+
"outputs": [],
669+
"source": []
670+
},
671+
{
672+
"cell_type": "code",
673+
"execution_count": null,
674+
"metadata": {},
675+
"outputs": [],
676+
"source": []
677+
},
678+
{
679+
"attachments": {},
680+
"cell_type": "markdown",
681+
"metadata": {},
682+
"source": [
683+
"#### 13."
684+
]
685+
},
686+
{
687+
"cell_type": "code",
688+
"execution_count": null,
689+
"metadata": {},
690+
"outputs": [],
691+
"source": []
692+
},
693+
{
694+
"cell_type": "code",
695+
"execution_count": null,
696+
"metadata": {},
697+
"outputs": [],
698+
"source": []
699+
},
700+
{
701+
"cell_type": "code",
702+
"execution_count": null,
703+
"metadata": {},
704+
"outputs": [],
705+
"source": []
706+
},
707+
{
708+
"cell_type": "code",
709+
"execution_count": null,
710+
"metadata": {},
711+
"outputs": [],
712+
"source": []
713+
},
714+
{
715+
"cell_type": "code",
716+
"execution_count": null,
717+
"metadata": {},
718+
"outputs": [],
719+
"source": []
720+
},
721+
{
722+
"cell_type": "code",
723+
"execution_count": null,
724+
"metadata": {},
725+
"outputs": [],
726+
"source": []
727+
},
728+
{
729+
"cell_type": "code",
730+
"execution_count": null,
731+
"metadata": {},
732+
"outputs": [],
733+
"source": []
734+
},
735+
{
736+
"cell_type": "code",
737+
"execution_count": null,
738+
"metadata": {},
739+
"outputs": [],
740+
"source": []
741+
},
742+
{
743+
"cell_type": "code",
744+
"execution_count": null,
745+
"metadata": {},
746+
"outputs": [],
747+
"source": []
748+
},
749+
{
750+
"cell_type": "code",
751+
"execution_count": null,
752+
"metadata": {},
753+
"outputs": [],
754+
"source": []
755+
},
756+
{
757+
"cell_type": "code",
758+
"execution_count": null,
759+
"metadata": {},
760+
"outputs": [],
761+
"source": []
762+
},
763+
{
764+
"cell_type": "code",
765+
"execution_count": null,
766+
"metadata": {},
767+
"outputs": [],
768+
"source": []
769+
},
770+
{
771+
"cell_type": "code",
772+
"execution_count": null,
773+
"metadata": {},
774+
"outputs": [],
775+
"source": []
776+
},
777+
{
778+
"cell_type": "code",
779+
"execution_count": null,
780+
"metadata": {},
781+
"outputs": [],
782+
"source": []
783+
},
784+
{
785+
"cell_type": "code",
786+
"execution_count": null,
787+
"metadata": {},
788+
"outputs": [],
789+
"source": []
790+
},
791+
{
792+
"cell_type": "code",
793+
"execution_count": null,
794+
"metadata": {},
795+
"outputs": [],
796+
"source": []
797+
},
798+
{
799+
"cell_type": "code",
800+
"execution_count": null,
801+
"metadata": {},
802+
"outputs": [],
803+
"source": []
804+
},
528805
{
529806
"cell_type": "code",
530807
"execution_count": null,
@@ -556,7 +833,7 @@
556833
],
557834
"metadata": {
558835
"kernelspec": {
559-
"display_name": "Python 3.8.8 ('base')",
836+
"display_name": "base",
560837
"language": "python",
561838
"name": "python3"
562839
},
@@ -570,12 +847,12 @@
570847
"name": "python",
571848
"nbconvert_exporter": "python",
572849
"pygments_lexer": "ipython3",
573-
"version": "3.8.8"
850+
"version": "3.9.13"
574851
},
575852
"orig_nbformat": 4,
576853
"vscode": {
577854
"interpreter": {
578-
"hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186"
855+
"hash": "fecfa28de78ead09ec2989a580469213059448baba5e0b02aeeb7641afb84221"
579856
}
580857
}
581858
},

16. Greedy Method/01. Prim's McST.ipynb

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,36 @@
133133
"obj.printMst()"
134134
]
135135
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 1,
139+
"metadata": {},
140+
"outputs": [
141+
{
142+
"ename": "NameError",
143+
"evalue": "name 'List' is not defined",
144+
"output_type": "error",
145+
"traceback": [
146+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
147+
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
148+
"\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_16976\\4071062386.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mclass\u001b[0m \u001b[0mSolution\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mminCostConnectPoints\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpoints\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mList\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mList\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m->\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0md\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mres\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'inf'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32melse\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpoints\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[0md\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# obtain the current minimum edge\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
149+
"\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_16976\\4071062386.py\u001b[0m in \u001b[0;36mSolution\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mclass\u001b[0m \u001b[0mSolution\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mdef\u001b[0m \u001b[0mminCostConnectPoints\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpoints\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mList\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mList\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m->\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0md\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mres\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'inf'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32melse\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpoints\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[0md\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# obtain the current minimum edge\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
150+
"\u001b[1;31mNameError\u001b[0m: name 'List' is not defined"
151+
]
152+
}
153+
],
154+
"source": [
155+
"class Solution:\n",
156+
" def minCostConnectPoints(self, points: List[List[int]]) -> int:\n",
157+
" d, res = {(x, y): float('inf') if i else 0 for i, (x, y) in enumerate(points)}, 0\n",
158+
" while d:\n",
159+
" x, y = min(d, key=d.get) # obtain the current minimum edge\n",
160+
" res += d.pop((x, y)) # and remove the corresponding point\n",
161+
" for x1, y1 in d: # for the rest of the points, update the minimum manhattan distance\n",
162+
" d[(x1, y1)] = min(d[(x1, y1)], abs(x-x1)+abs(y-y1))\n",
163+
" return res"
164+
]
165+
},
136166
{
137167
"cell_type": "code",
138168
"execution_count": null,
@@ -143,7 +173,7 @@
143173
],
144174
"metadata": {
145175
"kernelspec": {
146-
"display_name": "Python 3.8.8 ('base')",
176+
"display_name": "base",
147177
"language": "python",
148178
"name": "python3"
149179
},
@@ -157,12 +187,12 @@
157187
"name": "python",
158188
"nbconvert_exporter": "python",
159189
"pygments_lexer": "ipython3",
160-
"version": "3.8.8"
190+
"version": "3.9.13"
161191
},
162192
"orig_nbformat": 4,
163193
"vscode": {
164194
"interpreter": {
165-
"hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186"
195+
"hash": "fecfa28de78ead09ec2989a580469213059448baba5e0b02aeeb7641afb84221"
166196
}
167197
}
168198
},

0 commit comments

Comments
 (0)