Not immediately sure how this is different from OG two sum
Okay yup. Weirdness. Looks like Leetcode is just not really enforcing space-complexity correctly here, which is the kicker as far the differences go. I shouldn’t be allowed to use a hashmap ‘legally’ here, as hashmaps are obviously O(n) space complexity. That is, 100 elements use 100 elements of storage, and 10 use 10.
There is a way to use pointers so that 100 elements use 2 units of storage, as do 10. Let me try to figure that out.
Messed up, and didn’t think to add this line at first:
rightPoint = leftPoint + 1
There are some more problems. Here is the right solution:
My solution was weird…it sort of made sense. I was only moving the right point forward if there was “room”, but the clever thing is that I didn’t really need to make this check, as the leftPoint < nums.length-1
and the right point start as 1 more than this, so there’s at least room for it on the first loop.
Instead, I can check to see if we’ve reached that last position, or even out of index - it would be okay I guess if we went out of index since we aren’t going to pass it into the function where the array will be accessed.
So if we do find our right point is leaving the index on the right side, we can conclude
- We have checked every other potential match for our current left point, and can move the left point along
- If we move the left point along, well, we need to “reset” the right point, 1 in front of our new left point
The only real thing left to do is to return something if our while loop never finishes. Although. It should.