Two Sum (LeetCode #1) | 3 Solutions with animations | Study Algorithms
HTML-код
- Опубликовано: 31 июл 2024
- To see more videos like this, you can buy me a coffee: www.buymeacoffee.com/studyalg...
Two Sum is a programming question on LeetCode and very important for interviews. You are given an array of integers and a target value as the input. There are exactly two integers in the array which add up to the target value. You are required to return the indices of these 2 integers. Watch this video to learn 3 ways of solving this problem. You will learn a brute force solution, a solution using sorting, and a solution using Hash-map. Each of the solution has been explained with step by step animations and with a dry-run of the code so that it sticks in your mind forever.
00:00 - Intro
01:07 - Problem statement and description
03:54 - Brute Force Method
06:03 - Method 1: Using Sorting
10:36 - Method 2: Using Hash-Map
14:04 - Dry-run of code
16:35 - Final thoughts
📚 Links I talk about in the video:
Brute Force Method: • Brute Force algorithms...
Quick Sort: • Quick Sort super easy ...
Time Complexity: • What is the Time Compl...
Actual problem on LeetCode: leetcode.com/problems/two-sum/
Code on Github: github.com/nikoo28/java-solut...
Test cases on GitHub: github.com/nikoo28/java-solut...
📘 A text based explanation is available at: studyalgorithms.com/array/lee...
🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Follow on Twitter at: / studyalgorithms
Follow on Tumblr at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview
At 12:58, I am supposed to add "8" to the hash table and not "11".
Sorry for the error.
This is absolutely the best explanation on RUclips. Everyone else goes straight to coding and doesn't break down the logic beforehand.
Happy that I could help :)
thank you soooo much! I checked out so many videos about this question. finally your video helped me out! firmly subscribed to your channel
Underrated channel.
Thank you for taking the time to create this video and make a clear explanation
Only just got into coding a few days ago coming from a teacher background. Great explanations!
Love the way you teach, TYSM☺
You explained it very well, best video out there regarding this solution!!"!!!!
Thank you for the video it really helped me!
such clear explantion , subscribed
Thank you very much, finally understood
awesome explanation..keep teaching us
Thanks a lot, I was able to understand this problem thanks to your efforts.
awesome video... keep making them
I was searching for the solution but do not found dryryn but u did it thank bro ❤️
One of the best way of teaching . Sir Keep posting more videos of dsa. 👍👍
Thank you so much for your support :)
Great video best explanation
you are the best.I really appreciate your hard work and step-by-step approach to how to overcome with a solution. Hats off
thanks for the appreciation and motivation :)
Such a great way of explaining this problem. You have redefined teaching sir!!! Amazing Work!!!!
Thanks a ton
Thank you ❤
awesome tutorial sir
I watched two other videos before coming here , to be honest you are the best one . I only understand from you because other people talk too long without explaining they talk about the problem not the solution. You even taught me the math side of this . Thank you , I subscribed. I will watch the rest of the solutions
I prefer to focus on the problem. Language is a tool anyway, and they will keep changing and evolving. Thanks for the sub!!
Thank you, you are the best! Visualizing the code helped me a lot
You're welcome!
Very smooth sir
You're by far the most detailed explainer I've seen. I've seen a few, but you're really amazing. Having a coding interview soon.
All the very best for your upcoming interviews :)
Sir😄, I have no words, I have never seen anyone explain this nicley, thank you so much❤
Thank you so much for your kind words.
Excellent content. This is the second video I watch which allows one to be able to devise a nicer solution simply by sorting the array. The hash map solution is genius and is not complicated
I will now, always look at a problem and find out how I can solve it by first thinking "Would sorting this array help to simplify the solution"
Awesome, thank you!
@@nikoo28 you are welcomed
Good one
Amazing
Please keep uploading DSA/leetcode videos, they all are really helpful, thanks!
More to come!
Kudos! Great explanation brother.P.s: Bro, you sound exactly like Pennywise from It.
Best way to teach sir plz discuss more question and make a proper playlist ❤❤❤
Check out my playlists in the channel :)
Thank you bro you do grate things
your explanation is very simple and easy to understand
Iam so happy to see this video
so happy to read this
06:03 - Method 1: Using Sorting
This is basically sliding window solution, when you have two pointers , left and right and you move them correspondingly.
I do not know why he gets very few likes and he is the best teacher for explaining the leetcode problems
so happy you believe in me. Please share the videos if possible. Trying my best with SEO to get ranked up in search results.
subbed!
I had a question about the tastable, 19-4 is 15, but it is on the table so why don't we stop there? Thank you
your explanation is for more better than other youtuber
Glad you feel that way :D
For the second solution where we are using hashmap, the time complexity of finding an element is also O(n) in case of hash collisions
For a very big input size, yes there will be collisions…but for the domain of this problem you should be just fine.
Good to discuss this with your interviewer though 😄
its cool
thank you for a great explanation, what would be its space complexity?
Which solution do you want the space complexity for? :)
Is this possible using binary search?
Best
Hello I have a small doubt ? How we return two index values here map.get(complement),i);
Can any one explain 🤔 does it return complement index and current I value ?
what is this """'' return new int[]{map.get(complement),i}; """" ? as it throws error and i am getting you've used curly brackets with new int[]...................please reply..........................
It gets the complement pair and then returns it as an array.
Hi Nikhil, what program do you use to draw the solution?
GoodNotes 6
bro plz do in python
Awesome channel! Why I Find it so late?
Glad I could help you 😄
Can anybody tell how to write the main function code for the same function.
Just create an object of the class and call it in the main method
@@nikoo28 thanks.
AT 10:31 the method is not working for negative numbers in the array;
the optimized method takes care of all different test cases
@@nikoo28 Ohk Thanks Sir
Sir will 3rd Method will work if array has Duplicate elements . Becoz in HashMap , the key cannot be duplicate. And if array elements repeats , they duplicate key is found. PLZZZZ PLZZ EXPLIAN THIS SIR
since the problem says you will have EXACTLY ONE SOLUTION, you do not need to worry about duplicate number. Because if a duplicate number exists, then you will have 2 possible solutions...and hence that is not a valid scenario.
@@nikoo28 but in the example there is repetitive element
@@nikoo28 we have a valid scenario [3,3], and this method won't work :(
sir
what language you are used here
Java
100
koko eating banana 875 leetcode question
It is available now: ruclips.net/video/JGYXNpZaW2U/видео.htmlsi=2ftslOTD1LCE8-r2