Stickers to Spell Word - DP Memoization - Leetcode 691 - Python

  Рет қаралды 26,372

NeetCode

NeetCode

Күн бұрын

Пікірлер: 56
@NeetCode
@NeetCode 3 жыл бұрын
💡 DP Playlist: kzbin.info/www/bejne/bWTVZH6Nnqqpr80
@amyotun
@amyotun 3 жыл бұрын
The best explanation so far. 👍
@Cloud-577
@Cloud-577 2 жыл бұрын
this question should be illegal
@namitsharma9600
@namitsharma9600 7 ай бұрын
Thanks, I wasted a whole day on it. Finally submitted it after watching your video. I will not bother going over more optimised solution now.
@priyankasaddival4794
@priyankasaddival4794 Жыл бұрын
I love that "Word break with steroids"😂
@trefoilz7355
@trefoilz7355 10 ай бұрын
It can be converted as "find the shortest path between target and an empty string". We can then use bfs with a queue or Dijkstra with a min heap.
@alexzheng9841
@alexzheng9841 6 ай бұрын
Pure BFS won't work since it's still O(m^n), where m is the number of stickers and n is the length of the target. BFS outperforms DFS in that it doesn't search beyond the shortest path, but that's not the main inefficiency here. The main problem is that you are repeatedly solving the same subproblems whether you are doing DFS or BFS, which is why you want to cache the result. Also, in BFS, for every path, you need to keep the entire state (e.g., the remaining target), so it's going to consume a lot of memory, which is not recommended for 2^n problems like this.
@mingyuli8887
@mingyuli8887 Жыл бұрын
who designed this problem? To solve this under 45 mins during interview is insane!
@hydromarlon4168
@hydromarlon4168 3 жыл бұрын
Been working on this question for a week best I could do without this video was 65/101 test cases.
@arcface2casia255
@arcface2casia255 8 ай бұрын
Was asked to solve this in 20 minutes in a Facebook screening call. Pretty sure the interviewer himself couldn't have done it in 20 minutes (if it was his first time) It looks like they just expect us to parrot the solutions. To be honest these interviews are more fake than American porn.
@salczar
@salczar 8 ай бұрын
This can be simplified is one uses Counters for both the target and the stickers. Then it’s a simple matter of subtracting each counter sticker from the counter target. (Cnt1 - Cnt2, not Cnt1.subtract(Cnt2). I was able to solve this in half the code with this technique.
@AymenDaoudi-u6h
@AymenDaoudi-u6h Жыл бұрын
Hi NeetCode, thanks for this explanation, it really helped, however please there is a small enhancement you can do: In fact the reason why your solution at the end is showing less performance, it's because you are not benefiting from the memoization correctly. If you debug your solution, you will find that the line 30 is being hit multiple times for the same "remainT". the variable "used" in the line 24 should check first if "dp[remaintT]" is available to not do the work again. The dp check on the line 11 can be removed. Thanks again.
@alexzheng9841
@alexzheng9841 6 ай бұрын
Thanks for sharing this solution. I found it much easier to understand compared to the LeetCode's official DP solution. To improve the performance, you should always sort the characters in the target before caching. For example, building "abab" and "abba" is the same problem, but the way you are doing it now treats them as different problems. If you always sort the characters, you would cache the result as "aabb" and you wouldn't need to recalculate the solution for any combination of the same characters.
@MrUnemployedYouth
@MrUnemployedYouth 9 ай бұрын
Who are you mate? you are an absolute beast! thank you!
@abhinavbhattca
@abhinavbhattca Жыл бұрын
00:03 Solving the 'Stickers to Spell Word' problem using brute force and memoization 01:52 Implementing brute force and memoization approach to solve the problem 05:20 Choosing the stickers that contain required characters leads to finding the minimum number of stickers needed 07:06 Using caching or dynamic programming can help optimize the solution 10:37 Using memoization, we can optimize the process of finding the target word in a given set of characters. 12:18 We can have two to the n different subsequences of a given string. 15:54 Using dynamic programming to solve the 'Stickers to Spell Word' problem. 17:33 The code involves converting stickers to a hashmap and using dynamic programming to find the minimum number of stickers to create a target string. 20:35 The return value of the dfs function depends on whether the target string can be created from the given stickers 22:12 The remaining portion of the target string can be created using stickers 25:21 The result is calculated by adding the number of extra stickers used to the initial result. 26:58 Understanding and implementing DP Memoization in Python
@purnawirmanhuang5145
@purnawirmanhuang5145 Жыл бұрын
hmm, i found your solution this time is overly complicated and the discussion as well. It is hard to pinpoint exact things, but i think a couple of things can be improved: 1. in discussion, you went on length to discuss why simply using target is not enough, but later on you kinda contradict your own discussions. Perhaps i didnt understand the context very well, but i felt like you have not fully understood the solution yet thus you were considering a convoluted scenario where target can not be used as the key. 2. you used dfs(target, stick) as the dfs..., which i find a bit unnecessary... It gives the impression it is a 2-d DP, while actually we can think of the problem as 1-d DP, which has dp(target) = 1 + min(dp(target - sticker1), dp(target-sticker2, ...). 3.Lastly, you didn't explain why we have to check remainT[0] not in s is necessary. In fact it is not necessary condition, you will get the solution as well even without these lines. However, this line is super helpful since it can skip some loops by only processing sticker that is a potential candidate. This can be seen as like a heuristic A star technique in search. Missed an opportunity to explain the trick.
@syte_y
@syte_y 5 ай бұрын
I have to say your explanations are so well done!
@a19970615
@a19970615 2 жыл бұрын
Can I ask how to get the 2^n? I don't get it from video.
@Terszel
@Terszel 3 жыл бұрын
Thanks for organizing the playlists for easy consumption
@fragrancias972
@fragrancias972 2 ай бұрын
It would be really spicy if you could use sticker pieces of any length, instead of just one character.
@danielsun716
@danielsun716 Жыл бұрын
if we do not check the 1st one character, like the target is "thehat" at the beginning, we know we can first go to the sticker "with", cause "th" in with. But if we skip th, what I mean is we iterate all characters in "thehat", th is not in "example", but e and a are in "example", so we can go pass the sticker "example" and the remaining target is "thht", then we can go pass the second sticker "example". So the path gonna be "thehat with using example" -> "thht with using with" -> "ht with using with", then the target is empty. And the result is still 3 stickers. But in 11:07, @neetcode said we cannot go pass the sticker"example", because the 1st chacter "t" is only in "with". I think this thought also gonna be work, but why doesn't neetcode take this solution?
@AviralSrivastava2809
@AviralSrivastava2809 Ай бұрын
What a beautoful explanation!
@strawberriesandcream2863
@strawberriesandcream2863 Жыл бұрын
thanks for the solution! could someone please explain why the time complexity would be 2^n? it looks like we can make more than 2 choices for every subsequence if there are multiple stickers applicable.
@strawberriesandcream2863
@strawberriesandcream2863 Жыл бұрын
i got it now. it's because an array can have at most 2^n subsequences.
@chiamakabrowneyes
@chiamakabrowneyes 9 ай бұрын
This is the most evil question I've ever seen
@Yyyyyyyyhhhhh12342
@Yyyyyyyyhhhhh12342 9 күн бұрын
great explanation
@rite2riddhi
@rite2riddhi 2 жыл бұрын
dude , you are gold.
@haozhu6908
@haozhu6908 8 ай бұрын
This problem is hard to understand when trying to solve it with regular iterative or recursive DP code structures. It's much easier to understand when using a BFS DP structure (iterative level-order traversal).
@doronbaruch665
@doronbaruch665 3 жыл бұрын
Thank you for doing and sharing this. I love your videos.
@shuoj.2038
@shuoj.2038 3 жыл бұрын
Can you pls start the tag Binary problems? Really appreciated you make the leetcode problem easy to understand.
@agnesakne4409
@agnesakne4409 2 жыл бұрын
Why is this problem found under category bit masking? What is its relationship to bit masking?
@alexzheng9841
@alexzheng9841 6 ай бұрын
leetcode has an official DP solution that uses bit masking, which I don't understand at all...
@ravalibusetty
@ravalibusetty 3 жыл бұрын
Thank you! This made my day!
@algorithmo134
@algorithmo134 3 жыл бұрын
This question is preety hard.
@user-le6ts6ci7h
@user-le6ts6ci7h Жыл бұрын
When you choose a stikcker you are basically decreasing count of all the same character in the sticker and the target, then what is the need of reducing the frequency at the very start of the recursion it seems like redundant code
@venkateshtalapally7505
@venkateshtalapally7505 3 жыл бұрын
23:20 hey would you tell why we are going for recursion only first letter of target and stick matches?
@melissac4872
@melissac4872 2 жыл бұрын
so that we can keep the cache size as 2^n
@aybarskeremtaskan
@aybarskeremtaskan Жыл бұрын
Actually, we can skip that check (it is a shortcut for a few things); but we also need a way to return infinity if nothing is consumed; to that end: we could compare if the remainT == t and if so; return infinity to skip that sticker.
@anandpad
@anandpad 4 ай бұрын
because at some point, you have to construct the entire target string some way or the other - and in the process you have to use some sticker or the other. So comparing against all characters in the is a bit redundant. lets say the remainT = "ehat" and the stick(s) are example and with (ofc enumerated) => remainT[0] will match the 'e' and you consume the sticker and in that process you get remainT = "ht"... and subsequently in the next call you use the 'with' sticker again. lets say now the order of stick(s) are 'with' and 'example'. at the first iteration you do not use 'with' and then the loop picks 'example'. so the order doesnt matter. hope this explains
@maverickreal09
@maverickreal09 2 жыл бұрын
How are 3-dimensional mortals supposed to solve this in an interview?
@jieli8
@jieli8 Жыл бұрын
impossible
@atharvbhagya4317
@atharvbhagya4317 Жыл бұрын
you would have to be international grandmaster to be able to come up with solution in 45 minutes.
@algorithmo134
@algorithmo134 3 жыл бұрын
@Neetcode Hi can we cache both remainT and stick we used? I did it like this dp[(remainT, tuple(stick))] = used but it gives me Time Limit exceeded
@sanjeev0075
@sanjeev0075 3 жыл бұрын
Can we cache res for dp[t] in order to avoid the duplicate call or this part is not needed ?
@saksham.malhotra
@saksham.malhotra 2 жыл бұрын
Would sorting target reduce the number of hashes generated to memorize
@learning_hunt8544
@learning_hunt8544 Жыл бұрын
Can you also provide solutions of trilogy innovations coding interview questions?
@truthSaty
@truthSaty Жыл бұрын
Am i the only one who thought greedy approach would do ?
@shaharrefaelshoshany9442
@shaharrefaelshoshany9442 3 жыл бұрын
best
@naveenprasanthsa3749
@naveenprasanthsa3749 3 жыл бұрын
Need string interleaving
@nirutgupta5894
@nirutgupta5894 2 жыл бұрын
100/101 🥲
@dominic_lee
@dominic_lee 2 жыл бұрын
mee mo
@-kurdush
@-kurdush 3 жыл бұрын
stick[c] -= 1 can anyone please explain what this line is actually does? does it going to decrease length of each sticker we have passed to function please explain 😑😫😫.I have listened it 5 times still I am getting confused. what its actually doing? please reply me ASAP
@venkateshtalapally7505
@venkateshtalapally7505 3 жыл бұрын
Stick is a dictionary which track frequencies of each character. When we deal each sticker with target we are reducing the frequency as we are cancelling the matched ones
@akashjain7378
@akashjain7378 Жыл бұрын
Word Break on Steroid 😆
@dominic_lee
@dominic_lee 2 жыл бұрын
Solution is now TLE😣
Edit Distance - Dynamic Programming - Leetcode 72 - Python
21:00
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33
Players vs Pitch 🤯
00:26
LE FOOT EN VIDÉO
Рет қаралды 137 МЛН
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,2 МЛН
Maximal Square - Top Down Memoization - Leetcode 221
19:50
NeetCode
Рет қаралды 72 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 695 М.
Alien Dictionary - Topological Sort - Leetcode 269 - Python
22:05
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 303 М.
Burst Baloons - Dynamic Programming - Leetcode 312 - Python
21:20
Word Break - Dynamic Programming - Leetcode 139 - Python
15:35
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Word Ladder - Breadth First Search - Leetcode 127 - Python
17:06
ROBOT ROOM CLEANER | LEETCODE # 489 | PYTHON BACKTRACK SOLUTION
19:44
Cracking FAANG
Рет қаралды 11 М.
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33