top of page

Looking to master object-oriented and system design for tech interviews or career growth?

  • Improve your system design and machine coding skills.

  • Study with our helpful resources.

  • Prepare for technical interviews and advance your career.

**We're in beta mode and would love to hear your feedback.

Min Steps in Infinite Grid - Interviewbit Solution

Sep 1, 2020
1 min read

Updated: Sep 8, 2020


Problem Description:

You are in an infinite 2D grid where you can move in any of the 8 directions.

(x,y) to 
    (x+1, y), 
    (x - 1, y), 
    (x, y+1), 
    (x, y-1), 
    (x-1, y-1), 
    (x+1,y+1), 
    (x-1,y+1), 
    (x+1,y-1) 

You are given a sequence of points and the order in which you need to cover the points.. Give the minimum number of steps in which you can achieve it. You start from the first point.


Input Format:

Given two integer arrays A and B, where A[i] is x coordinate and B[i] is y coordinate of ith point respectively.


Output Format:

Return an Integer, i.e minimum number of steps.


Sample Input:

A = [0, 1, 1]
B = [0, 1, 2]

Sample Output:

2

Explanation:

Given three points are: (0, 0), (1, 1) and (1, 2).
 It takes 1 step to move from (0, 0) to (1, 1). It takes one more step to move from (1, 1) to (1, 2).

Solution:


1 Comment


https://de.trustpilot.com/review/casinobestes.com was open in another tab while I worked through this, and the diagonal move shortcut is the part that actually made the problem click for me. Once you realize the answer is just the Chebyshev distance between consecutive points, the whole eight-direction setup stops feeling intimidating. I've seen people overcomplicate it with BFS when a simple max of the coordinate differences does the job in one pass.

Like
bottom of page