Edit Distance - Dynamic Programming - Leetcode 72 - Python

Edit Distance - Dynamic Programming - Leetcode 72 - Python

NeetCode

3 года назад

119,427 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@simbol5638
@simbol5638 - 08.02.2024 15:29

Thanks so much for your videos

Ответить
@zaid6527
@zaid6527 - 01.02.2024 14:18

We can do this in O(word2.size()) space complexity, but just using 2 arrays instead of using a 2d, we can use prev and cur array to construct the answer from bottom to up, here's the code in cpp, class Solution {
public:
int minDistance(string word1, string word2) {
int counter = 0;
int sizes = word2.size();
vector<int> prev(sizes+1);
vector<int> cur(sizes+1);
for(int i=0; i<sizes+1; i++){
prev[i] = sizes-i;
}
for(int i=word1.size()-1; i>=0; i--){
counter++;
cur[sizes] = counter;
for(int j=sizes-1; j>=0; j--){
if(word1[i] == word2[j]){
cur[j] = prev[j+1];
}
else{
int curs = min(cur[j+1], prev[j+1]);
cur[j] = min(curs, prev[j]);
cur[j] += 1;
}
}
prev = cur;
}
return prev[0];
}
};

Ответить
@arijaa.9315
@arijaa.9315 - 26.01.2024 10:37

I have noticed that the result does not change if we reverse the column and rows does that mean that converting word2 to word1 have the same distance as converting word1 to word2?

Ответить
@arijaa.9315
@arijaa.9315 - 26.01.2024 10:23

Thanks! what do ou use the explain and draw on the screen?

Ответить
@kirillzlobin7135
@kirillzlobin7135 - 30.12.2023 14:28

Hi NeetCode. What is your rank on leetcode?

Ответить
@jadenlin3724
@jadenlin3724 - 06.11.2023 04:15

Hey NeetCode, your code is the best. Your code always helps me understand how you solved the problem.

Ответить
@ary_21
@ary_21 - 12.10.2023 19:42

Thanks!

Ответить
@theteacher010
@theteacher010 - 10.10.2023 20:03

Wouldn't have guessed this was LCS until you mentioned it!

Ответить
@am_rxd
@am_rxd - 09.10.2023 15:08

nicely done i am learning through these videos cause I was not able to understand what to dointially no idea what can I do or not just doing hit and rum stuff thanks for this its help me understand what to start with and what kind of concept I would have to keep in tracjk while having something of this caliber it was quite a easy code but without this explanation of your I would never able to understand the the logic how to do .

Ответить
@alexandersmirnov4274
@alexandersmirnov4274 - 08.10.2023 15:45

now 72 is a medium problem not a hard

Ответить
@ancai5498
@ancai5498 - 01.09.2023 09:15

For anyone who is interested in the DFS memorization solution(C++) :

int dfs(int i, int j, string word1, string word2, unordered_map<Position, int, hashFunction>& cache) {
if(cache.count({i, j})) {
return cache[{i, j}];
}

if (i == word1.size() && j == word2.size()) {
return 0;
}

if (i == word1.size()) {
return word2.size() - j;
}

if (j == word2.size()) {
return word1.size() - i;
}

int distance = 0;
if (word1[i] == word2[j]) {
distance = dfs(i + 1, j + 1, word1, word2, cache);
} else {
// cases if we need replace, delete, insert
distance = 1 + min(dfs(i + 1, j + 1, word1, word2, cache), min(dfs(i + 1, j, word1, word2, cache), dfs(i, j + 1, word1, word2, cache)));
}

cache[{i, j}] = distance;

return distance;
}

int minDistance(string word1, string word2) {
unordered_map<Position, int, hashFunction> cache;

return dfs(0, 0, word1, word2, cache);
}

Ответить
@river.
@river. - 04.08.2023 19:14

i am just so happy when i find your video

Ответить
@yanjimmy8794
@yanjimmy8794 - 02.08.2023 11:39

Hi I have a question. I'm confused about why when word1[i] == word2[j] we need to use the diagonal value? How to find this pattern which is crucial to this problem. I'm new to dynamic programming. Hope someone can help. thanks in advance

Ответить
@shawn_yg1394
@shawn_yg1394 - 28.07.2023 06:08

Can not believe that it has been downgraded to a medium question. I have completely no idea the first time saw it lol.

Ответить
@sagarchawla4926
@sagarchawla4926 - 21.07.2023 10:57

After watching you explanation, me be like - isn't there any hard dp problem on leetcode 🤣

Ответить
@julesrules1
@julesrules1 - 21.07.2023 06:14

You never fails at giving me an awe moment. Thank you!

Ответить
@wat5931
@wat5931 - 19.06.2023 00:36

Insert and remove are functionally the same. If you always make word 1 longer than word 2 (by swapping if necessary), you can always use just insert or just remove

Ответить
@brawnybytes
@brawnybytes - 08.06.2023 11:30

Great explanation

Ответить
@chowtuk
@chowtuk - 30.05.2023 18:25

holy shit, things look so simple after this video

Ответить
@Raviranjankumar761
@Raviranjankumar761 - 21.05.2023 17:06

I'm stuck at why We cannot solve this problem with the LCS ?
STEPS:
1. Find LCS of two strings. Let the length of LCS be x.
2. Let the length of the first string be m and the length of the second string be n. (m – x) will be the answer in that case.

