LeetCode Two Sum II Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Комментарии
Автор

There's an error in the code. It says you may not use the same element twice. Which means the code should be:
while (a_pointer < b_pointer) and not <=. The reason this passes with <= is because all of the inputs have an answer where there are two indexes.

skp
Автор

it's an amazing solution. I spent a morning optimize the code and then I watched this one. it blows my mind

lethihue
Автор

why would it be a_pointer <= b_pointer if you can't use the same number twice?

hickton
Автор

i like how you start your videos with the little clap haha

trevor
Автор

Keep up the good work Nick! Really love your simple explanations....

samyakjain
Автор

awesome explanation bro, you made my learning, Small suggestions
1. Explain about alternative solutions
2. Explain time and space complexity

holatechm
Автор

I almost had it first attempt, I came up with an algorithm that compares each pair possible only once and skips the entire algorithm if the first two elements are found to sum up to the target. While its definitely better than 0^n2, it still ain't fast enough to pass the last test that that filled with 0s for like the first 1000 elements or something.

johnnywilson
Автор

But I have a little question about the last return statement.
Isn't it should be "return new int[] {0, 0}".
because the value of a_pointer and b_pointer are changed.

qianwenye
Автор

awesome explaination, but ur content is incomplete without the time and space complexity so please consider including them.

praveenchouhan
Автор

Simple Approach Using HashMap:

class Solution {
public int[] twoSum(int[] numbers, int target) {
HashMap<Integer, Integer> map=new HashMap<>();

int[] result=new int[2];

for(int i=0;i<numbers.length;i++){


result[1]=i+1;
}

map.put(numbers[i], i+1);
}
return result;
}
}

dadingchen
Автор

This solution is really neat and fast also easy to understand, thanks Nick!

jerrywong
Автор

I did it with hashing which works even without sorted array and still O(n), but I think the 2-pointers solution is faster, and use less memory.

xbeta
Автор

alternative algo

maintain map<num[i], i>
if sum(a, b) = target then target - a should give b

for (i till num)
complement = target - num[I]
if map.contains(complement) return map.get(complement) // soltion is index i and return value
else
map.put(num[i], i)

reallylordofnothing
Автор

Man, that was awesome!!! Simple and to the point. It saved much of my time.

lakshyaagarwal
Автор

What is the last return statement for at line 20?

a.yashwanth
Автор

Thanks for a good jobs. Looks easy but tricky. I didn't understand one thing. Why you return filled array at the end and not just new int[] {} ?

giorgi
Автор

Why there won't be a situation when by moving either left or right pointer we actually skip and jump over possible solution? E.g. why rule "if sum is less then move left pointer, if sum is more move right pointer" converges and eventually gives us the solution?

ievgengavrysh
Автор

cool stuffs, please keep it up, really helpful for my interview preparation. Thank you so much

TheSatishPatel
Автор

this is better than hasmap only because of Space complexity right, cz time is still the same?

rahul_spawar
Автор

very good explanation ! any comments on the last return statement? also, what is the time complexity analysis compared to two sum -1, with unsorted input..

raam