Visual introduction Two Pointer Algorithm | Data Structure and Algorithm for Coding Interviews
2024 ж. 13 Мам.
42 224 Рет қаралды
We have explained two pointer technique which is the optimal way to solve problems related to arrays in O(N) time. We will start with a brute force solution from O(N^2) to an optimal O(N).
The best part of this video is showing how to identify where to apply it. Great Explanation overall
Thanks for explaining perfectly. You were talking about asking the interviewer or using another ds. I came up with another solution, especially if it’s not sorted it’s technically O(n) . You convert the array to an object/hash map , you map the number to index and then loop over the array, you minus the target from the number at array index where your loop is currently at, you try to get if you can then return the indexes. It’s O(n+n) which is 0(n). But two pointers is totally better
This code is clean and makes so much sense. Thank you for sharing!
Thank you very much. Very descriptive video!
thank you so much sir Such a great and simple way of explanation
nice visualisation and great technique with binary search
This was very well explained, thanks!
Great one
Thanks!, Logré resolver el problema de los contenedores de agua luego de ver su video.
Thank you! It helps!😊
Hi. What software do you use for these visualizations? It looks a lot like 3blue1brown's
It's manim, a python library
I'm fairly new to this concept, I understand that you return an array of size 2 with the current index locations of the 2 values that equal our sum. However, I don't understand why you add one to these indexes? I solved this problem in Java and adding one to the indexes returned the wrong value. Is this just how the language you are using functions?
That's how the leetcode question is worded.
Can you go topological sort next?
Thanks for the suggestion! You'll be happy to know that it is in the backlog of topics that we will cover in the future!
It will work only for unique elements
O(N) is not > than O(N Log n)
when returning the two indicies, why add 1 to both? 2 and 7 were at indexes 0 and 1 respectively so confused why you have to add the 1
I was really confused too. The problem has a note "Your returned answers (both index1 and index2) are not zero-based." so I think they're adding 1 to account for this note. It's a weird note, though.
Yes, it’s atypical in my experience to request an answer in index form but also want it non zero. Makes me raise my eyebrow and wonder why add that extra layer.
@@Meeko1123 yup look at the first example. [1,2] was the answer
the rum time animation in 4:37 is not accurate shouldn't move the front pointer until the current sum is smaller then target, as your code says. but nice video, thanks
Yes