Ответить
@joshkjoby8400
@joshkjoby8400 - 14.04.2023 09:05

this is crazy

Ответить
@romo119
@romo119 - 13.04.2023 00:05

What's the purpose for initializing every elem to inf? Aren't we filling every element right to left, bottom up anyways? We just need to make sure the edges of the matrix are filled and solve the problem in the correct order, so the value at dp[i][j] can originally be 0

Ответить
@aaronvillano4295
@aaronvillano4295 - 22.03.2023 18:06

Hi sa mga kaklase ko diyan

Ответить
@yang5843
@yang5843 - 27.02.2023 04:01

Elegant explanation, thank you

Ответить
@ArcGaming07YT
@ArcGaming07YT - 26.02.2023 21:02

very helpful, thanks man

Ответить
@JustTrace17
@JustTrace17 - 26.02.2023 14:01

Thank you so much!

Ответить
@chilly2171
@chilly2171 - 26.02.2023 09:37

This isn't optimal in terms of space.

Ответить
@ferb7o2
@ferb7o2 - 26.02.2023 07:55

Ok this is crazy

Ответить
@MP-ny3ep
@MP-ny3ep - 26.02.2023 07:37

After listening to your solution , this problem feels more like a medium level problem. That's how good your explanation is. Thank you !

Ответить
@arunvijay2279
@arunvijay2279 - 26.02.2023 07:32

Why we need to add 1

Ответить
@danielsun716
@danielsun716 - 03.02.2023 10:11

dp = {}
def dfs(i, j):
if i == len(word1) and j == len(word2):
return 0
if i == len(word1):
return len(word2) - j
if j == len(word2):
return len(word1) - i
if (i, j) in dp:
return dp[(i, j)]
dp[(i, j)] = float("inf")
for i in range(len(word1) - 1, -1, -1):
for j in range(len(word2) - 1, -1, -1):
if word1[i] == word2[j]:
dp[(i, j)] = dfs(i + 1, j + 1)
else:
dp[(i, j)] = 1 + min(dfs(i, j + 1), dfs(i + 1, j), dfs(i + 1, j + 1))
return dp[(i, j)]
return dfs(0, 0)

Ответить
@hepbitkin9854
@hepbitkin9854 - 05.01.2023 10:34

I think you should draw rest of the problem.

Ответить
@AshwaniSharma-of2nq
@AshwaniSharma-of2nq - 18.12.2022 20:22

Easy to understand explanation, clear, concise and to the point.

Ответить
@dave6012
@dave6012 - 17.11.2022 07:32

Have you ever done a “total unique combinations that add up to n” algorithm? I had that problem come up and I couldn’t write an algorithm efficient enough to pass all cases in the allotted time.

It’s pretty wild, 200 ends up producing 487 million unique combinations. I know there must be a dynamic programming solution, but I couldn’t crack it.

Ответить
@shiweiwong5292
@shiweiwong5292 - 16.11.2022 11:27

the best explanation on the internet! thanks!

Ответить
@jxw7196
@jxw7196 - 30.10.2022 15:22

Brill. GOod job!

Ответить
@abc00gh
@abc00gh - 19.10.2022 02:49

Best explanation for 2d dp ever!

Ответить
@AdenGolden
@AdenGolden - 13.10.2022 01:57

for the else statement:cache[i + j] = 1 + min(cache[i][j + 1], cache[i+ 1][j], cache[i + 1][j +1])
why do we use 1+, where the 1 comes from? Thanks!

Ответить
@gonglarry9933
@gonglarry9933 - 03.10.2022 18:04

nb

Ответить
@miladjamali5123
@miladjamali5123 - 26.09.2022 16:17

thank you so much.could you help me how to use EDR on two point sets?

Ответить
@dataadvantage1890
@dataadvantage1890 - 23.09.2022 01:50

try this --- for j in range(len(word1) + 1, -1, -1)

Ответить
@user-gq1ij
@user-gq1ij - 16.09.2022 15:55

Nice explanation, BTW you are Canadian lad

Ответить
@paradox2738
@paradox2738 - 15.09.2022 01:06

dp = [[i+j for i in range(len(word2),-1,-1)] for j in range(len(word1),-1,-1)]

this combines the first 3 loops into one

Ответить
@yuqian822
@yuqian822 - 07.09.2022 08:47

I have to say that again... I LOVE NEETCODE!!!!! You are the FIRST one to look for whenever I am confused about an algo question.

Ответить
@hooriehmarefat5972
@hooriehmarefat5972 - 26.08.2022 19:32

Your explanations are absolutely awesome! Thanks for creating such helpful content!

Ответить
@ceb-rj4qw
@ceb-rj4qw - 08.08.2022 21:11

Oii.Diabolical

Ответить
@kuangyimeng8850
@kuangyimeng8850 - 08.08.2022 16:02

wow, brilliant explanation. Would you like to share how to come up with this idea?

Ответить
@augustdanellhakansson7400
@augustdanellhakansson7400 - 07.08.2022 12:38

Extremely well explained! Picture perfect tutorial!

Ответить
@sumanth6299
@sumanth6299 - 20.07.2022 15:53

This is called Excellence

Ответить