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,B), 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 :
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 :
The complete solution of this question in Java can be found here :
PS : This is going to be a series on the questions that I solve on Interview Bit so stay tuned!!