← Home

1515. Best Position for a Service Centre

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
151

Let's make this one less mysterious. For 1515. Best Position for a Service Centre, the solution in this repository is mainly a two pointers solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: two pointers.

The notes already sitting in the source point us in the right direction:

  • geometric mean
  • https://leetcode.com/problems/best-position-for-a-service-centre/discuss/731577/C%2B%2B-with-picture-zoom-in

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are getMinDistSum.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//geometric mean
02//https://leetcode.com/problems/best-position-for-a-service-centre/discuss/731577/C%2B%2B-with-picture-zoom-in
03//Runtime: 396 ms, faster than 25.00% of C++ online submissions for Best Position for a Service Centre.
04//Memory Usage: 8 MB, less than 100.00% of C++ online submissions for Best Position for a Service Centre.
05class Solution {
06public:
07    double getMinDistSum(vector<vector<int>>& positions) {
08        double left, bottom, right, top;
09        left = bottom = numeric_limits<double>::max();
10        right = top = numeric_limits<double>::lowest();
11
12        for(vector<int>& pt : positions){
13            left = min(left, (double)pt[0]);
14            right = max(right, (double)pt[0]);
15            bottom = min(bottom, (double)pt[1]);
16            top = max(top, (double)pt[1]);
17        }
18
19        double minDist = numeric_limits<double>::max();
20        double minX, minY;
21        for(double delta = 1.0; delta >= 1e-5; delta/=10){
22            for(double x = left; x <= right; x += delta){
23                for(double y = bottom; y <= top; y += delta){
24                    double dist = 0.0;
25                    for(vector<int>& pt : positions){
26                        dist += sqrt(pow((double)pt[0]-x, 2) + pow((double)pt[1]-y, 2));
27                    }
28                    if(dist < minDist){
29                        minDist = dist;
30                        minX = x;
31                        minY = y;
32                    }
33                }
34            }
35            // WA: 48 / 56 test cases passed.
36            // left = minX - delta*0.5;
37            // right = minX + delta*0.5;
38            // bottom = minY - delta*0.5;
39            // top = minY + delta*0.5;
40            left = minX - delta;
41            right = minX + delta;
42            bottom = minY - delta;
43            top = minY + delta;
44        }
45
46        return minDist;
47    }
48};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.