-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1515-best-position-for-a-service-centre.js
72 lines (61 loc) · 1.97 KB
/
1515-best-position-for-a-service-centre.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* 1515. Best Position for a Service Centre
* https://leetcode.com/problems/best-position-for-a-service-centre/
* Difficulty: Hard
*
* A delivery company wants to build a new service center in a new city. The company knows the
* positions of all the customers in this city on a 2D-Map and wants to build the new center in
* a position such that the sum of the euclidean distances to all customers is minimum.
*
* Given an array positions where positions[i] = [xi, yi] is the position of the ith customer on
* the map, return the minimum sum of the euclidean distances to all customers.
*
* In other words, you need to choose the position of the service center [xcentre, ycentre] such
* that the following formula is minimized:
* - Answers within 10-5 of the actual value will be accepted.
*/
/**
* @param {number[][]} positions
* @return {number}
*/
var getMinDistSum = function(positions) {
const n = positions.length;
let xSum = 0;
let ySum = 0;
for (const [x, y] of positions) {
xSum += x;
ySum += y;
}
let xCenter = xSum / n;
let yCenter = ySum / n;
const epsilon = 1e-7;
const maxIterations = 1000;
for (let iter = 0; iter < maxIterations; iter++) {
let xNumerator = 0;
let yNumerator = 0;
let denominator = 0;
for (const [x, y] of positions) {
const dx = xCenter - x;
const dy = yCenter - y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < epsilon) continue;
const weight = 1 / dist;
xNumerator += x * weight;
yNumerator += y * weight;
denominator += weight;
}
if (denominator < epsilon) break;
const newX = xNumerator / denominator;
const newY = yNumerator / denominator;
if (Math.abs(newX - xCenter) < epsilon && Math.abs(newY - yCenter) < epsilon) {
break;
}
xCenter = newX;
yCenter = newY;
}
let sumDist = 0;
for (const [x, y] of positions) {
sumDist += Math.sqrt((xCenter - x) ** 2 + (yCenter - y) ** 2);
}
return sumDist;
};