STICKERS TO SPELL WORD | LEETCODE 691 | PYTHON DFS + MEMOIZATION SOLUTION

  Рет қаралды 4,353

Cracking FAANG

Cracking FAANG

Күн бұрын

Пікірлер: 33
@landocodes
@landocodes 11 ай бұрын
LMAO the pain and exhaustion at the end was funny. Great work as always and thanks for going through that for us.
@unoriginalcontent6110
@unoriginalcontent6110 11 ай бұрын
Factual 😂
@chandrashekharmuradnar5681
@chandrashekharmuradnar5681 7 ай бұрын
Its fun to listen to your commentary while solving the problem. It makes the learning process more real than just going through another problem solving technique.
@gaam
@gaam Жыл бұрын
I asked and you delivered! You're a goddamn legend. Definitely the clearest solution video I've watched on this BS problem. Thank you! Little note @ 0:50, - example 2 is not possible because none of the stickers contain an "a". Think you just misspoke mentioning "b" there.
@crackfaang
@crackfaang Жыл бұрын
Haha thanks, this one was definitely not a fun one to solve!
@boopboop451
@boopboop451 Жыл бұрын
Struggled with this question for a few weeks now. This is definitely the cleanest and most intuitive approach. Thanks for making this video man, really appreciate it! Also on line 24 - I thought Counters are unordered collections. I am surprised to see that "remaining_characters" counter somehow works here and returns the chars in the same order every time across different target strings (for the memo to work). I was thinking that we would do sort on the keys before constructing the "letters" list.
@crackfaang
@crackfaang Жыл бұрын
You know I thought this as well and was surprised it just worked. Maybe it is accepted whether or not you use the sorting. But yea to avoid duplicate work the ordering should always be normalized. But maybe collections.Counter is somehow a sorted dictionary as well
@aman4434
@aman4434 11 ай бұрын
@@crackfaang nice video! Does ordering matter though? Since we are going through all characters anyway. I also wouldn't have thought of just checking the first character. I would have taken a set intersection or something, and that would be linear time for every word. This is cool.
@ahmedtremo
@ahmedtremo Жыл бұрын
the dark theme and zooming in to code is much better thanks
@crackfaang
@crackfaang Жыл бұрын
Yea lessons learned! Not going to bother remaking the old ones but agreed it's much easier to see
@rushikeshbutley7576
@rushikeshbutley7576 Жыл бұрын
Right on time 🙌
@ax5344
@ax5344 10 ай бұрын
@10:29 ```if target_str[0] not in sticker: continue. ``` I cannot quite follow here. Say target_str = "then", sticker={a:1,e:2, n:2}, we will just skip this sticker because "t" is not in it?
@jimmymoore5210
@jimmymoore5210 3 ай бұрын
Think about it like this: to be able to count the number of stickers each letter MUST have a corresponding sticker. In the event that there aren't any stickers that match to that specific character then we're out of luck and have to return -1. This line is an optimization and base case in one since it allows us to only recursively call dfs on stickers that match the first character thus shortening the resulting target string for the next dfs call. Pretty elegant
@AcidLite
@AcidLite Жыл бұрын
Great Video, I was struggling with this question.
@crackfaang
@crackfaang Жыл бұрын
Glad you liked it, was not a fun one for sure
@pria_xoxo
@pria_xoxo Жыл бұрын
Thank you for this gem!!!!
@krisd8687
@krisd8687 8 ай бұрын
what is the idea behind ans = min(ans, 1+dfs(target_str))? I dont seem to understand this part.. Why are we checking for the min value? In the first loop the value of ans will be float('inf').So that makes sense for the first loop. But from the second loop why should it be the min value?
@landocodes
@landocodes 11 ай бұрын
Could you explain why we are skipping the sticker if target_str[0] is not in sticker? I'd struggle to be able to explain my decision for line 19 in an interview.
@crackfaang
@crackfaang 11 ай бұрын
It would be too expensive to check every single character so we only check the first because it would be a O(1) operation. If we can get a solution, it won't matter in the end because eventually all the characters will be used up.
@davidoh0905
@davidoh0905 8 ай бұрын
@@crackfaang if we skipped on a sticker because it did not have the first match, and no other stickers had any matches, then woulnd't it turn out to be not finding out the solution even though it exists?
@HamidrezaFarhidzadeh
@HamidrezaFarhidzadeh 8 ай бұрын
Do you have github repo for codes you implemented here?
@zhouzhouchen3731
@zhouzhouchen3731 11 ай бұрын
Love this solution!
@AnandKumar-kz3ls
@AnandKumar-kz3ls Жыл бұрын
thanks bro, your solution is great first i though of storing used character in mask than found out there may be duplicate characters also
@crackfaang
@crackfaang Жыл бұрын
Whenever I see a solution in the discuss tab that involves masking I always just close it lol that shit is too hacky for me
@dnm9931
@dnm9931 6 ай бұрын
Thanks soo much as always
@JiachengLiu-v3l
@JiachengLiu-v3l 10 ай бұрын
It looks like a linear integer programming problem, something like a knapsack problem? I would definitely throw this thing to a LIP solver such as LINDO.
@bogdanivchenko3723
@bogdanivchenko3723 10 ай бұрын
I think that lines 19 and 20 are a hack for leetcode to speed up specific cases. I don't see any reason why [0] was chosen
@TooManyPBJs
@TooManyPBJs 9 ай бұрын
is it safe to assume if they ask me this, they don't want me? Lol
@crackfaang
@crackfaang 9 ай бұрын
Haha yea it is a tricky one. But just one you memorize and be done with it. As long as you can explain it as you go, it's fine
@davidoh0905
@davidoh0905 8 ай бұрын
why why why why why are we checking just the first character existing and only when it exists do you try dfs? it may have second character or third character? not only this video but also neetcode makes that assumption! omg! second character of the target string might exist in the sticker!!! what happens if we passed on a sticker that is going to be the only stick that's helpful to progress but we passed on it just because it was not holding the first character?
@crackfaang
@crackfaang 8 ай бұрын
I don't think you fully understood the algorithm. If we have to check every single character in the string it will be too computationally expensive. The key part here is that if there is a match on the first character of the string we are trying to build with a character in the sticker, we then take ALL the intersecting characters by using the set operations. Thus it saves us from having to check each character individually. If a solution exists, then doing it with just looking at the first character will eventually give us an answer because we continually take characters in a greedy manner from the sticker until we have matched all the characters. Conceptually it can be hard to see this but I'd recommend doing this problem line by line with a pen and paper and following the code in the video to see how the string changes and how we take characters.
@TheSmashten
@TheSmashten 2 ай бұрын
if I get this question in my Meta interview I might be cooked..
@krisd8687
@krisd8687 8 ай бұрын
Does this solution not work on leetcode anymore? The code seems to be returning -1 constantly and leetcode is rejecting the solution :(( Here is the code I used.. def minStickers(self, stickers: List[str], target: str) -> int: sticker_counts = [collections.Counter(stickers) for sticker in stickers] memo = {} def dfs(target_str): if not target_str: return 0 if target_str in memo: return memo[target_str] target_counter = collections.Counter(target_str) ans = float('inf') for sticker in sticker_counts: if target_str[0] not in sticker: continue remaining_characters = target_counter - sticker letters = [char * count for char, count in remaining_characters.items()] next_target = "".join(letters) ans = min(ans, 1 + dfs(next_target)) memo[target_str] = ans return ans ans = dfs(target) if ans != inf: return ans else: return -1
LONGEST UNIVALUE PATH | LEETCODE 687 | PYTHON DFS SOLUTION
14:46
Cracking FAANG
Рет қаралды 1,3 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 96 МЛН
ROBOT ROOM CLEANER | LEETCODE # 489 | PYTHON BACKTRACK SOLUTION
19:44
Cracking FAANG
Рет қаралды 12 М.
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 87 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 322 М.
VALID NUMBER | LEETCODE #65 | PYTHON SOLUTION
18:18
Cracking FAANG
Рет қаралды 7 М.
VALID PALINDROME III | LEETCODE 1216 | PYTHON MEMOIZED DFS SOLUTION
16:01
EXCLUSIVE TIME OF FUNCTIONS | LEETCODE 636 | PYTHON SOLUTION
15:42
Cracking FAANG
Рет қаралды 7 М.
Maximum Score Words Formed By Letters - Leetcode 1255 - Python
14:10
ACCOUNTS MERGE | LEETCODE # 721 | PYTHON SOLUTION
23:04
Cracking FAANG
Рет қаралды 13 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН