Implement Manhattan Distance
Implement Manhattan Distance
The Manhattan distance (L1 distance) between two equal-length vectors x, y is:
d(x,y)=i∑∣xi−yi∣Examples
Input: x = [1,2,3], y = [2,4,6]
Output: 6.0
|1-2| + |2-4| + |3-6| = 1 + 2 + 3 = 6
Input: x = [-1,-2], y = [1,2]
Output: 6.0
|-1-1| + |-2-2| = 2 + 4 = 6
Input: x = [0,0,0], y = [0,0,0]
Output: 0.0
Hint 1
Convert inputs to NumPy arrays first, then use vectorized operations to compute absolute differences.
Requirements
- Must work for lists or NumPy arrays
- Must return a float
- Must be vectorized (no Python element loops)
Constraints
- Time limit: 200 ms, Memory: 64 MB
- NumPy only (no sklearn, scipy)
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array
Implement Manhattan Distance
Implement Manhattan Distance
The Manhattan distance (L1 distance) between two equal-length vectors x, y is:
d(x,y)=i∑∣xi−yi∣Examples
Input: x = [1,2,3], y = [2,4,6]
Output: 6.0
|1-2| + |2-4| + |3-6| = 1 + 2 + 3 = 6
Input: x = [-1,-2], y = [1,2]
Output: 6.0
|-1-1| + |-2-2| = 2 + 4 = 6
Input: x = [0,0,0], y = [0,0,0]
Output: 0.0
Hint 1
Convert inputs to NumPy arrays first, then use vectorized operations to compute absolute differences.
Requirements
- Must work for lists or NumPy arrays
- Must return a float
- Must be vectorized (no Python element loops)
Constraints
- Time limit: 200 ms, Memory: 64 MB
- NumPy only (no sklearn, scipy)
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array