See the original problem on HackerRank.
You are a salesman and your goal is to visit all the houses along a certain street.
You walk at a constant rate and can start and end at any location. You may visit the houses in any order you wish. Given the locations of the houses along the street, what is the minimum amount of time needed for you to visit all the houses?
Formally, we represent the street as a number line and the positions of the houses as numbers along this line.
There are \(n\) houses numbered \(1,2…,n\) and they are located at c(x_1\), respectively. Assume that it takes \(0\) (negligible) time to visit a house, and walking from house \(i\) to house \(j\) takes \(|x_i-x_j|\) minutes.
Complete the function minimumTime which takes an integer array , denoting the locations of the houses as input. Return an integer denoting the minimum amount of time, in minutes, needed to visit all the houses.
Input Format
he first line contains a single integer \(t\) denoting the number of streets. The description of \(t\) streets follow.
Each street is described by two lines. The first line contains a single integer $n$ denoting the number of houses in that street. The second line contains \(n\) integers \(x_1,x_2,…,x_n\) denoting the locations of the houses along the street.
Constraints
- \(1\leqslant t \leqslant 500\)
- \(1\leqslant n \leqslant 50\)
- \(1\leqslant x_i \leqslant 1000\)
- The houses’ locations are distinct
Output Format
For each street, print a single line containing a single integer denoting the minimum amount of time, in minutes, needed to visit all the houses.
Example
Input:
|
|
Output:
|
|
Explanation:
In this case, there are \(n=3\) houses at locations \(x_1=11, x_2=6\) and \(x_3=9\).
We can consider \(6\) possible orders to visit the houses:
- \(1 \rightarrow 2 \rightarrow 3\). This will take \(|11-6| + |6-9| = 8\) minutes.
- \(1 \rightarrow 3 \rightarrow 2\). This will take \(|11-9| + |9-6| = 5\) minutes.
- \(2 \rightarrow 1 \rightarrow 3\). This will take \(|6-11| + |11-9| = 7\) minutes.
- \(2 \rightarrow 3 \rightarrow 1\). This will take \(|6-9| + |9-11| = 5\) minutes.
- \(3 \rightarrow 1 \rightarrow 2\). This will take \(|9-11| + |11-6| = 7\) minutes.
- \(3 \rightarrow 2 \rightarrow 1\). This will take \(|9-6| + |6-11| = 8\) minutes.
Solutions
The solution is simply the difference between the max and the min of the input array:
A C++ Solution:
|
|
The same in Python:
|
|
Haskell:
|
|
Rust:
|
|
One can easily fall into the trap of following the example by calculating the adjacent differences of the sorted array. That’s more expensive but it’s a valid solution too.
Here is a succinct example in C++:
|
|
The pattern here is zip | map | reduce, that is: we zip the array with itself by shifting it by 1, we map every pair by calculating the difference (it’s always positive because the array is sorted in ascending order) and we reduce by aggregating the temporary results along the way.
|
|
|
|