NoteGPT - Competitive Programming in Javascript - Solving Your First Problem.
NoteGPT - Competitive Programming in Javascript - Solving Your First Problem.
solve this problem and in feed as many such questions to that particular
once we have written that down so that's the idea behind writing an algorithm to
automate it a study you go to and to do the task ready to be faster and I do it for
a lot of cellular problems so the problem that we are solving today is that we have
to move from one city to the other and then from the second to the third city and
finally end up at the LA City you have a lot of such steps that you have to go
through and you have
to tell the shortest or the minimum number of steps that you need to do
in order to reach the final destination so to visualize this problem simply let's
say you have a partition plane over here and you have to move from this initial
point to the final point and initial point is tenure and this final point is
mangling you have to go from here to here in the shortest number of steps and what
is the step one step is that you move either upward or downward left or right or at
that medium a tiny town a tiny left a tiny net so you can
move in one of eight directions and that would count as one step and you
have to go from here to here in the minimum number of steps okay so if you see over
here this is synchronicity to the ideal way of doing this you know if this is N and
this is M the shortest distance between these two is root of H square bassam's but
that's how you solve a problem using the triangle inequality but in this case you
cannot move from here to here technically the directly in the dying manner because
your diagonals
the minimum number of steps that you need to reach from here to here is M
plus that make sense because M is greater than n that we can see or let's say even
if n was later than M then the answer would have been n so basically the maximum of
these two is the minimum number of steps that you need to take why because and each
time you can decrease the distance from here to here from here the horizontal
distance and the vertical distance at max by one you cannot decrease it by more
that much open this what this tacit direction over
here I can only decrease the distance the vertical distance by one and
horizontal distance by one in one step I cannot decrease it by more than one the
horizontal or the vertical distance hence the minimum number of steps two each from
here to here is M now can we prove somehow that that is also the maximum number of
steps that you need and then can we prove that M is the total number of or the
maximum of distance and this distance is the computer model steps that you need to
reach from here to here the answer is
yes that would be the maximum number of steps that you need why because
let's say you start from here now you know that this particular point the
finalization lies to my top right so it's in the high tension in the and is above
me so I'm if I start moving in this diagonal direction till the time I reach the
body five degree we move I forgot unit in the time we reach the point where this n
distance becomes zero so you have conquered the horizontal distance that you have
to enter is only this vertical distance that's left that
you need to conquer so let's say you keep moving in this direction and
the distance that you covered BC cleans and in this section and n in this section
and the distance that is left is M minus n right and this M minus n you can cover
by going in the other direction so the next time or the next number of steps that
you need is n plus M minus n which is basically and so if basically if you have to
solve this problem and you know that this is this particular point is at x1 y1 and
spread it was point is and x2
y2 find out what you listen to it in the book which is but the absolute
value of y1 minus y2 find the horizontal distance between these two this is nothing
but the absolute value of x1 minus x2 and then in fact the maximum of these two
whatever this is maximal these two is the time what is the number of steps that you
need to reach from here to here I hope that clears of the problem and this can be
extended to my total cities as well you have given your city one city to an eternal
city three then you
know you have to the number of steps that you need to reach from here to
here is the absolute the maximum of the horizontal the vertical is in between a
spoon and this nozzle can be applied with these two points as well and you can keep
doing it for multiple points and reach your final answer so I hope you got a little
taste of how you can solve the problem now you will be actually coding this problem
in JavaScript all the platform quite interview wind so you don't have to set up and
I think on the computer and once
start with the first point move to the second of the second move to the
third and you have to do that in the minimum number of steps considering it move in
da directions so just come down here it's like the languages JavaScript because we
are doing all the computer programming in JavaScript in this course and if you
don't understand what all of this stuff is it's basically module dot exports as you
can see is an object it has one attribute called couple points and this attribute
is a function so if
you don't understand this structure right now just ignore it what we have
to do is that inside this function we have two parameters a and B which are the add
of integers which represent the x coordinates and the Y coordinates of the points
of the earth index value and this in these areas is an integer and that negative
resents the x-coordinate and the y-coordinate of the island city other point which
we have to go to and we need to start from the first point is to see the zeros
point and move to the n minus one point and
find the minimum distr minimum number of steps that we need to take to
cover those and whatever that you became we need to return from this function I am
little bit I will basically check what this function for a lot of cases and check
if your answers are expected so you have a a and you have vanity if you remember
from what we discussed for each and every point we need to go diagonally to become
compressed one of the directions and then move vertically or horizontally to
finally cover the rest of it and what we
got down to in the egg was that if there's a point starting with x1 comma
y1 and the second point is x2 comma Y doing what we need to do is we need to find
the maximum of the absolute value x1 minus x2 c'mon the absolute value of y1 minus
y2 and this will give us the answer right now we don't have just two cities like we
did in the example that we discussed we have multiple cities so we will need to
hydrate using the forum so far away I equal to 0 I less than n minus 1 i plus plus
why did we write n minus 1 over
here because we need to compare the first point with the second one the
second with the third one and the second asks with the last one we don't need to
compare the last one with anything else so we will just hydrate order from the 0 to
the N minus 2 index we leave the last one because you don't have to with anything
now what is next one for this loop x1 is nothing but a of I X 2 is nothing but yeah
I responded Y 1 is nothing but V of I and Y 2 is your I plus 1 so in this
particular item of mine I'm checking the distance between
the iron and I plus 1 it's city so these are the X's and Y's in that case
we need to return the net number of steps that it took for all of the furniture for
hydrating through all of the cities so we will define an answer variable whose
initial value we have taken as you so initially if it has the value 0 over here we
modify its value to something and finally we will return that value in this
function and what will we add to this answer over here so in each ication what is
the distance that we have to
this thing so answer was in DC 0 after the first equation we had this
value to the answer after the second iteration we do that and we do that so that
answer that it will finally contains the net answer the answer which contains the
summation of all the cities or the steps taken in all the cities to each from the
first to the last point if we try to touch this and this will basically let us know
if there are any errors there are it says n is not defined because and it's not a
variable defined in English so I tell you can
defined and as a dot length you can simply write a dot length over here
and it says that this is the it gives the correct answer for one or two cases so
when you test it just test its four disks it for its composition and for few basic
cases when you click on submit then it actually checks whether you're whether or
not your solution passed and if you see in this case the solution passed so as you
can see this is just five lines of code that you have to write in the end and
JavaScript made it so simple I think actually this would be
very simple in any language but functions like Max and function like
absolute which sharply defined in Zurich helped us basically in achieving our fan
your and I hope you understand how this particular algorithm is working if you did
not understand I go over it one more time what we have to do is we have to check
that this steps taken from the zeros to the first city then the let's say there is
three cities they never good from zero to the first and the first to the same yet
there are just three cities the value of n would be
three and a of zero and we have zero putting the X and the y index of the
zero city which is the first city so x1 would be the x coordinate of the slow city
why I am going to be the y coordinate of the zero city x2 would be the x coordinate
of the first city and why it would be the y coordinate of the first city and as we
saw this is the answer for a particular X Y and well if there is one x one y one
away and x2 y2 over there then the number of steps check needed is this particular
thing which
individual form which is doing this again and again by looping through a
four loop what are the four units looping through and because there are a lot of
CDs have you have to go to and so that we don't have to write this code again again
we use a forward to do this this is a little difficulty question than what we
discussed what we discussed was for just blue cities but if they have multiple
cities then you have to like to follow you and go through all of these and if you
submit this your books
are strongly suggest you to try this question on your own if you have any
doubts just post them in the comments if this is the first thing you're trying to
submit your question I know this can be a little difficult to understand so if you
have any doubts lightning on and try to solve this problem on into a bit because we
will be moving to little more difficult questions in the future if you don't
understand this one you might get confused in those in the ones that come in the
future so thank you for joining