Two Sum II - Two Pointers Algorithm - Leetcode 167

preview_player
Показать описание
FAANG Coding Interviews / Data Structures and Algorithms / Leetcode
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

This is a great teaching question. I love it when an answer has a (more or less) optimal solution that uses two pointers.

RS-fzps
Автор

Binary search or Two Pointer : TC => O(nlogn) SC => O(1) (When Array not sorted)
TC => O(n) SC => O(1) (sorted array)
Hash Map: TC => O(n), SC => O(n)

md.shariarkabir
Автор

I saw you using n as in r = n - 1 before declaring that n = len(numbers)
Good job

not_vinkami
Автор

finally someone who can explain two pointer simply

kareems
Автор

So the reason we used two point here is becoz this is sorted right? Not in two sum 1 as it wasn't sorted?!

VanisreeSunkara
Автор

Would be great if you displayed the time complexity in the end

hakan
Автор

Great video, please continue with this short videos solving algorithms are very useful.

pablourbanohernandezvizcarra
Автор

Is any chance using two pointer it may miss any element to check ? or it's time complexity may be high ?

monish.v
Автор

How does your code work if you declarated r before n if the declaration of r is dependent of n?

Progamertoat
Автор

Quick question, does it work if you just have a dictionary(hashmap)? Like index through each of the element A[i] then store (target - A[i]) as the key and its index as the value. Then return the index if found in the hashmap. This is O(n) too I believe.
P/s: if it need constant space then 2 pointer is the way to go.

themrunknown
Автор

if only two numbers, then extremely simple. In general, suppose we want to look for numbers, no matter how many, that have the summation equal to a given number k, then a fundamental way is to apply dynamic programming.

CTT
Автор

i would just do like put p1 on 0 then p2 from 1-len of list then if sum not found, p1 goes to 1 and p2 starts from 2-len of list, this continues until sum is found

then if p1 == p2: return p1, p2

works on both two sum and two sum II

elochess
Автор

It looks like a binary search what is difference between the br and tp

P.Shivakrishna
Автор

Anyone know greg using which tool explain problem like white borad

learnwithbharath-
Автор

Suggestion: Pls pause for an extra couple of seconds after revealing your soln.

giantbush
Автор

Do these companies still ask such easy questions?

shrayesraman
Автор

It doesn't work for negative numbers or target.

SSIRP
Автор

Three sum is more interesting.
You have a loop of index i and inside you solve with this TP algorithm to find j and k

BederikStorm
Автор

could you still use a hash map on this one?

tellmewhyaintnothinbutaheartac
welcome to shbcf.ru