[LeetCode] 72. Edit Distance_hard tag: Dynamic Programming
Given two words word1 and word2, find the minimum number of operations required to convert word1to word2.
You have the following 3 operations permitted on a word:
- Insert a character
- Delete a character
- Replace a character
Example 1:
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
Example 2:
Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u') 这个题目思路是用Dynamic Programming, ans[i][j] 表明前i-1 个word1的字符与到前j-1个word2的
字符的最小值, 然后ans[i][j] = min(ans[i-1][j]+ 1, ans[i][j-1] + 1, ans[i-1][j-1] + temp)
其中temp = 0 if word1[i] == word2[j] else 1
最后返回ans[m,n]即可. 这个模式跟[LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
很像, 都是用上, 左和左上角的元素去递推现在的元素. 当然也可以用滚动数组的方式去将space 降为 O(n) 1. Constraints
1) size >= [0*0]
2) element can be anything, captal sensitive 2. ideas
Dynamic programming, T: O(m*n) S: O(m*n) => O(n) using rolling array 3. Codes
1) S: O(m*n)
class Solution:
def editDistance(self, word1, word2):
m, n = len(word1), len(word2)
ans = [[0]*n+1 for _ in range(m+1)]
for i in range(1, m+1):
ans[i][0] = i
for j in range(1, n+1):
ans[0][j] = j
for i in range(1, m+1):
for j in range(1, n+1):
temp = 0 if word1[i-1] == word2[j-1] else 1
ans[i][j] = min(ans[i-1][j] + 1, ans[i][j-1] + 1, ans[i-1][j-1] + temp)
return ans[m][n]
2) S: O(n) using 滚动数组
class Solution:
def editDistance(self, word1, word2):
m, n = len(word1), len(word2)
ans = [[0]*(n+1) for _ in range(2)]
for j in range(1, n+1):
ans[0][j] = j
ans[1][0] = 1
for i in range(1, m+1):
for j in range(1, n+1):
ans[i%2][0] = i
temp = 0 if word1[i-1] == word2[j-1] else 1
ans[i%2][j] = min(ans[i%2-1][j] + 1, ans[i%2][j-1] + 1, ans[i%2-1][j-1] + temp)
return ans[m%2][n]
4. Test cases
1) "horse", "ros"
[LeetCode] 72. Edit Distance_hard tag: Dynamic Programming的更多相关文章
- [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] 120. Triangle _Medium tag: Dynamic Programming
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- [LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming
基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如8 ...
- [LeetCode] 256. Paint House_Easy tag: Dynamic Programming
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- sqlite的一个Unable to Open database file的坑爹错误
今天,被sqlite的一个机制给坑了.本人用C语言写的cgi程序去访问sqlite数据库,读取没有问题,但是插入新纪录和更新数据就不行,在服务器上直接对数据库进行增删查改则没有任何问题.但浏览器上访问 ...
- JS - 常用效果代码库 (四)
1.首字母大写示例: var value = “一段文本或一个参数”; value = value.toString() return value.charAt(0).toUpperCase() + ...
- Android studio修改字体(font)大小(size)
Android Studio 默认编辑器(Editor)的方案(Scheme)是无法修改字体的, 可以Save as, 保存为新的方案(Scheme), 然后更改字体大小; 位置: File-> ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- CF 217A Ice Skating
A. Ice Skating time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- CONE NAT 和 Symmetric NAT
CONE NAT 和 Symmetric NAT 1. NAT 的划分 RFC3489 中将 NAT 的实现分为四大类: Full Cone NAT 完全锥形 NAT Restricted Cone ...
- ThreadLocal的实现和使用场景
ThreadLocal 内部实现.应用场景和内存泄漏 深入理解线程局部变量:ThreadLocal <Java源码分析>:ThreadLocal /ThreadLocalMap Threa ...
- 用mongoose实现mongodb增删改查
//用户 var mongoose = require("mongoose"), setting = require("./setting"); //配置连接数 ...
- [分布式系统学习]阅读笔记 Distributed systems for fun and profit 之一 基本概念
因为工作的原因,最近打算看一些分布式学习的资料.其中这个http://book.mixu.net/distsys/就是一篇非常适合分布式入门的介绍. 这个短小的材料有下面5个小的章节,图文并茂,也没有 ...
- PAT甲1038 Recover the smallest number
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...