1515. Best Position for a Service Centre Hard
1/**
2 * [1515] Best Position for a Service Centre
3 *
4 * 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.
5 * 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.
6 * In other words, you need to choose the position of the service center [xcentre, ycentre] such that the following formula is minimized:
7 * <img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_edited.jpg" />
8 * Answers within 10^-5 of the actual value will be accepted.
9 *
10 * Example 1:
11 * <img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_e1.jpg" style="width: 377px; height: 362px;" />
12 * Input: positions = [[0,1],[1,0],[1,2],[2,1]]
13 * Output: 4.00000
14 * Explanation: As shown, you can see that choosing [xcentre, ycentre] = [1, 1] will make the distance to each customer = 1, the sum of all distances is 4 which is the minimum possible we can achieve.
15 *
16 * Example 2:
17 * <img alt="" src="https://assets.leetcode.com/uploads/2020/06/25/q4_e3.jpg" style="width: 419px; height: 419px;" />
18 * Input: positions = [[1,1],[3,3]]
19 * Output: 2.82843
20 * Explanation: The minimum possible sum of distances = sqrt(2) + sqrt(2) = 2.82843
21 *
22 *
23 * Constraints:
24 *
25 * 1 <= positions.length <= 50
26 * positions[i].length == 2
27 * 0 <= xi, yi <= 100
28 *
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/best-position-for-a-service-centre/
33// discuss: https://leetcode.com/problems/best-position-for-a-service-centre/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38 pub fn get_min_dist_sum(positions: Vec<Vec<i32>>) -> f64 {
39 0f64
40 }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn test_1515() {
51 }
52}
53
Back
© 2025 bowen.ge All Rights Reserved.