Generate Parentheses - Leetcode 22 - Recursive Backtracking (Python)

  Рет қаралды 5,268

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер: 15
@GregHogg
@GregHogg 4 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@saaddude1
@saaddude1 5 ай бұрын
Love you explanations, by far the best on the internet! It makes these problems so much easier to understand and code. Please upload DSA 150 as these will be more helpful for those preparing for interviews.
@GregHogg
@GregHogg 5 ай бұрын
Thank you! And I'll try haha
@Redaxi
@Redaxi Ай бұрын
When im stuck at backtracking i listen to your explanations without looking at your code and in the middle of the explanation something just clicks and then i go and am able to solve it immediately. Hopefully with time i can start solving without the need of your explanations ^^ Thanks for everything tho
@ArdianUmam
@ArdianUmam 2 ай бұрын
Thanks, Greg! Btw, wanna share the modified version using a string, instead of a list. We can just append each character "(" or ")" to the string in the function parameter. class Solution: def generateParenthesis(self, n: int) -> List[str]: result = [] def rec(n_open, n_close, parenthesis): if len(parenthesis) == (2*n): result.append(parenthesis) return if n_open < n: rec(n_open+1, n_close, parenthesis+"(") if n_close < n_open: rec(n_open, n_close+1, parenthesis+")") rec(n_open=0, n_close=0, parenthesis="") return result
@chandlerkenworthy3185
@chandlerkenworthy3185 2 ай бұрын
The problem with this approach is that adding characters to a string is in general an O(n) operation but appending to a list is generally O(1). It is therefore much faster to use a list and join at the end.
@SaPpHiRe051918
@SaPpHiRe051918 Ай бұрын
Subbed. Great explanation. Also base condition could be if (open==n && closed==n) then append.
@SelftaughtSoftwareEngineer
@SelftaughtSoftwareEngineer 2 ай бұрын
Thanks for the video! What software did you use for the whiteboarding? Do you use a special pen or just a mouse to write?
@tushitapatel5782
@tushitapatel5782 9 күн бұрын
With n=8, there are 1430 combinations. That's 1430 combinations of size (2*n). The space complexity is not O(n).
@tushitapatel5782
@tushitapatel5782 9 күн бұрын
What is it??
@derrickagyemangduah2353
@derrickagyemangduah2353 8 күн бұрын
@@tushitapatel5782 O(2^n)
@anton_melnikov
@anton_melnikov 3 ай бұрын
i dont understand two things: 1. why do we need to pop after we add a parenthesis. if we pop, wouldn't sol be an empty list (and then how an empty list would be used in appending to ans?)? 2. why would this algorithm not stop after getting the first combination "((()))"?
@ahmadbasyouni9173
@ahmadbasyouni9173 3 ай бұрын
My advice to you is to use pen and paper and go through the tree it will click in, but the way i like to understand it is to look all the way at the first case you wanna ignore the deeper levels of the tree so if u look at 4:13 initially we are able to add open, so thats the first if statement, and we will add it and call the backtracking function for when u have an open. But when all of that finishes to the left of the root, we need to backtrack up and explore the right branch as if we didnt add a opeing (add closing). Therefore we have the pop at the end of first if. The second pop in the second if statement is if you are in the left tree still at 4:13 and at that call went down the second if statement you see how u add a close parantheses but what happens after this call is u need to back track back up so ur able to explore the right side of the root like we said therefore u need to "clean up".
@rahulreddy4944
@rahulreddy4944 2 ай бұрын
The TC is wrong
Word Search - Leetcode 79 - Recursive Backtracking (Python)
12:24
Generate Parentheses - Stack - Leetcode 22
13:37
NeetCode
Рет қаралды 377 М.
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2,1 МЛН
What type of pedestrian are you?😄 #tiktok #elsarca
00:28
Elsa Arca
Рет қаралды 36 МЛН
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 98 МЛН
Чистка воды совком от денег
00:32
FD Vasya
Рет қаралды 2,7 МЛН
Recursive Backtracking - DSA Course in Python Lecture 14
12:58
House Robber - Leetcode 198 - Dynamic Programming (Python)
13:15
Daily Temperatures - Leetcode 739 - Stacks (Python)
12:35
Greg Hogg
Рет қаралды 7 М.
11. Generate Parantheses | The recursion question that I have asked the most in interviews! 🚀🚀
15:30
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 253 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 674 М.
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2,1 МЛН