Geometry · Easy
An offline tracker stores integer grid coordinates in visit order. The summary needs the travelled Manhattan distance, not displacement from the start.
Sum distances between adjacent recorded points.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ points:[x:integer,y:integer][] }
What each field means
points— recorded[x, y]coordinates in travel order.
Output
Return value and rules
- Return the sum of Manhattan distances between adjacent points:
abs(dx) + abs(dy). - Do not use straight-line Euclidean distance or close the route automatically.
- Fewer than two points return
0.
Constraints
- 0 <= points.length <= 100000
- Coordinates in [-1000000,1000000]; repeated positions are allowed.
Examples
Example 1
{
"points": [
[
0,
0
],
[
3,
4
],
[
0,
0
]
]
}Returns
14Example 2
{
"points": []
}Returns
0Required complexity
O(n) time and O(1) auxiliary space