FatherPhi
โ† Back to long-form videos

FatherPhi video

Two Sum LeetCode - Optimal JS w/ Time Space Solution - Line by Line Walkthrough | JavaScript

104 views ยท 6 likes ยท 1 comments

About this video

Vote for the next video here
๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡
https://FatherPhi.com

Eleven labs is the crazy ai voice generation tool I use to fool ChatGPT
Try it out for free!๐Ÿ‘‰ https://try.elevenlabs.io/6wbvspyzcq44
(Affiliate link)

For the weekly brief on accessible and practical Ai usage + updates on our latest mission join the newsletter
๐Ÿ’Œ https://master-of-ai.com


๐Ÿ“ฉ For business inquiries please reach out to phi@fatherphi.com



And since people are asking.
The camera I use to shoot these videos
๐Ÿ“น DJI Osmos Pocket 3 ๐Ÿ‘‰ https://amzn.to/3O3Qw8R

Transcript

what's up code ponies today we're gonna
be doing to some elite road easy
challenge and we're just gonna go
straight into it alright like I said
we're gonna hop straight into to some
[Music]
we're gonna go over to JavaScript and
okay so the problem is given an array of
integers return indices of the two
numbers such that they add up to a
specific target you may assume that each
input would have exactly one solution
and you may not use the same element
points okay so in my opinion the optimal
solution would be using some kind of map
because then you can in debt have an
index and a value in this situation kind
of reversed into normally in JavaScript
and JavaScript with an object you can't
have a number as the the property value
the property name so with with the map
object you can actually have a number as
the then the index value the one of the
two mapped values so you can map any two
values to each other so that anytime you
call the first value you get the second
value so this is actually something
either rather recently so it's kind of
like you have an integer of one and
another integer of one and anytime you
call this one here you can get this one
okay so let's start this off by creating
a new map so let's say let map equal new
map capitalize it from an object and
then from here we can do it do a for
loop for let I equals 0 I less than
Blum's
dot length because we need to iterate as
many times as there are
numbers in the numbers array and then we
have to keep adding I adding to I after
every iteration so we can keep the loop
going so now we can create another
variable for J and J all J is gonna do
is hold the cup actually is named a
compliment to make it easy to understand
so let compliment equal target
- nums i so all that's doing is taking
the current num nums value at the index
of i so let's say we have an array of 1
2 3 that's going to give us the in the
first iteration the 1 so if i is 1 I is
0 which is what it starts off at we're
gonna get 1 and then if if numbers is 1
then we'll get 2 and so on and so forth
so then the idea of the complement is
that we're receiving a target variable
from leak code so the target let's say
is 10 so 10 - in this situation one that
would be 9 so we're looking for a 9 so
this array would have no solution
because there's no 9 so we're gonna do
that and then we're gonna do it a little
bit statement if math dot has complement
so it's pretty simple logic there if mat
if our map has a complement that
complement within it it'll return a
boolean value so a boolean can be either
true or false and if it's true then
it'll enter this if statement and it'll
execute this returns out a return
statement and the return is going to
going to be an array value of map
get complement so that's gonna return a
number that will equal the index so of
the numbers array so we have to feed
that to the map object later on so
complement and then we're gonna also
return I for the current index that
we're iterating on right now and it that
doesn't work then we're just going to
add this number you and its index to the
map object so if we get to this point
it's going to be map dot set and then
we're going to set the complement so if
we get to this point we're gonna have to
set the value in the map so it's going
to be map done set nums oh my god hi
and then I so what this does is every
time we go through the if statement and
it fails it's going to instead just set
the current value no value and the index
to the map so I'm going to illustrate
this for you so if your fails we're
going to add to the map like so so let's
say for example the first one is I that
is one and zero index of zero then next
if down failed as well we're going to
and we're going to set two and an index
of 1 the next going to be 3 with index 2
and then
it's going to end and then it's gonna
fail so I'd like to get to two kind of
like an air handler here if not if it
doesn't work out I just return you
negative one so this is kind of like an
air handler so let's run the code okay
let me log in real quick okay can we run
the code
okay accepted and let's try submitting
all right so it's pretty fast
but it uses quite a bit of memory I'm
guessing because I'm using the inbuilt
map object and I like it that way
because it's just really readable and I
like the fact that we can use maps in
JavaScript okay so let's go over some
complexity issues let's say for time
complexity complexity we're going to
because okay so we're gonna iterate
through the for loop but as many times
as we have number use so we clear all
this out real quick so if we have four
numbers values we will run the for loop
four times if we have three quit three
same same thing we run it three times so
that kind of sounds like it's in number
of times to me cuz you know as made like
whatever the numbers that length is that
it's gonna be so
time complexity is going to be hope in
then for space complexity we'll have to
figure that out by going through the
program so first we need some space for
numbers so now I'm just going to be
let's say let's say each variables worth
four bytes before in target is worth say
for map there's going to be so map
consists of two key value pair so that's
to n times n no it's worth 2 times 4
times in so each value pair is worth
eight and then you multiply it by n
which is a 10 so then we have our I
that's also I is for what else do we
have
with complement that's 4 and with the
return value which is another 4 so let's
add this all up
it's 4 in plus we have 4 we have a four
individual variables that's another four
in then we add a 10 so that's going to
let's simplify that into 16 in so the 16
end is a linear function so that we can
say that our oh is of n
so clean here so it's a linear so it's
linear in times fate time and space
complexity so one last thing I want ask
you guys to do before hitting the submit
button please hit the like and subscribe
button it really helps my channel and it
makes me feel really good thank you so
much you think you have a better
solution to this challenge in JavaScript
if you do just don't forget to comment
down below and if you like videos like
this feel free to click on any of my
other ones right over here and if you
want to see more videos like this just
give me a like maybe subscribe I really
appreciate it and till next time Pro 20
out peace