No video

Find Eventual Safe States - Leetcode 802 - Python

  Рет қаралды 25,473

NeetCode

NeetCode

Күн бұрын

Пікірлер: 37
@amegahed94
@amegahed94 Жыл бұрын
This is a super neat solution. Initially, I was approaching this with a visited and current_path sets & was doing backtracking on both :) Now the code is much cleaner! Thank you for this video.
@joelpww
@joelpww 2 жыл бұрын
4 has an outgoing edge
@awekeningbro1207
@awekeningbro1207 2 жыл бұрын
and
@vivekgagrani8288
@vivekgagrani8288 2 жыл бұрын
So 4 should not be a terminal node Isn't it
@_7__716
@_7__716 2 жыл бұрын
But it leads to a terminal node which makes it a safe node
@vivekgagrani8288
@vivekgagrani8288 2 жыл бұрын
@@_7__716 yes agree, but in video during explanation, he said 4 is a terminal node.
@river.
@river. Жыл бұрын
@@vivekgagrani8288 bhawnao ko samjho
@tommysohn2747
@tommysohn2747 Жыл бұрын
such a elegant way to detect loops
@sidazhong2019
@sidazhong2019 9 ай бұрын
This problem is genius. Usually, we use a visited set() in such graph problems to detect a circle. But not for this question. A visited node may visit a second time since one node connects to many nodes. So, it uses a hashmap instead of the visited set! The default set value to False. Once the node reaches the end, it changes to True. Damn it, I didn't come up with it.
@Sulerhy
@Sulerhy 4 ай бұрын
Same here bro, thank you
@juhairahamed5342
@juhairahamed5342 9 ай бұрын
Good Explanation. Get new way of thinking
@CineVibe69
@CineVibe69 Жыл бұрын
This is Superbb.. Very clean solution
@rithickchowdhury7116
@rithickchowdhury7116 Жыл бұрын
Thanks for making this super easy 🙌
@AlancRodriguez
@AlancRodriguez Жыл бұрын
A genius can make a complex topic seem simple
@mehulsolanki9435
@mehulsolanki9435 2 жыл бұрын
This is illegal. You can't make a problem sound soo simple !!
@Man_of_Culture.
@Man_of_Culture. 2 жыл бұрын
Solution optimized with dp (safe vector used as dp) concept class Solution { public: bool dfs(int u,vector&visited,vector&safe,vector& graph) { visited[u]=1; bool isSafe=1; for(auto v:graph[u]) { if(visited[v]) isSafe&=safe[v]; else isSafe&=dfs(v,visited,safe,graph); } return safe[u]=isSafe; } vector eventualSafeNodes(vector& graph) { int n=graph.size(); vectorvisited(n,0),safe(n,0); for(int i=0;i
@nishantingle1438
@nishantingle1438 2 жыл бұрын
Variation of Topological Sort
@halahmilksheikh
@halahmilksheikh 2 жыл бұрын
Yeah it's a modification of topological sort. Basically asking to find nodes that aren't part of a cycle. Here's a solution that uses topo sort DFS using a white gray black implementation. It's also O(V+E) uses the same visited each dfs call. /** * @param {number[][]} graph * @return {number[]} */ var eventualSafeNodes = function(graph) { let visited = new Array(graph.length).fill(0) let res = [] for (let i = 0; i < graph.length; i++) { if (dfs(i)) { res.push(i) } } return res // return true if not part of a cycle // false if part of cycle function dfs(i) { if (visited[i] == 1) { return false } if (visited[i] == 2) { return true } visited[i] = 1 for (let nei of graph[i]) { if (dfs(nei) == false) { return false } } visited[i] = 2 return true } };
@alexthezhang
@alexthezhang Жыл бұрын
Thank you!!!
@vineethnc8934
@vineethnc8934 2 жыл бұрын
Please solve Leetcode 321..
@schan263
@schan263 23 күн бұрын
I just finished this question but my implementation is not as clean as yours.
@tommasodonato991
@tommasodonato991 Жыл бұрын
No algoexpert, for the 1000th time, i don’t want to be a software engineer at google. Thank you
@a4addel
@a4addel 2 ай бұрын
It's the bitches showing off there boobs in there ads that drive me nuts, like bro this is not Pornhub here.
@arunks4918
@arunks4918 2 жыл бұрын
And third. Done guys.
@abhaytiwari5991
@abhaytiwari5991 2 жыл бұрын
Fourth 😁😁😁😁
@mattmendez8860
@mattmendez8860 7 ай бұрын
This solution doesn't work anymore BTW, gives TLE
@darshanputtaswamy3199
@darshanputtaswamy3199 2 жыл бұрын
Second
@akankshasharma7498
@akankshasharma7498 Жыл бұрын
I cannot believe I couldn't solve it on my own 😥😥
@vikassharma-th7kn
@vikassharma-th7kn Жыл бұрын
us moment bhai!!!!
@lakshyasaharan5348
@lakshyasaharan5348 2 жыл бұрын
First
@harigovind11
@harigovind11 2 жыл бұрын
Thanks for the great explanation! Small suggestion. Better name for dfs function would make the program more readable.
@Marcox385
@Marcox385 2 жыл бұрын
Does it? I mean, depth first search it's a pretty know algorithm with easily identifiable initial, I think it's good the way it is
@harigovind11
@harigovind11 2 жыл бұрын
@@Marcox385 What I meant is making the method name isSafe or something would make it easier to follow the logic once we know it's DFS. But understand some others like the way it is.
@siqb
@siqb 2 жыл бұрын
nahh you gotta see his style. It is consistent. That's far more important IMHO. I recognize the dfs immediately.
@starstarhaha
@starstarhaha 10 ай бұрын
in work,good name is necessary ,here not necessary
@mahesh_kok
@mahesh_kok Жыл бұрын
This is Amazing... I have written code following all your videos and code looks like this: def eventualSafeNodes(self, graph: List[List[int]]) -> List[int]: output = [] for index, ele in enumerate(graph): for inner_ele in ele: output.append((index, inner_ele)) new_graph = {i: [] for i in range(len(graph))} for src, dst in output: new_graph[src].append(dst) if dst not in new_graph: new_graph[dst] = [] stack = [] result = {} def dfs(vertex): if vertex in result: return result[vertex] if vertex in stack: while stack: vertex = stack.pop() result[vertex] = False return False stack.append(vertex) for neighbor in new_graph[vertex]: if not dfs(neighbor): return False if stack: stack.pop() result[vertex] = True return True final_result = [] for node in new_graph: if node not in result: if dfs(node): final_result.append(node) elif result[node]: final_result.append(node) return final_result
Evaluate Division - Leetcode 399 - Python
17:37
NeetCodeIO
Рет қаралды 28 М.
Snakes and Ladders - Leetcode 909 - Python
21:22
NeetCode
Рет қаралды 50 М.
Dad gives best memory keeper
01:00
Justin Flom
Рет қаралды 20 МЛН
Fake watermelon by Secret Vlog
00:16
Secret Vlog
Рет қаралды 3,1 МЛН
女孩妒忌小丑女? #小丑#shorts
00:34
好人小丑
Рет қаралды 98 МЛН
G-20. Find Eventual Safe States - DFS
23:43
take U forward
Рет қаралды 165 М.
I Feel Bad For New Programmers
19:12
ThePrimeTime
Рет қаралды 438 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 201 М.
Open the Lock - Leetcode 752 - Python
14:22
NeetCode
Рет қаралды 39 М.
Shortest Path with Alternating Colors - Leetcode 1129 - Python
13:43
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 302 М.
Shortest Bridge - Leetcode 934 - Python
14:51
NeetCode
Рет қаралды 37 М.
How Fast can Python Parse 1 Billion Rows of Data?
16:31
Doug Mercer
Рет қаралды 216 М.
Contiguous Array - Leetcode 525 - Python
15:41
NeetCodeIO
Рет қаралды 23 М.
Dad gives best memory keeper
01:00
Justin Flom
Рет қаралды 20 МЛН