Skip to content

Min Steps in Infinite Grid

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-1), 
    (x-1, y)  , 
    (x-1, y+1), 
    (x  , y-1),
    (x  , y+1), 
    (x+1, y-1), 
    (x+1, y)  , 
    (x+1, y+1)
 (x,y) to 
    (x-1, y-1), 
    (x-1, y)  , 
    (x-1, y+1), 
    (x  , y-1),
    (x  , y+1), 
    (x+1, y-1), 
    (x+1, y)  , 
    (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.

NOTE: This question is intentionally left slightly vague. Clarify the question by trying out a few cases in the “See Expected Output” section.

Problem Constraints

1 <= |A| <= 10^6
- 2 * 10^3 <= Ai, Bi <= 2 * 10^3
|A| == |B|
1 <= |A| <= 10^6
- 2 * 10^3 <= Ai, Bi <= 2 * 10^3
|A| == |B|

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.
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.
Return an Integer, i.e minimum number of steps.

Example Input

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

Example Output

Output 1:
 2
Output 1:
 2

Example Explanation

Explanation 1:

 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).
Explanation 1:

 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

swift
import Foundation

class Solution {
    
    func distance(_ A:(Int,Int), _ B:(Int,Int)) -> Int {
        let xdiff = abs(A.0 - B.0)
        let ydiff = abs(A.1 - B.1)
        return min(xdiff,ydiff) + abs(xdiff - ydiff)
        
    }
    
	func coverPoints(_ A: inout [Int], _ B: inout [Int]) -> Int {
        let len = A.count
        var ret = 0
        for i in 1..<len{
            ret += distance((A[i-1],B[i-1]),(A[i],B[i]))
            // ret += d
        }
        return ret
	}
}
import Foundation

class Solution {
    
    func distance(_ A:(Int,Int), _ B:(Int,Int)) -> Int {
        let xdiff = abs(A.0 - B.0)
        let ydiff = abs(A.1 - B.1)
        return min(xdiff,ydiff) + abs(xdiff - ydiff)
        
    }
    
	func coverPoints(_ A: inout [Int], _ B: inout [Int]) -> Int {
        let len = A.count
        var ret = 0
        for i in 1..<len{
            ret += distance((A[i-1],B[i-1]),(A[i],B[i]))
            // ret += d
        }
        return ret
	}
}

References