Longest String Chain - Leetcode 1048 - Python

  Рет қаралды 9,595

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 28
@danielsun716
@danielsun716 Жыл бұрын
class Solution: def longestStrChain(self, words: List[str]) -> int: from collections import defaultdict # cache len of chain cache = defaultdict(int) # word: length of chain # initiate data data = sorted(words, key=lambda w: len(w)) for word in data: # from word, the len of chain is 1 at 1st cache[word] = 1 for index in range(len(word)): # construct predecessor predecessor = word[:index] + word[index + 1:] if predecessor in cache: cache[word] = max(cache[word], cache[predecessor] + 1) return max(cache.values())
@iscoto4914
@iscoto4914 Жыл бұрын
I think he makes it complex. You're one is Good. Good work
@troytaylor4719
@troytaylor4719 Жыл бұрын
Love The Stuff Man,Just Learning Leetcode and the way you take things that look complex and make it seem so simple is genius
@reggiehurley1479
@reggiehurley1479 Жыл бұрын
does sorting even help us for this implementation? For the non recursive dp it helps, but for the recursive version it doesn't help since we go thru all the words anyway. can someone confirm this for me cuz im not sure lol.
@shrn
@shrn 8 ай бұрын
On second thought, it doesn't help actually. You are right
@devkumar9889
@devkumar9889 Жыл бұрын
I don't know what to do , I was not able to think this straight even when I used hint from leetcode , But when I see some part of your solution , I was able to code it my self . I don't know where I am lagging , still not able to do many medium problems
@bidishadas832
@bidishadas832 5 ай бұрын
This is the best explanation of this problem. I couldn't come up witht this.
@AchyutSarmaB
@AchyutSarmaB Жыл бұрын
from collections import defaultdict from typing import List class Solution: def longestStrChain(self, words: List[str]) -> int: words.sort(key=len) # Sort the words by length to process shorter words first word_chain = defaultdict(int) # Store the longest chain for each word for word in words: for i in range(len(word)): prev_word = word[:i] + word[i+1:] word_chain[word] = max(word_chain[word], word_chain[prev_word] + 1) return max(word_chain.values())
@Raymond-Wu
@Raymond-Wu Жыл бұрын
This was easier to understand for me than the solution shown in the video. Great job!
@michelle_tsai_drums
@michelle_tsai_drums Жыл бұрын
Love the explanation and python's string slicing
@aasheesh6001
@aasheesh6001 Жыл бұрын
Great Explanation!!! Thanks for Daily Problem solutions!!!!
@gauthamsakthiveeran7946
@gauthamsakthiveeran7946 Жыл бұрын
Was waiting for this ! Thanks mate
@rajanmaheshwari
@rajanmaheshwari Жыл бұрын
For time complexity, we will not take sort into consideration?
@vs3.14
@vs3.14 Жыл бұрын
Love this explanation. Thanks man. One question, I have been following the 150 Qs from neetcode and then the leetcode 150 most asked. I started Trees yesterday. Is it possible to finish up the list within a month and understand everything? (Given, I have done every topic sequentially and Continuously review 3-4 of them each day) I am really worried about my upcoming interviews. And I find the important ones(Tree, Graph, DP) quite hard😅
@VidyaBhandary
@VidyaBhandary Жыл бұрын
Genius explanation 🎉
@phpostrich
@phpostrich Жыл бұрын
literally was looking for a video on this yesterday, and couldnt find on, thats so weird
@phpostrich
@phpostrich Жыл бұрын
thanks for the help :)
@iscoto4914
@iscoto4914 Жыл бұрын
can someone explain me the line res = max(res, 1 + dfs(word_index[pred])) pls?
@binfos7434
@binfos7434 3 ай бұрын
class Solution: def longestStrChain(self, words: List[str]) -> int: dp = {w: -1 for w in words} # {word: seqLen} def dfs(w): if w in dp: if dp[w] != -1: return dp[w] else: return 0 for i in range(len(w)): dp[w] = max(dp[w], 1 + dfs(w[:i] + w[i+1:])) return dp[w] res = 0 for i in words: res = max(res, dfs(i)) return res
@簡崇宇-b5z
@簡崇宇-b5z Жыл бұрын
Waiting this explanation 🎉
@memevideos7461
@memevideos7461 3 ай бұрын
isnt time complexity O(2^N * L * L * N)
@roywastaken
@roywastaken Жыл бұрын
i know the company in the thumbnail might not matter to you much, but TikTok has actually asked this problem more frequently (according to Leetcode stats) than any other company. So idk why Meta's logo is in the thumbnail when they've asked it very infrequently in comparison
@uddiptakalita3006
@uddiptakalita3006 Жыл бұрын
Can we solve this using DSU
@sumitsharma6738
@sumitsharma6738 Жыл бұрын
Yeah, my First thought is also Trie or DSU but the answer is noo once you try to draw the trie you will see why DSU will not work
@saiashishvure
@saiashishvure Жыл бұрын
yoo appreciate the daily upload, will you ever venture into codeforces questions, etc? i know they're not really asked in interviews, but will definitely help in improving problem solving skills and are quite fun in general
@iscoto4914
@iscoto4914 Жыл бұрын
codeforces not worth it unless u're targeting for icpc and you have enough time in hand
@saiashishvure
@saiashishvure Жыл бұрын
@@iscoto4914 basically expand out from interview prep to problem solving in general. or atleast maybe once in a while
Champagne Tower - Leetcode 799 - Python
13:48
NeetCodeIO
Рет қаралды 12 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 147 М.
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 2,8 МЛН
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 51 МЛН
When u fight over the armrest
00:41
Adam W
Рет қаралды 31 МЛН
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 166 МЛН
DP 45. Longest String Chain | Longest Increasing Subsequence | LIS
16:57
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 687 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 329 М.
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 852 М.
Last Stone Weight II - Leetcode 1049 - Python
19:04
NeetCodeIO
Рет қаралды 16 М.
Maximum Matrix Sum - Leetcode 1975 - Python
16:07
NeetCodeIO
Рет қаралды 341
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 577 М.
Count Ways to Build Good Strings - Leetcode 2466 - Python
14:09
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 2,8 МЛН