ACCOUNTS MERGE | LEETCODE # 721 | PYTHON SOLUTION

  Рет қаралды 11,990

Cracking FAANG

Cracking FAANG

Күн бұрын

Пікірлер: 53
@Amy-nq3xw
@Amy-nq3xw 2 жыл бұрын
Your channel is such a hidden gem. I really like how you don't just solve all the mainstream LC questions but also the ones that are more advanced but useful. Thank you!!
@aglovecraft
@aglovecraft 2 жыл бұрын
This is a great explanation. I watched like 4 other videos of people explaining this problem and you're the only one who took the time to visually represent the problem. That step is so helpful in remembering the logic for these problems. Keep up the great work!
@crackfaang
@crackfaang 2 жыл бұрын
Thanks for the kind words! Based on my channel analytics a lot of people just watch the code portions but I’m glad people are getting value from the drawing parts too. Make sure to subscribe so you don’t miss future videos!
@谢雨潇
@谢雨潇 Жыл бұрын
Nice tutorial! I have a disagreement with the complexity calculation though. We have N accounts -> then N scattered graphs -> ForEach graph, we do traversal(K) + sorting(KlogK) = KlogK, finally O(NK logK). Ignore the nested-loop format, we still traverse every single node in one graph; after this traversal, we do one sorting. We should not have those many multiplications. Please correct me if I'm wrong.
@th2315
@th2315 Жыл бұрын
I really like the way you do a very detailed complexity analysis in the end, I learned quite a lot from that, and I don't see other users doing that much. Please keep doing so! Appreciated!
@hulyahare7718
@hulyahare7718 2 жыл бұрын
I've watch a lot of videos about this question but I couldn't understand any of them. But your vido is just GREAT, I get it finally! THANK YOU SO MUCHH for showing that it's actually very easy!
@crackfaang
@crackfaang 2 жыл бұрын
Glad you found it useful!
@rsKayiira
@rsKayiira 2 жыл бұрын
Was stuck on this problem but now I can do it. Thanks a lot. Glad I found the channel.
@3rd_iimpact
@3rd_iimpact 11 ай бұрын
Thanks for this. Will the interviewer be ok with this solution and not union-find?
@TooManyPBJs
@TooManyPBJs 7 ай бұрын
Great video. One thing I would nitpick is that you are visiting a node, not an edge.
@crackfaang
@crackfaang 7 ай бұрын
Yea I tend to mix the terminology. You get the idea though
@annlaosun6260
@annlaosun6260 5 ай бұрын
thank you. after watching so many dfs videos from you. I used a dfs function to code this question and I was able to code it. !
@electric336
@electric336 2 жыл бұрын
Very good explanation. Probably the best I've seen regarding this problem.
@portiseremacunix
@portiseremacunix 2 жыл бұрын
Thanks! Hope to see comments of the code if possible...somewhere in a github?
@markthompson2813
@markthompson2813 Жыл бұрын
For the time complexity, wouldn't the sorting time for K items be K Log K? That would make the ultimate Time complexity O( N * K * N * K * Log K) -> O(N^2 K^2 Log K) ?
@geekydanish5990
@geekydanish5990 Жыл бұрын
correct
@syafzal273
@syafzal273 8 ай бұрын
Love your explanation! I feel like the DFS would've been more intuitive recursively instead of iteratively. (This is your code, with just using recursive DFS) class Solution: def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]: graph = collections.defaultdict(set) # map of email -> name email_to_name = {} # build a graph for account in accounts: name = account[0] for email in account[1:]: # just connect to the first email # instead of connecting each email to # every other one graph[email].add(account[1]) graph[account[1]].add(email) email_to_name[email] = name res = [] visited = set() cur = [] # return all emails connected to the email def dfs(email): if email in visited: return visited.add(email) cur.append(email) for nxt in graph[email]: dfs(nxt) for email in graph: if email not in visited: dfs(email) res.append([email_to_name[email]] + sorted(cur)) cur = [] return res
@무야호-w8g
@무야호-w8g 2 жыл бұрын
if possible, can you show the recursive way to implement DFS for this question?
@hudsonyuen
@hudsonyuen Жыл бұрын
This worked for me - not the most space efficient tho class Solution(object): def accountsMerge(self, accounts): """ :type accounts: List[List[str]] :rtype: List[List[str]] """ graph = {} emailNameMap = {} for account in accounts: name = account[0] for email in account[1:]: if email not in graph: graph[email] = set() # connect every email to the first one for efficiency # we know there will be >= 1 email, so index 1 in bounds graph[email].add(account[1]) graph[account[1]].add(email) emailNameMap[email] = name visited = set() def dfs(email, connected): for email in graph[email]: if email in visited: continue visited.add(email) connected.append(email) dfs(email, connected) return connected res = [] for email in graph: # return array of all emails connected to that email, including itself connected = dfs(email, []) # if no connected emails that are unvisited, it's already been added if len(connected) == 0: continue connected.sort() # append name to front, then append to res array res.append([emailNameMap[email]] + connected) return res
@callmejustlenny
@callmejustlenny 11 ай бұрын
i like your sarcastic tone in the video! Keep it up!
@rachnaramkumar
@rachnaramkumar 2 жыл бұрын
Amazing Explanation! Thank you so much!
@fallencheeto4762
@fallencheeto4762 8 ай бұрын
Explanation is amazing as always, just subbed!
@nehanarwal2944
@nehanarwal2944 2 жыл бұрын
Great explanation! Can you also add solution to Calender I problem on leetcode. Thanks!
@ebenezeracquah478
@ebenezeracquah478 Жыл бұрын
This is great man!. Thank you so much.
@akhilchandra5935
@akhilchandra5935 2 жыл бұрын
lol i love the intro about karma!!
@crackfaang
@crackfaang 2 жыл бұрын
Hehe got to get you guys to subscribe somehow!
@deezeey5811
@deezeey5811 2 жыл бұрын
Thanks for the solution! Btw why is this DFS not BFS?
@crackfaang
@crackfaang 2 жыл бұрын
Doesn’t matter. You have to fully explore all paths. Both will work here though I guess when you want to fully explore a path you typically use DFS in theory but it doesn’t matter. Just personal preference here
@bdjsjjs
@bdjsjjs Жыл бұрын
I think what he uses is BFS instead of DFS. That's my thought. Correct me if I'm wrong.
@bdjsjjs
@bdjsjjs Жыл бұрын
Oh, it's a dfs
@jimmyahmed5424
@jimmyahmed5424 2 жыл бұрын
Thanks for explaining!
@preethisowjanya5203
@preethisowjanya5203 5 ай бұрын
thank you!
@sonalverma5981
@sonalverma5981 2 жыл бұрын
Can it be done using union find algo?
@geekydanish5990
@geekydanish5990 Жыл бұрын
yes
@leetcoderafeeq2641
@leetcoderafeeq2641 Жыл бұрын
Thanks!
@nehanarwal2944
@nehanarwal2944 2 жыл бұрын
One question - Why would sorting be Nlog K and not k log k
@read200books
@read200books 2 жыл бұрын
Following
@fadsa342
@fadsa342 Жыл бұрын
I'm still a little confused about how he created the graph and why you only have to connect each email to the first one in the list instead of connecting each email in the list to all the others
@crackfaang
@crackfaang Жыл бұрын
It's simpler to do it this way. You will still be able to traverse the entire graph by only populating that first email. If you were to populate every single possible connection bidirectionally it would drastically increase the processing time and the storage space. There's no reason to do so when only populating the first will allow you to traverse the graph with the same result. For this one I'd suggest drawing out an example on a piece of paper and going through the algorithm line by line to understand. That's what helped me when I first came across this problem
@SeanKearney-g7d
@SeanKearney-g7d 2 ай бұрын
cool
@AmgaaKhosbayar
@AmgaaKhosbayar 4 ай бұрын
For the good karma!
@benjaminnguyen592
@benjaminnguyen592 2 жыл бұрын
Great explanation! Keep it up man
@crackfaang
@crackfaang 2 жыл бұрын
Thank you! Will do! Make sure to keep watching 😃
@mateo9912
@mateo9912 Ай бұрын
Need the good karma
@vigneshsampath3256
@vigneshsampath3256 2 жыл бұрын
I subscribed. Hope to get the good Karma.
@crackfaang
@crackfaang 2 жыл бұрын
Thank you! Glad people are hearing the message. If only more people did the same haha I would have a lot more subscribers! 😀
@NinjiaJoeLife
@NinjiaJoeLife 2 жыл бұрын
really nice
@crackfaang
@crackfaang 2 жыл бұрын
Thanks for the kind words. Make sure to subscribe so you don’t miss future videos
@nguyentuan6108
@nguyentuan6108 2 жыл бұрын
subscribed!
@crackfaang
@crackfaang 2 жыл бұрын
Welcome to the channel
@Lily-xp2vb
@Lily-xp2vb Жыл бұрын
i subscribed long time ago, but I need more good karma, please send me good energy!
@sharwariphadnis1298
@sharwariphadnis1298 6 ай бұрын
Thanks for the clear explanation @crackfaang! Could you please confirm the time complexity?
@lalalagogo8508
@lalalagogo8508 8 ай бұрын
lol good karma
Accounts Merge - Leetcode 721 - Python
17:13
NeetCodeIO
Рет қаралды 33 М.
WORD BREAK | PYTHON | LEETCODE # 139
18:04
Cracking FAANG
Рет қаралды 4,7 М.
МЕНЯ УКУСИЛ ПАУК #shorts
00:23
Паша Осадчий
Рет қаралды 5 МЛН
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 12 МЛН
LOWEST COMMON ANCESTOR OF A BINARY TREE III [PYTHON]
16:38
Cracking FAANG
Рет қаралды 12 М.
LeetCode 721. Accounts Merge [Coding Interview Solution Explained]
14:30
SHORTEST PATH IN A BINARY MATRIX | LEETCODE 1091 | PYTHON BFS SOLUTION
14:03
MAKING A LARGE ISLAND | LEETCODE # 827 | PYTHON SOLUTION
22:09
Cracking FAANG
Рет қаралды 8 М.
Minimum Time Difference - Leetcode 539 - Python
21:43
NeetCodeIO
Рет қаралды 8 М.
Machine Learning for Everybody - Full Course
3:53:53
freeCodeCamp.org
Рет қаралды 8 МЛН
Minimum Height Trees - Leetcode 310 - Python
23:30
NeetCodeIO
Рет қаралды 21 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 300 М.
The OTHER AI Alignment Problem: Mesa-Optimizers and Inner Alignment
23:24
Robert Miles AI Safety
Рет қаралды 233 М.