Loading...
You are given three integer arrays of the same length: counts, xs, and ys. For each index i, there are counts[i] people located at the point (xs[i], ys[i]) on a grid.
Choose a meeting point (x, y) (any pair of integers) that minimizes the total travel cost, where each person's cost is the Manhattan distance from their location to the meeting point:
totalCost(x, y) = Σ counts[i] * (|x - xs[i]| + |y - ys[i]|)
Return the minimum possible total cost.
Input: counts = [1, 2], xs = [1, 3], ys = [1, 3]
Output: 4
Explanation: Meeting at (3, 3): the 1 person at (1, 1) travels |3-1| + |3-1| = 4,
and the 2 people at (3, 3) travel 0. Total = 1*4 + 2*0 = 4. No point does better.
Input: counts = [5], xs = [-7], ys = [9]
Output: 0
Explanation: Meeting at (-7, 9) costs nothing.
counts.length = xs.length = ys.lengthcounts[i] ≤104xs[i], ys[i] ≤106Click "Run" to test with sample cases or "Submit" to run all tests.