Тёмный

Longest String Chain - Leetcode 1048 - Python 

NeetCodeIO
Подписаться 176 тыс.
Просмотров 9 тыс.
50% 1

Опубликовано:

 

6 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 28   
@danielsun716
@danielsun716 11 месяцев назад
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 11 месяцев назад
I think he makes it complex. You're one is Good. Good work
@troytaylor4719
@troytaylor4719 11 месяцев назад
Love The Stuff Man,Just Learning Leetcode and the way you take things that look complex and make it seem so simple is genius
@vs3.14
@vs3.14 11 месяцев назад
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😅
@bidishadas832
@bidishadas832 2 месяца назад
This is the best explanation of this problem. I couldn't come up witht this.
@devkumar9889
@devkumar9889 11 месяцев назад
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
@reggiehurley1479
@reggiehurley1479 11 месяцев назад
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 5 месяцев назад
On second thought, it doesn't help actually. You are right
@AchyutSarmaB
@AchyutSarmaB 11 месяцев назад
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 11 месяцев назад
This was easier to understand for me than the solution shown in the video. Great job!
@michelle_tsai_drums
@michelle_tsai_drums 11 месяцев назад
Love the explanation and python's string slicing
@aasheesh6001
@aasheesh6001 11 месяцев назад
Great Explanation!!! Thanks for Daily Problem solutions!!!!
@gauthamsakthiveeran7946
@gauthamsakthiveeran7946 11 месяцев назад
Was waiting for this ! Thanks mate
@saiashishvure
@saiashishvure 11 месяцев назад
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 11 месяцев назад
codeforces not worth it unless u're targeting for icpc and you have enough time in hand
@saiashishvure
@saiashishvure 11 месяцев назад
@@iscoto4914 basically expand out from interview prep to problem solving in general. or atleast maybe once in a while
@roywastaken
@roywastaken 11 месяцев назад
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
@phpostrich
@phpostrich 11 месяцев назад
literally was looking for a video on this yesterday, and couldnt find on, thats so weird
@phpostrich
@phpostrich 11 месяцев назад
thanks for the help :)
@binfos7434
@binfos7434 Месяц назад
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
@rajanmaheshwari
@rajanmaheshwari 11 месяцев назад
For time complexity, we will not take sort into consideration?
@VidyaBhandary
@VidyaBhandary 11 месяцев назад
Genius explanation 🎉
@iscoto4914
@iscoto4914 11 месяцев назад
can someone explain me the line res = max(res, 1 + dfs(word_index[pred])) pls?
@user-hm8pj7mi7f
@user-hm8pj7mi7f 11 месяцев назад
Waiting this explanation 🎉
@memevideos7461
@memevideos7461 20 дней назад
isnt time complexity O(2^N * L * L * N)
@uddiptakalita3006
@uddiptakalita3006 11 месяцев назад
Can we solve this using DSU
@sumitsharma6738
@sumitsharma6738 11 месяцев назад
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
Далее
Champagne Tower - Leetcode 799 - Python
13:48
Просмотров 11 тыс.
Самое неинтересное видео
00:32
Просмотров 609 тыс.
ВОТ ЧТО МЫ КУПИЛИ НА ALIEXPRESS
11:28
Просмотров 857 тыс.
Find the Difference - Leetcode 389 - Python
11:24
Просмотров 10 тыс.
Parallel Courses III - Leetcode 2050 - Python
11:42
Просмотров 15 тыс.
Path with Minimum Effort - Leetcode 1631 - Python
14:35
Reverse Words in a String III - Leetcode 557 - Python
10:18
Flatten Nested List Iterator - Leetcode 341 - Python
10:14
Самое неинтересное видео
00:32
Просмотров 609 тыс.