Easy · Intervals

Distance of nearest stores

Given a non-empty list of store points and a list of customer points (x, y), return an array whose i-th entry is the squared Euclidean distance from customer i to its nearest store.

Examples

Example 1

{
  "stores": [
    {"x": 0, "y": 0},
    {"x": 4, "y": 0}
  ],
  "customers": [
    {"x": 1, "y": 1},
    {"x": 3, "y": 2}
  ]
}

Output: [2, 5]

Example 2

{
  "stores": [
    {"x": 0, "y": 0},
    {"x": 5, "y": 5},
    {"x": 2, "y": 1}
  ],
  "customers": [
    {"x": 1, "y": 0},
    {"x": 4, "y": 4},
    {"x": 2, "y": 2}
  ]
}

Output: [1, 2, 1]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Intervals problems