Freedom Trail - Leetcode 514 - Python

  Рет қаралды 14,368

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 47
@anand_dudi
@anand_dudi 7 ай бұрын
Only channel on whole youtube which is best not just solving leetcode problems but also to explaining the problem as much as simple possible with many approaches DAMM
@siddharth-gandhi
@siddharth-gandhi 7 ай бұрын
bro single handedly saving my streak. thanks for doing this :)
@yang5843
@yang5843 7 ай бұрын
These problem descriptions are getting out of hand
@vjnt1star
@vjnt1star 7 ай бұрын
I agree, a couple of months ago I was looking for a job and I was given some leetcode problems to solve. The description was very long and I had to read it a couple of times because it was going all over the place. After a while focusing on the example the things to do was not so complicated. But my god so much time wasted on blabla when the timer is running down at the same time
@huyennguyenmaingoc468
@huyennguyenmaingoc468 7 ай бұрын
Thank you Neetcode, I learned Algorithm from your series by watching the whole Medium playlist. Now I passed my Codility test and got the first internship, which I thought I couldn't 2 months ago XD
@williamdufault6413
@williamdufault6413 7 ай бұрын
python 80ms - recursion + memoization + binary search class Solution: def findRotateSteps(self, ring: str, key: str) -> int: @cache def dfs(r: int, k: int) -> int: if k == len(key): return 0 steps = math.inf for i in get_closest_indexes(r, key[k]): steps = min(steps, get_min_distance(r, i) + dfs(i, k + 1)) return 1 + steps def get_closest_indexes(i: int, char: str) -> Tuple[int]: if ring[i] == char: return (i,) char_indexes = indexes[char] if len(char_indexes) == 1: return tuple(char_indexes) l, r = 0, len(char_indexes) - 1 while l < r: m = l + (r - l) // 2 if char_indexes[m] < i: l = m + 1 else: r = m - 1 return ( char_indexes[l], char_indexes[l - 1] if char_indexes[l] > i \ else char_indexes[(l + 1) % len(char_indexes)] ) def get_min_distance(i: int, j: int) -> int: diff = abs(i - j) return min(diff, len(ring) - diff) indexes = defaultdict(list) for i, char in enumerate(ring): indexes[char].append(i) return dfs(0, 0)
@chaitanya812
@chaitanya812 7 ай бұрын
another problem with dp vs greedy ... where greedy fails
@yassine-sa
@yassine-sa 7 ай бұрын
I just saw now that this is the same problem we'll have to solve if we tried to type a string using a wheel that contains letters if we have the same mechanism as the those old phone wheels, the only difference is that phone wheels has has only numbers and they appear only once, this is a more general situation
@SaiPreethamDasari
@SaiPreethamDasari 7 ай бұрын
For the recursive solution with memoization, although it makes the code a little messier, you don't really need to consider all possible occurrences of a particular key character in the ring. Finding the first occurrence of the required character towards the left and the right and then recursively solving the rest of the problem is also adequate! But yeah great solution that helped me save my streak! Keep writing more neet code! :D
@swamysriman7147
@swamysriman7147 7 ай бұрын
No, that would be greedy and we'd miss better, but further positions
@Munchen888
@Munchen888 7 ай бұрын
Neetcode, thank you for detailed explanation specially when using recursion. A decision tree really helps to divide and conquer the problem. By the way dp solution are better. Thanks 😊
@yassine-sa
@yassine-sa 7 ай бұрын
Am I supposed to have a solution that works from the first try "normally"? because I always write a solution that works for basic cases and then I start finding bugs I didn't see after submitting the thing, I think that's because I don't completely see what my code is doing until I get a hint by the failed submissions that probably this part isn't working, how do you guys deal with this? is it just me or it's about me not trying to go over the code again and again to try and catch bugs before submitting the solution, if that's the case, is it really what should happens when coding in real life situations?
@thefreemarketdev
@thefreemarketdev 6 ай бұрын
The DP drawing is very helpful
@raghavrathi2412
@raghavrathi2412 7 ай бұрын
Please do yesterday's daily question i.e sum of distances in a tree, it seems like a really good question
@MP-ny3ep
@MP-ny3ep 7 ай бұрын
Phenomenal explanation as always. Thank you
@IK-xk7ex
@IK-xk7ex 7 ай бұрын
I recognise that it is DP problem, but I get stuck at the moment how to find circular offset, blain on me. After the moment you get the explanation I solved it by myself.
@tanzeembelal3177
@tanzeembelal3177 7 ай бұрын
Please solve contest problems for leetcode it will be so beneficial your quality of explanation is what we want please please please
@get_out_it
@get_out_it 7 ай бұрын
I’m adept at problem solving complex technical issues)
@tawfikkoptan5781
@tawfikkoptan5781 7 ай бұрын
PLEASE SOLVE TODAY'S CONTEST'S PROBLEM "Find All Possible Stable Binary Arrays I" BECAUSE IT WAS SO SO DIFFICULT 😭😭😭😭
@sheersendughosh
@sheersendughosh 7 ай бұрын
Thanks for the solution! If we come up with the caching solution and not the best optimal approach is it considered bad in an real interview? Can you kindly show/share code snippet for both caching solution and the optimal sol that you would do in a real FAANG interview? Love your videos❤
@satyamjha68
@satyamjha68 7 ай бұрын
Solved it!!
@hida-steak-donburi
@hida-steak-donburi 7 ай бұрын
Whoelse came up with Greedy solution and got suboptimal like me...
@ashrafuldowla6214
@ashrafuldowla6214 7 ай бұрын
recursive 2state dp with memoization
@aswathchandrasekar2917
@aswathchandrasekar2917 7 ай бұрын
Really good!!!!
@shahnawazhussain4925
@shahnawazhussain4925 7 ай бұрын
Hii. I have a request. Please make a video on the problem "1915: Number of wonderful substrings". No matter how hard i try i could not understand the logic. I watch your videos and i think you can explain it to me. And i am pretty sure i will understand if you make a video on this problem. Please take it as a request. Thanks. love your videos
@johnrivers9931
@johnrivers9931 7 ай бұрын
this was a pretty cool problem
@betabias
@betabias 7 ай бұрын
"wanna spend your life doing that" xD, tabulation gives me nightmares
@soumyajitchatterjee5822
@soumyajitchatterjee5822 7 ай бұрын
I need to practice more......
@nikhil199029
@nikhil199029 7 ай бұрын
21:45 Martin Fowler wants to know your location.
@Kauliflower-yl8te
@Kauliflower-yl8te 7 ай бұрын
To understand recursion you must first understand recursion
@benmaduabuchi1636
@benmaduabuchi1636 7 ай бұрын
solved it w a heap and it worked 💀
@pastori2672
@pastori2672 7 ай бұрын
btw in python you can just do res = inf or -inf its a shorter then float("inf")
@diasutsman
@diasutsman 6 ай бұрын
Bro the video embed in your courses are not working
@transient6366
@transient6366 7 ай бұрын
in desperate need for a good solution of 1915. Number of Wonderful Substrings cant find anywhere
@Mayanksingh-qp6dy
@Mayanksingh-qp6dy 7 ай бұрын
I have a question would be thankful if someone can please explain. In recursive solution if c == key[k] min_dist = min(abs(r-i), len - abs(r-i)) why are we taking this minimum instead of trying out both the solutions, as taking the minimum won't make this approach greedy?
@LOKESHE-wi2yd
@LOKESHE-wi2yd 7 ай бұрын
same doubt here , if the distance may decrease upon upcoming key values as he mentioned in 2:57 , why using greedy , i haven't watched full video yet , did he changed it in tabulation part
@LOKESHE-wi2yd
@LOKESHE-wi2yd 7 ай бұрын
gotcha , he selecting the minimum upon each recursive calls at line15 at timestamp 13:39
@deadlyecho
@deadlyecho 7 ай бұрын
Wouldn't it be easier if we concatenated the string with itself to make the recurssive solution easier ?
@ashrafuldowla6214
@ashrafuldowla6214 7 ай бұрын
you don't need to concatenate the string. try to find the distance between two indices both clockwise and anti-clockwise.
@deadlyecho
@deadlyecho 7 ай бұрын
@@ashrafuldowla6214 Yeah maybe, I just thought we could make it easier by doing this concat, didn't try it though
@yuvrajmalhotra9276
@yuvrajmalhotra9276 7 ай бұрын
what do u mean by easier ? r u trying to get rid of that formula to find distance if we move anticlockwise ? if this is tru then why stake all that space and you also have to write extra code.. just use a simple formula i.e key.size-absolute(nextPos-currentClockHand)
@ashrafuldowla6214
@ashrafuldowla6214 7 ай бұрын
@@yuvrajmalhotra9276 i did the same thing using the formula. I just only explained elaborately
@deadlyecho
@deadlyecho 7 ай бұрын
@@yuvrajmalhotra9276 Yes, well twice the length of the string is not so bad 2*N after all... didn't try how it will work out though just an idea
@wytsai7660
@wytsai7660 7 ай бұрын
4:55 Sorry, but I still can’t see why trying every characters (those three 'b's) won’t make our solution more inefficient 😢 Can somebody explain to me 🙏
@deadlyecho
@deadlyecho 7 ай бұрын
Vault-tec 😂😂😂
@pushkarsaini2
@pushkarsaini2 7 ай бұрын
where are you man?? 2 days no solutions
Minimum Cost to Hire K Workers - Leetcode 857 - Python
19:01
NeetCodeIO
Рет қаралды 15 М.
K Inverse Pairs Array - Leetcode 629 - Python
29:44
NeetCodeIO
Рет қаралды 16 М.
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 60 МЛН
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 20 МЛН
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 84 МЛН
Score After Flipping Matrix - Leetcode 861 - Python
22:30
NeetCodeIO
Рет қаралды 10 М.
Find the Safest Path in a Grid - Leetcode 2812 - Python
26:40
NeetCodeIO
Рет қаралды 15 М.
Longest Ideal Subsequence - Leetcode 2370 - Python
28:02
NeetCodeIO
Рет қаралды 12 М.
Become a Malloc() Pro
6:58
thedoubleeguy
Рет қаралды 3,4 М.
Student Attendance Record II - Leetcode 552 - Python
27:10
NeetCodeIO
Рет қаралды 9 М.
Cherry Pickup II - Leetcode 1463 - Python
24:00
NeetCodeIO
Рет қаралды 16 М.
Sum of All Subsets XOR Total - Leetcode 1863 - Python
18:26
NeetCodeIO
Рет қаралды 11 М.
Find All People With Secret - Leetcode 2092 - Python
14:35
NeetCodeIO
Рет қаралды 14 М.
Time Needed to Buy Tickets - Leetcode 2073 - Python
12:45
NeetCodeIO
Рет қаралды 14 М.
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 60 МЛН