You are given a sequence of 2D points in an order that you have to cover them. Every point is represented by 2 digits (x and y coordinate). Compute the distance from the first point to the last point. Movement in all eight directions is allowed (horizontal, vertical, and diagonal). You can test your solution here.
Example
Input X: [0, 1, 1] Input Y: [0, 1, 2] Output: 2
The input is [(0,0), (1,1), (1,2)]
, and the distance from (0,0)
to (1,1)
is 1, while distance from (1,1)
to (1,2)
is also 1. The total distance is 2.
Possible Corner Case Inputs
- Zero length
- Single point
- Movement back (negative movement – i.e.: from 1 to 0)
- Movement only diagonally
- Any more?
Templates
C
/** * Traverse through all points, and return the total distance * * @return Integer * * @input X : Integer array corresponding to the X coordinate * @input n1 : Integer array's ( X ) length * @input Y : Integer array corresponding to the Y coordinate * @input n2 : Integer array's ( Y ) length */ int coverPoints(int* X, int n1, int* Y, int n2) { // TODO: Write your code here }
C++
/** * Traverse through all points, and return the total distance * * @return int * * @input X : int vector corresponding to the X coordinate * @input Y : int vector corresponding to the Y coordinate */ int coverPoints(vector<int> &X, vector<int> &Y) { // TODO: Write your code here }
Java
public class reach { /** * Traverse through all points, and return the total distance * * @return int * * @input X : ArrayList<Integer> corresponding to the X coordinate * @input Y : ArrayList<Integer> corresponding to the Y coordinate */ public int coverPoints(ArrayList<Integer> X, ArrayList<Integer> Y) { // TODO: Write your code here } }
Python
"""Traverse through all points, and return the total distance Args: X: list corresponding to the X coordinate Y: list corresponding to the Y coordinate Returns: integer, representing the total distance """ def coverPoints(X, Y): # TODO: Write your code here return