Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c606a1b

Browse files
DP medium
1 parent e92c842 commit c606a1b

File tree

1 file changed

+77
-4
lines changed

1 file changed

+77
-4
lines changed

‎17. Dynamic Programming/00. LeetCode Medium.ipynb‎

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,19 +657,92 @@
657657
"Solution().canJump(nums)"
658658
]
659659
},
660+
{
661+
"attachments": {},
662+
"cell_type": "markdown",
663+
"metadata": {},
664+
"source": [
665+
"#### 72. Edit Distance"
666+
]
667+
},
660668
{
661669
"cell_type": "code",
662-
"execution_count": null,
670+
"execution_count": 11,
663671
"metadata": {},
664672
"outputs": [],
665-
"source": []
673+
"source": [
674+
"######################\n",
675+
"# Recursive Solution #\n",
676+
"######################\n",
677+
"\n",
678+
"from functools import cache\n",
679+
"\n",
680+
"class Solution:\n",
681+
" @cache\n",
682+
" def minDistance(self, word1, word2):\n",
683+
" \"\"\"Naive recursive solution\"\"\"\n",
684+
" if not word1 and not word2: return 0\n",
685+
" if not word1: return len(word2)\n",
686+
" if not word2: return len(word1)\n",
687+
" if word1[0] == word2[0]: return self.minDistance(word1[1:], word2[1:])\n",
688+
" insert = 1 + self.minDistance(word1, word2[1:])\n",
689+
" delete = 1 + self.minDistance(word1[1:], word2)\n",
690+
" replace = 1 + self.minDistance(word1[1:], word2[1:])\n",
691+
" return min(insert, replace, delete)"
692+
]
666693
},
667694
{
668695
"cell_type": "code",
669-
"execution_count": null,
696+
"execution_count": 12,
670697
"metadata": {},
671698
"outputs": [],
672-
"source": []
699+
"source": [
700+
"######################\n",
701+
"# Iterative Solution #\n",
702+
"######################\n",
703+
"\n",
704+
"class Solution:\n",
705+
" def minDistance(self, word1, word2):\n",
706+
" \"\"\"Dynamic programming solution\"\"\"\n",
707+
" m = len(word1)\n",
708+
" n = len(word2)\n",
709+
" table = [[0] * (n + 1) for _ in range(m + 1)]\n",
710+
"\n",
711+
" for i in range(m + 1):\n",
712+
" table[i][0] = i\n",
713+
" for j in range(n + 1):\n",
714+
" table[0][j] = j\n",
715+
"\n",
716+
" for i in range(1, m + 1):\n",
717+
" for j in range(1, n + 1):\n",
718+
" if word1[i - 1] == word2[j - 1]:\n",
719+
" table[i][j] = table[i - 1][j - 1]\n",
720+
" else:\n",
721+
" table[i][j] = 1 + min(table[i - 1][j], table[i][j - 1], table[i - 1][j - 1])\n",
722+
" return table[-1][-1]"
723+
]
724+
},
725+
{
726+
"cell_type": "code",
727+
"execution_count": 13,
728+
"metadata": {},
729+
"outputs": [
730+
{
731+
"data": {
732+
"text/plain": [
733+
"5"
734+
]
735+
},
736+
"execution_count": 13,
737+
"metadata": {},
738+
"output_type": "execute_result"
739+
}
740+
],
741+
"source": [
742+
"word1 = \"horse\"; word2 = \"ros\"\n",
743+
"word1 = \"intention\"; word2 = \"execution\"\n",
744+
"Solution().minDistance(word1, word2)"
745+
]
673746
}
674747
],
675748
"metadata": {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /