Learn to THINK. Learn to CODE — Day 03: Two Pointers
So far we solved problems where we scanned a list of items, and used simple tools to remember what we had seen. But think about it. If a list has 100 items, do you want to go through it one by one? Until now we used one pointer to move through the list. To read n items, it takes n steps. What if you use two pointers instead? You can often read the list faster and make better decisions along the way. That is the technique we are learning today. Two pointers. Here is a simple way to feel the idea. Imagine you have one task, and you split it between 2 engineers. One engineer is doing very well. The other is not. Which side do you push a little more? You push the side that is limiting the progress. Correct. Two pointers works the same way. You start with two pointers, left and right. Whichever side is limiting the result, you move that side. Keep our rule in mind the whole time: Understand the problem clearly. Break it into small parts. Work thr...