DEV Community 👩‍💻👨‍💻

Cover image for Minimum Steps to Visit Nodes in Infinite 2D Grid x Interview Bit
Vinit Gupta
Vinit Gupta

Posted on

Minimum Steps to Visit Nodes in Infinite 2D Grid x Interview Bit

I have been solving questions for some time on Leetcode.
But having a timer for each question is another challenge. So I recently decided to start solving questions on Interview Bit.

Also, sharing what I learn as a series of posts.

The first question I solved the following question :
Minimum Steps to Visit Nodes in Infinite 2D Grid

The problem states that there is list of points in a matrix or a 2D array. Here, the array is infinite.

Once we start from point P(A[0],B[0]), we have to visit all the points in the given order.

The first thing that might come in mind is that we have to check all possibilities. But the solution is not that complicated.

There are 2 ways to reach to a given point from another point :

  • 4-direction movement: We have to cover the x-axis difference and the y-axis difference differently. For that the distance between the two points will be : abs(Bx-Ax)+abs(By-Ay)
  • 8-direction movement: Now, that we can move diagonally, we can cover x and y axes in on diagonal move. So, the optimal distance between 2 points comes out to be : Max(abs(Bx-Ax),abs(By-Ay))

The complete solution of this question in Java can be found here :
Github

PS : This is going to be a series on the questions that I solve on Interview Bit so stay tuned!!

Top comments (0)

An Animated Guide to Node.js Event Loop

>> Check out this classic DEV post <<