GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/072_Edit_Distance.py at master · zhimintu/leetcode

Expand file tree

/

072_Edit_Distance.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
38 lines (37 loc) · 1.37 KB
/

072_Edit_Distance.py

Copy path

File metadata and controls

38 lines (37 loc) · 1.37 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution(object):
# https://discuss.leetcode.com/topic/17639/20ms-detailed-explained-c-solutions-o-n-space/2
# def minDistance(self, word1, word2):
# """
# :type word1: str
# :type word2: str
# :rtype: int
# """
# ls_1, ls_2 = len(word1), len(word2)
# dp = [[0] * (ls_2 + 1) for _ in range(ls_1 + 1)]
# for i in range(1, ls_1 + 1):
# dp[i][0] = i
# for j in range(1, ls_2 + 1):
# dp[0][j] = j
# for i in range(1, ls_1 + 1):
# for j in range(1, ls_2 + 1):
# if word1[i - 1] == word2[j - 1]:
# dp[i][j] = dp[i - 1][j - 1]
# else:
# dp[i][j] = min(dp[i - 1][j - 1] + 1,
# dp[i][j - 1] + 1,
# dp[i - 1][j] + 1)
# return dp[ls_1][ls_2]
def minDistance(self, word1, word2):
ls_1, ls_2 = len(word1), len(word2)
dp = range(ls_1 + 1)
for j in range(1, ls_2 + 1):
pre = dp[0]
dp[0] = j
for i in range(1, ls_1 + 1):
temp = dp[i]
if word1[i - 1] == word2[j - 1]:
dp[i] = pre
else:
dp[i] = min(pre + 1, dp[i] + 1, dp[i - 1] + 1)
pre = temp
return dp[ls_1]
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.