Тёмный
NeetCode
NeetCode
NeetCode
Подписаться
Current NEET and ex-Google SWE, also I love teaching!

N.E.E.T. = (Not in education, employment or training)

Preparing for coding interviews? Checkout neetcode.io
The LeetCode Fallacy
6:08
2 месяца назад
Most Common Concepts for Coding Interviews
6:08
8 месяцев назад
Leetcode 24 Hour Challenge (while learning Golang)
24:17:16
8 месяцев назад
I quit Amazon after two months
10:09
8 месяцев назад
Google's Tech Stack (6 internal tools revealed)
9:09
10 месяцев назад
My Last Day at Google
4:59
Год назад
GPT-4 is OVERHYPED
3:51
Год назад
Will AI replace programmers?
4:08
Год назад
Is FAANG dead?
6:28
Год назад
Tech Layoffs & Hiring Freezes
5:57
Год назад
Комментарии
@akire1711
@akire1711 4 минуты назад
I like you’re hat.
@adityatiwari2412
@adityatiwari2412 13 минут назад
Thanks for a clear explanation!
@prathikthotre8680
@prathikthotre8680 2 часа назад
A course in dsa using python please
@mananhingorani2551
@mananhingorani2551 4 часа назад
Leetcode weekly 402, almost similar question came (Q3)
@aurkom
@aurkom 4 часа назад
The pythonistas that want to use slicing, here, def rotate(self, nums: List[int], k: int) -> None: n = len(nums) k = k%n nums[0:n-k] = nums[0:n-k][::-1] nums[n-k:n] = nums[n-k:n][::-1] nums[0:n] = nums[::-1]
@ShreksSpliff
@ShreksSpliff 4 часа назад
Watching this after attempting on NeetCode is soooo good!
@davea136
@davea136 6 часов назад
By doing the one-liner you end up guaranteed going over every element in the array. By checking the hashset on each element you will most likely get better performance, except in a degenerate case.
@user-qt1lm3yf5j
@user-qt1lm3yf5j 6 часов назад
yea this is just a lot of case work, easy to miss smtg
@awesome_ashu
@awesome_ashu 6 часов назад
I hope this explanation helps someone who found this problem tricky - If two strings are of same length, how can be determine if one is a permutation of the other? One way is to check the frequencies of all the chars in s1 and s2. If they are exactly the same, that means they are permutations of each other. How many matches are needed? That will be equal to the number of characters allowed. In this case, the problem mentions that all are lowercase, so we need 26 matches. We start by initializing the freq of first n characters of s1. We do this for s2 as well (for its n characters only). If the freq of all 26 chars match at this point, we can simply return true. Otherwise, we will use sliding window to look for other substrings. We shift the window by 1 step. The freq of new char on right will increase. The freq of left most char in the previous window will decrease. Then, we can again check if the freq of all the 26 chars is exactly the same. If yes, they must be permutations of each other. So, return true. ```java class Solution { public boolean checkInclusion(String s1, String s2) { int n = s1.length(); int m = s2.length(); if(n > m) return false; int[] f1 = new int[26]; int[] f2 = new int[26]; for(int i=0; i<n; i++) f1[s1.charAt(i)-'a']++; for(int i=0; i<n; i++) // we will iterate only till n chars since those will form the initial permutation f2[s2.charAt(i)-'a']++; if (Arrays.equals(f1, f2)) return true; int idx = n; // start of next window while(idx < m){ // we have moved the window one step towards right // so freq of char at idx should increase // and freq of left most char in prev window should decrease char newChar = s2.charAt(idx); char prevChar = s2.charAt(idx-n); f2[newChar-'a']++; f2[prevChar-'a']--; if (Arrays.equals(f1, f2)) return true; idx++; } return false; } } ``` ### OPTIMIZED The previous solution will take O(26\*N) time complexity. We can optimize it further by using a `matches` variable to track the number of matched frequencies in s1 and s2. If it equals 26, we can return true. ```java class Solution { public boolean checkInclusion(String s1, String s2) { int n = s1.length(); int m = s2.length(); if(n > m) return false; int[] f1 = new int[26]; int[] f2 = new int[26]; for(int i=0; i<n; i++){ f1[s1.charAt(i)-'a']++; f2[s2.charAt(i)-'a']++; } int matches = initializeMatch(f1, f2); if (matches == 26) return true; int idx = n; // start of next window while(idx < m){ // we have moved the window one step towards right // so freq of char at idx should increase // and freq of left most char in prev window should decrease char newChar = s2.charAt(idx); char prevChar = s2.charAt(idx-n); // update freq for new character in the next window f2[newChar-'a']++; if(f2[newChar-'a'] == f1[newChar-'a']) matches++; // IMPORTANT: // we should reduces matches, if they were equal before the update for this char // one way to check if the freq increased by exactly 1 after update // which means, they must have been same before this update else if(f2[newChar-'a'] - 1 == f1[newChar-'a']) matches--; // IMPORTANT // if we update freq of both newChar and prevChar at the same time, that will not be correct // update freq for prev char (left most) of the prev window f2[prevChar-'a']--; if(f2[prevChar-'a'] == f1[prevChar-'a']) matches++; else if(f2[prevChar-'a'] + 1 == f1[prevChar-'a']) matches--; if(matches == 26) return true; idx++; } return false; } public int initializeMatch(int[] f1, int[] f2){ int count = 0; for(int i=0; i<26; i++){ if(f1[i] == f2[i]) count++; } return count; } } ``` gkgaurav31.github.io/posts/permutation-in-string/
@SHARMATUSHAR1_
@SHARMATUSHAR1_ 8 часов назад
Reason it works: Initially: (l+r)/2 Add and subtract l in the numerator (l-l+r+l)/2 (2l + r-l)/2 2l/2 +(r-l)/2 l + (r-l)/2
@RahulJain-ye4gz
@RahulJain-ye4gz 8 часов назад
""" 1 / \ 2 3 / \ \ 4 5 6 Initialization res = [] (to store the right side view) q = deque([1]) (initial queue containing the root node) Level 1 rightSide = None qLen = len(q) = 1 Process the level: node = q.popleft() (pops 1 from the queue) rightSide = 1 Add children of 1 to the queue: q.append(2), q.append(3) rightSide is 1, so res.append(1): res = [1] Queue now contains: [2, 3] Level 2 rightSide = None qLen = len(q) = 2 Process the level: node = q.popleft() (pops 2 from the queue) rightSide = 2 Add children of 2 to the queue: q.append(4), q.append(5) node = q.popleft() (pops 3 from the queue) rightSide = 3 Add children of 3 to the queue: q.append(None), q.append(6) rightSide is 3, so res.append(3): res = [1, 3] Queue now contains: [4, 5, None, 6] Level 3 rightSide = None qLen = len(q) = 4 Process the level: node = q.popleft() (pops 4 from the queue) rightSide = 4 No children to add to the queue node = q.popleft() (pops 5 from the queue) rightSide = 5 No children to add to the queue node = q.popleft() (pops None from the queue) Skip since node is None node = q.popleft() (pops 6 from the queue) rightSide = 6 No children to add to the queue rightSide is 6, so res.append(6): res = [1, 3, 6] Queue is now empty, so we exit the while loop Result The final right side view of the binary tree is [1, 3, 6].
@skyjoe3655
@skyjoe3655 8 часов назад
this is easy to understand but hard to write running code without bugs
@RahulJain-ye4gz
@RahulJain-ye4gz 10 часов назад
""" 1 / \ 2 5 / \ \ 3 4 6 Step 1: Flatten the left subtree of 1 Call: dfs(1) Call: dfs(2) Call: dfs(3) root.left and root.right are None Return 3 (last node in the left subtree of 2) Call: dfs(4) root.left and root.right are None Return 4 (last node in the right subtree of 2) leftTail = 3, rightTail = 4 Set 3.right = 4 Set 2.right = 3, 2.left = None Return 4 (last node in the flattened subtree of 2) Step 2: Flatten the right subtree of 1 Call: dfs(5) Call: dfs(None) Return None Call: dfs(6) root.left and root.right are None Return 6 (last node in the right subtree of 5) leftTail = None, rightTail = 6 Return 6 (last node in the flattened subtree of 5) Step 3: Combine left and right subtrees of 1 leftTail = 4, rightTail = 6 Set 4.right = 5 Set 1.right = 2, 1.left = None Return 6 (last node in the flattened tree) """
@JShaker
@JShaker 11 часов назад
The justification for using NoSQL was kind of lacking.Relational database do just fine with high read throughput. I don't think it's necessarily a bad choice, I'd say either SQL or NoSQL would be fine.
@georgplaz
@georgplaz 12 часов назад
if python had int overflows, this would still not have been a bug, but a litation. also, r-l can produce an underflow. if r is relaly low and l really big. this didn't change anything
@NeetCode
@NeetCode 11 часов назад
r will always be larger than l, so your second point is incorrect
@suketu1
@suketu1 12 часов назад
thanks for video. Nice explanation. path compression can be improved here. def find(self, p): if self.parent[p] != p: self.parent[p] = self.find(self.parent[p]) # Path compression return self.parent[p]
@heavenstone3503
@heavenstone3503 12 часов назад
Ah yes, thr big old python int overflow
@divyanshsingh6668
@divyanshsingh6668 12 часов назад
The way I did it was to store it in a hashset and compared the sizes of array and the set, if equal then no duplicate in c++ 🤣🤣
@blackvirus9
@blackvirus9 12 часов назад
Toxic Culture, favoritism, Managers who have no idea what they are doing, resumes Amazon in a nutshell.
@nishantaanjaneyjalan8583
@nishantaanjaneyjalan8583 12 часов назад
You talk leetcode at parties? And friends call me an insufferable nerd.
@Dhruvbala
@Dhruvbala 13 часов назад
In place of the visit hashset, you could just set prereq[crs] = None whenever you've added it to the output list. Kind of like what you did for the other problem
@tomonkysinatree
@tomonkysinatree 13 часов назад
The way you write the solution out in code, it seems simple, but I am almost certain the edge cases with this approach would have totally stumped me if I get this in an interview
@VidNudistKid
@VidNudistKid 15 часов назад
Isn't the Python 'int' built-in type capable of storing arbitrarily large integers?
@impossikour3309
@impossikour3309 15 часов назад
Yes, but underneath it allocated 32 bits of memory to the value. When you add two of those numbers together, the memory slot doesnt have enough space to store that, causing an error.
@darthwalsh1
@darthwalsh1 13 часов назад
​@@impossikour3309 If your system has such limited memory that allocating an extra 32 bits crashes with OOM, the bug wasn't in the arithmetic: the bug was choosing python
@ipizza9941
@ipizza9941 12 часов назад
​@@impossikour3309it reallocates to a higher space.
@baleygrsteysionfayf9818
@baleygrsteysionfayf9818 15 часов назад
why do you sound like daily dose of internet
@davitmodebadze9707
@davitmodebadze9707 16 часов назад
A.man who enjoyes coding and teaching how to code, thanks a lot for the effort
@bommireddyvenkatadheerajre4999
@bommireddyvenkatadheerajre4999 16 часов назад
After seeing your videos my brain was so sharp and able to do this question on my own
@galkk3
@galkk3 17 часов назад
came up with similar solution, with only 2 parameters for the inner function: res = [] def backtrack(i, total): if i == len(candidates) or sum(total) > target: return if sum(total) == target: res.append(total.copy()) return backtrack(i, total + [candidates[i]]) backtrack(i + 1, total) backtrack(0, []) return res
@fraserdab
@fraserdab 17 часов назад
Imagine telling a python user about integer overflow 💀
@prod_by_slvg
@prod_by_slvg 17 часов назад
Ok but how quickly can you pull some bitches
@priyanshuganatra
@priyanshuganatra 18 часов назад
Ahh the good old integer overflow that everyone knows about these days
@IronicHavoc
@IronicHavoc 15 часов назад
It's the cause of bug maybe .001% of the time, but if you randomly mention it as a possibility every so often people will be impressed
@alansun697
@alansun697 14 часов назад
I thought everyone (studying CS) knew about integer overflow?
@FourOneNineOneFourOne
@FourOneNineOneFourOne 11 часов назад
it's actually a very common bug and it's immediately obvious what went wrong when it does, so it gets fixed quickly. When someone makes an N+1 bug you find it 5 years later when the amount of accumulated data halts the query to a halt.
@CarlJohnson-iv7sn
@CarlJohnson-iv7sn 8 часов назад
Another reason to not start with python
@muscleman6473
@muscleman6473 18 часов назад
why doesnt the value of arr[m+k] ever go outside the length of arr?
@HemprasanthTech
@HemprasanthTech 19 часов назад
Thanks for great explanation. I can follow along once we decide l=start and r=end. I was stuck with l=start and l=start+1. How to decide where to place the 2 pointers initially?
@Ebrahim-gj9ko
@Ebrahim-gj9ko 19 часов назад
The most easy problem.. It’s implemente the basic recursion
@mightyprogrammer2899
@mightyprogrammer2899 19 часов назад
Somehow Leetcode has deleted this problem
@spongebobsquarepants4576
@spongebobsquarepants4576 22 часа назад
Great explanation! Thank you so much
@manibhushankumarsingh5196
@manibhushankumarsingh5196 22 часа назад
I had solve this problem only after seeing your diagram.
@mohammedabdel-monsef7571
@mohammedabdel-monsef7571 23 часа назад
thanks a lot !
@tomcat9761
@tomcat9761 День назад
8:08 "But I'll just put a return for no reason." 🤣
@introvertwhiz-ll2ip
@introvertwhiz-ll2ip День назад
Ok I am a full stack developer currently grinding on leetcode. I just don't understand why you don't share code on github. If you have shared then provide the link. Thank you so much for you explanation.
@Virtualexist
@Virtualexist День назад
why does it happent that we include [2,2] ? Why did this loop not skip the second '2' when it was the same as previous element?
@binirxn
@binirxn День назад
Isn't this too much for beginners, cuz they say it easy level
@yomamasofat413
@yomamasofat413 День назад
This is nuts. How is anyone supposed to come up with solutions like this if you do not look at the solution first? Who can even think of this shit
@mberu14
@mberu14 День назад
the assign right to left is silly ,it is pointless is easy if we just element the duplicates and assign just a single digit needs to be explained better
@spageen
@spageen День назад
3:10 <- pee pee button
@pumpkinpie798
@pumpkinpie798 День назад
and what to do if its asking for a maximal rectangle of 0's .. I don't understand
@aakashavril
@aakashavril День назад
I like the solutions by neetcode but this one is particularly very confusing and does not come naturally. The following approach builds each permutation choosing one element at a time and further building up the solution. class Solution: def permute(self, nums: List[int]) -> List[List[int]]: res = [] self.dfsPermute(res,nums,[]) return res def dfsPermute(self,res,nums,curr): if len(curr)==len(nums): # base case when we found one permutation solution res.append(curr.copy()) # go over each element add it to the list for n in nums: # check to avoid dulicate value in the arr if n in curr: continue else: # call recursively for each case curr.append(n) self.dfsPermute(res,nums,curr) #undo the last chosen element and go to the other decision branch tree curr.pop()
@mso4324
@mso4324 День назад
I was struggling with understanding solutions in the forum, this really helped clarify how recurrence relation is working here
@shadowsw8020
@shadowsw8020 День назад
This is kinda crazy
@bhuvankiransagarg.b9678
@bhuvankiransagarg.b9678 День назад
void rotate(int *nums, int numsSize, int k) { int t=0; while(t<k){ for(int i=numsSize-1;i>=0;i--){ *(nums + (i+1))=*(nums+i); } numsSize++; *nums=*(nums + numsSize-1); numsSize--; t++; } } This code works fine on compilers but does not work on Leetcode can somebody explain plz 🥲😭🙏
@eltongbollie1881
@eltongbollie1881 День назад
return len(nums) != len(set(nums))