Search Suggestions System - Leetcode 1268 - Python

  Рет қаралды 45,461

NeetCode

NeetCode

Күн бұрын

Пікірлер: 90
@NeetCode
@NeetCode 2 жыл бұрын
Discord: discord.gg/ddjKRXPqtk Correction: Time complexity is O(nlogn + n + m) where n is size of products, and m is length of searchWord.
@masternobody1896
@masternobody1896 2 жыл бұрын
Bug brain time
@anon325
@anon325 2 жыл бұрын
It should be S*nlogn + n + m where s is avg string length.
@aishwaryaranghar3385
@aishwaryaranghar3385 2 жыл бұрын
can you make a video one day for all the complexities.
@tomyao7884
@tomyao7884 2 жыл бұрын
Very nice correction
@mirceanicolaescu9804
@mirceanicolaescu9804 2 жыл бұрын
it is actually a little worse than that because we have to account for copying over the strings into the result map at the end. so it would be something like S*nlogn + n + m * S
@uditsharma5688
@uditsharma5688 2 жыл бұрын
Nice man , i used a binary search but two Pointer is more intuitive and easier to remember for me now. 👍
@rockstars369
@rockstars369 2 жыл бұрын
For those who are wondering the purpose of 2nd pointer(right ptr.) as it isn't obvious in the video due to the example. In the example, it appears like we could get the solution for each typed character with constant amount of work i.e check the next 3 words from the current index of first pointer(left/top) and if they match the typed character add the word to the list and then append to the result List. The efficient way to verify the next 3 words from current index of first pointer(left ptr.) is just check the current char directly instead of reverifying all the previous chars of the next 3 words are also matching the prefix typed so far of the searchWord. Too much blabbering 🙃..... In short 2nd pointer is to limit the search space and guarantee that as long as the index of 1st pointer(left ptr) is less than or equal to 2nd pointer (right ptr) we are still in valid search space and avoid reverifying all the previous chars of the next 3 words from 1st pointer. Example : sorted search list = [mobile, monitor, mousepad, muumuu, tsunami, vault...] searchWord = mouse 1) char 'm' = [mobile, monitor, mouse] 2) char 'o' = [mobile, monitor, mouse] 3) char 'u' = [mouse] -> *current search char is 'u' with prefix searched so far "mo", without a second pointer we would have to reverify "mo" is also a prefix for words mousepad, muumuu, tsunami.*
@lonen3rd
@lonen3rd 8 ай бұрын
You're right. The first idea that came to mind was Trie, but the implementation was not easy.
@dianasvideos123
@dianasvideos123 2 жыл бұрын
Woah! This was my Amazon question last year! Thank you for this video, Neetcode! I’ve been meaning to re-attempt this! 😃
@ObtecularPk
@ObtecularPk 2 жыл бұрын
Did you get it right?
@dianasvideos123
@dianasvideos123 2 жыл бұрын
@@ObtecularPk No, I ran out of time 😅
@ramkrushnamadole874
@ramkrushnamadole874 2 жыл бұрын
Did u place in Amazon
@ObtecularPk
@ObtecularPk 2 жыл бұрын
@@ramkrushnamadole874 what do you think? If she missed a question and ran outta time.
@dianasvideos123
@dianasvideos123 2 жыл бұрын
@@ramkrushnamadole874 No, but I did get to try again 6 months later, and then I failed again 😅 But the moral of the story is- it wasn’t the end of the world 🙂 After that first failure, I was still able to interview with Meta (twice), Amazon again, and then Google as well last May 🙂 Each time I interviewed, I got A LOT better, but I’m still not quite there yet 🤷🏻‍♀️ I think if you want to interview with FAANG/MANGA, just do it 🙂 If anything, it’ll give you an idea of where you currently stand and what you need to work on 😄🤷🏻‍♀️
@Mutual_Information
@Mutual_Information 2 жыл бұрын
Out of all the channels that try to help people transition into tech, this one has to be the most useful. Seriously these problem coding skills, as petty as they appear, as huge for getting an offer.
@coderabbit118
@coderabbit118 2 жыл бұрын
Thank you very much for these helpful videos. I did Blind75 and watched your solutions for those 75 questions! I must say, the solutions help me a lot for my tech round interviews!
@zbynekjurica
@zbynekjurica 2 жыл бұрын
Wouldn't it be better to use binary search to move the pointers instead of just moving it by 1? Anyway, great explanation!
@zr60
@zr60 2 жыл бұрын
binary search is more complex, because of the string slicing required in comparison.
@joelpww
@joelpww 2 жыл бұрын
Idk if it was corrected but lexicographically and alphabetically are different. The former is alphabetically order preceded by a length comparison
@joelpww
@joelpww 2 жыл бұрын
So this is asking for alphabetical and shortest results
@nickheyer
@nickheyer 2 жыл бұрын
Hey @NeetCode, wouldn't it be faster to use a slice on the final append to answer instead of that for j in range loop? ie: answer.append(products[l:min(3, r - l + 1)+l])
@ooow333
@ooow333 Жыл бұрын
result.append(products[l:r+1][:3]) also works
@hugoibanez
@hugoibanez 2 жыл бұрын
I really have no clue how are we supposed to come up with this sort of answers during an interview if we never saw the problem 😅
@johnpaul4301
@johnpaul4301 2 жыл бұрын
yep same bro
@shokhrukhabduahadov3985
@shokhrukhabduahadov3985 5 ай бұрын
But question does not mention about products being sorted (or coming in order)
@adityadhikle9473
@adityadhikle9473 2 жыл бұрын
I think on your website you should list this problem under two pointers rather than binary search.
@alanprado1652
@alanprado1652 2 жыл бұрын
this is just to complex to get in my head :/. i always pause the video to try to understand it but i don't get when he adds a bunch of code in the same line, i feel frustrated😢
@DavidDLee
@DavidDLee Жыл бұрын
I'd use a TreeMap / std::map, which will do the l/r portion in logN time
@tanishq2766
@tanishq2766 Жыл бұрын
Yeah i solved using the same technique
@akhma102
@akhma102 Ай бұрын
Thank you, Neet!
@oscarheerkensthijssen5454
@oscarheerkensthijssen5454 11 ай бұрын
How about res.append(products[l:min(l+3, r+1)]) instead of lines 15-18
@raihanulalamhridoy4714
@raihanulalamhridoy4714 2 жыл бұрын
Thank you. The explanation was really good. Better than leetcode solution page.
@vineethsai1575
@vineethsai1575 2 жыл бұрын
The future generations are very lucky because by that time you might have completed videos for most of the leetcode problems!
@sudhanshushekhar4222
@sudhanshushekhar4222 4 ай бұрын
really amazing explanation....
@shrimpo6416
@shrimpo6416 2 жыл бұрын
2 pointer solution is so neeeeeeeeeeeeet!!!
@wintersol9921
@wintersol9921 2 жыл бұрын
Your explenations are great.
@NeetCode
@NeetCode 2 жыл бұрын
Thanks! 🙂
@memeproductions4182
@memeproductions4182 2 жыл бұрын
Right pointer doesn't change complexity right?i could just iterate all words with left pointer and sfill find the first match, then just pick the first 3 matches from there
@dynamic.6302
@dynamic.6302 2 жыл бұрын
Yes, as you iterate with your left pointer and check if it matches, you also need to check with left + 1 and left + 2 if it matches with the left pointer. And you need to ensure left + 1 and left + 2 are less than len(searchWord)
@flamendless
@flamendless 2 жыл бұрын
Question, would this algo scale for a database table (sorted always) with millions of data? hmmmmmmm
@tempestofsouls5693
@tempestofsouls5693 2 жыл бұрын
Solved it using Trie + DFS. Should've realized that there was a much simpler solution using binary search or two pointer :/
@shayanshayan8741
@shayanshayan8741 2 жыл бұрын
Can I send a graph question and also a simple question that I didn't get to solve. Will you have look into that
@PunithNayak-rj3fg
@PunithNayak-rj3fg Жыл бұрын
Hey please mention the the time and space complexity for every question you solve
@altusszawlowski4209
@altusszawlowski4209 11 ай бұрын
He did at 7:47
@EMdragonKnight
@EMdragonKnight 6 ай бұрын
Can you do this problem as a Trie?
@austinhuang2454
@austinhuang2454 2 жыл бұрын
For the testcase of ["havana"] "havana", r will be 5, which occurs an error that list index out of range for products[r] and products[r][i]. How do you fix this?
@austinhuang2454
@austinhuang2454 2 жыл бұрын
my bad, the right pointer should be len(products) - 1.
@austinhuang2454
@austinhuang2454 2 жыл бұрын
which would fix the issue of my question
@davidardo4466
@davidardo4466 2 жыл бұрын
hello teacher, i have two question? math is important to software engineer? Second question? can i do better math? third/Sir, I'd like to work with Google in the future. Can I?
@johnpaul4301
@johnpaul4301 2 жыл бұрын
go out and touch grass
@neerajasanjay2375
@neerajasanjay2375 7 ай бұрын
It gives a TLE now right?
@bree9895
@bree9895 7 ай бұрын
here after a year! and after 2 jobs:)
@trollbaron1329
@trollbaron1329 2 жыл бұрын
Hi Neetcode, love your succinct and clear explanation! Would you be willing to make videos on more advanced concepts like Segment Trees and String Hashing/KMP algorithm?
@mdazharuddin4684
@mdazharuddin4684 2 жыл бұрын
I believe Trie will be more practical if the word list is mutable.
@cyanimpostor6971
@cyanimpostor6971 2 жыл бұрын
It's more of a concept and application I believe
@memeproductions4182
@memeproductions4182 2 жыл бұрын
How would you implement it with trie?i get you add all words character by character. But what when you search them up?you search until the prefix but then?you'll have to explore all the children subtrees and get the first 3 lexically minor
@mdazharuddin4684
@mdazharuddin4684 2 жыл бұрын
@@memeproductions4182 maybe a dfs preorder traversal
@dynamic.6302
@dynamic.6302 2 жыл бұрын
@@memeproductions4182 In your TrieNode class, have two attributes - children and suggestions. As you insert your product to the TrieNode, also add the product in the suggestions field. Suggestions can be an array and you can sort this array whenever you insert to it. Also, ensure that you pop out the elements whenever suggestions.size is greater than 3. Sorry if you didn't understand this. Will be happy to elaborate on this
@mdazharuddin4684
@mdazharuddin4684 2 жыл бұрын
@@dynamic.6302 won't that take more time to keep updating the suggestion at every insert? I tried the dfs approach, took 2887ms while the two-pointer took only 115ms
@aishwaryaranghar3385
@aishwaryaranghar3385 2 жыл бұрын
Thank You!
@mk-19memelauncher65
@mk-19memelauncher65 Жыл бұрын
This doesnt seem scalable because you would need to sort every word first.
@vivekshaw2095
@vivekshaw2095 2 жыл бұрын
yesterday I took a test from aquasec this was my question with a bit of twist, insteaf of prefix I had suffix and instead of just one search word I had an array of multiple words
@shantanukumar4081
@shantanukumar4081 2 жыл бұрын
Masterpiece 👌👌👌
@ohhellnooooo8233
@ohhellnooooo8233 2 жыл бұрын
Hello, what's the app you use for doodling?
@NeetCode
@NeetCode 2 жыл бұрын
Paint3d
@ReAgent003
@ReAgent003 2 жыл бұрын
@@NeetCode :D
@janailtongoncalvesdesouza4160
@janailtongoncalvesdesouza4160 6 ай бұрын
Very clever!
@mageshyt2550
@mageshyt2550 2 жыл бұрын
const suggestedProducts = (products, searchWord) => { let ans = []; products.sort(); for (let i = 0; i < searchWord.length; i++) { const curr_search = searchWord.slice(0, i + 1); let temp = []; for (let j = 0; j < products.length; j++) { if (products[j].startsWith(curr_search)) { if (temp.length < 3) { temp.push(products[j]); } } } ans.push(temp); } return ans; };
@__Y1a2s3h4__
@__Y1a2s3h4__ 2 жыл бұрын
Leetcode 82 pls🙏
@krateskim4169
@krateskim4169 2 жыл бұрын
beautiful
@pranavsharma7479
@pranavsharma7479 2 жыл бұрын
easiest solution
@dera_ng
@dera_ng 2 жыл бұрын
❤️
@colin398
@colin398 2 жыл бұрын
5 seconds in: trie trie trie trie
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 148 М.
Non-Decreasing Array - Leetcode 665 - Python
10:59
NeetCode
Рет қаралды 57 М.
The Singing Challenge #joker #Harriet Quinn
00:35
佐助与鸣人
Рет қаралды 45 МЛН
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 169 МЛН
Search Suggestions System | Leetcode 1268 | Trie | Binary Seach
24:16
I quit Amazon after two months
10:09
NeetCode
Рет қаралды 645 М.
Learn Python OOP in under 20 Minutes
18:32
Indently
Рет қаралды 107 М.
Top 5 Most Common Graph Algorithms for Coding Interviews
13:01
Programming Languages Tier List 2024
16:18
Neal Wang
Рет қаралды 11 М.
LeetCode 1268. Search Suggestions System - Interview Prep Ep 103
20:34
Remove Sub-Folders from the Filesystem - Leetcode 1233 - Python
19:29
Decode String - Leetcode 394 - Python
16:26
NeetCode
Рет қаралды 92 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 674 М.
Minimum Cost to Hire K Workers - Leetcode 857 - Python
19:01
NeetCodeIO
Рет қаралды 14 М.
The Singing Challenge #joker #Harriet Quinn
00:35
佐助与鸣人
Рет қаралды 45 МЛН