Festival Charging Carts
A music festival moves its phone-charging carts each evening using wristband pings. The routine has three steps. Everyone is served by the closest cart, then each cart rolls to the average position of the people it served. Because some people now have a different closest cart, the routine repeats until nobody switches or time runs out.
That routine is k-means, the classic way to find groups in unlabelled data. It is used to group documents by their embeddings, to compress image colours, and to split a vector-search index into regions so that a query only searches the nearest few.
Your task
Write placeCarts(people, carts, maxRounds). people[i] and carts[j] are positions with the same number of coordinates. Repeat the following steps for at most maxRounds rounds.
- Assign. Give every person to the nearest cart by straight-line distance, using the cart positions from the start of this round. If two carts are equally near, pick the lower cart index.
- Check. If every person has the same cart as in the previous round's assignment, stop now. In round 1 there is no previous assignment, so always go on to Move.
- Move. Move each cart to the average of its people's positions (each coordinate averaged separately). A cart with nobody assigned stays where it is.
Return the final cart positions, in the original cart order.
placeCarts(people: float[][], carts: float[][], maxRounds: int) → float[][] Numbers are accepted within 1e-6.
Examples
people = [[0, 0], [0, 2], [10, 0], [10, 2]] carts = [[0, 1], [1, 1]] maxRounds = 10
[[0, 1], [10, 1]]
In round 1, (0, 0) and (0, 2) are nearer cart 0, and (10, 0) and (10, 2) are nearer cart 1. Cart 0 moves to their average, (0, 1), and cart 1 moves to (10, 1). In round 2 nobody switches, so the process stops.
people = [[0], [2], [4], [10]] carts = [[1], [3]] maxRounds = 1
[[1], [7]]
Only one round is allowed. The person at 0 is nearer cart 0. The person at 2 is 1 from both carts, so the tie goes to cart 0. The people at 4 and 10 go to cart 1. Cart 0 moves to (0 + 2) / 2 = 1, and cart 1 moves to (4 + 10) / 2 = 7.
people = [[1, 1], [2, 2]] carts = [[0, 0], [50, 50]] maxRounds = 5
[[1.5, 1.5], [50, 50]]
Cart 1 is far from everyone and never gets a person, so it stays at (50, 50). Cart 0 moves to (1.5, 1.5) in round 1, and round 2 changes nothing.
Constraints
- 1 ≤ people.length ≤ 1,000
- 1 ≤ carts.length ≤ 20
- 1 ≤ number of coordinates ≤ 5
- -1,000 ≤ each coordinate ≤ 1,000
- 1 ≤ maxRounds ≤ 100
- Answers within 0.000001 of the expected values are accepted.
Start with brute force
The best grouping is the one that minimises the total squared distance. Trying every grouping means k^n options, which for 20 carts and 1,000 people is a number over 1,300 digits long. k-means skips that search. Each round can only lower the total or keep it the same, so the process settles quickly, usually at a good answer but not always the best one. Each round costs O(n · k · d) for n people, k carts and d coordinates.
Hints
0 of 4 shown- Hint 1
Run the first example by hand for one round and note where each cart ends up. Then consider what would change if cart 0 moved as soon as its first person was assigned, because the next person would no longer see the same carts.
- Hint 2
Each round has two separate phases. First decide everyone's cart from the positions at the start of the round and keep the assignment in its own list. Only after that, move the carts.
- Hint 3
To move the carts, keep a running coordinate sum and a people count per cart. A cart with a count of 0 has no average, so leave it where it is.
- Hint 4
Loop up to
maxRoundstimes: build the assignment list (nearest cart, lower index on ties, comparing squared distances). If it equals the previous round's list, stop. Otherwise sum and count per cart, divide, and keep this assignment for the next round.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
k-means alternates two steps. With the carts held still, each person picks the nearest cart. With those choices held still, each cart moves to the middle of its people. Neither step can raise the total squared distance, so the process settles.
The trace below uses the first example, with people at (0, 0), (0, 2), (10, 0) and (10, 2) and carts at (0, 1) and (1, 1).
- In round 1, the assign step puts (0, 0) with cart 0, because it is 1 from cart 0 and √2 from cart 1. The person at (0, 2) is assigned the same way, and (10, 0) and (10, 2) are nearer cart 1. The assignment is [0, 0, 1, 1], and since there is no previous round the process continues.
- In round 1, the move step sends cart 0 to the mean of (0, 0) and (0, 2), which is (0, 1), and cart 1 to the mean of (10, 0) and (10, 2), which is (10, 1).
- In round 2, the assignment is still [0, 0, 1, 1]. It is the same as in the last round, so the process stops.
There are two common mistakes. One is moving a cart while people are still being assigned, so that later people see a half-moved cart. The other is dividing by a zero count instead of leaving an empty cart in place.
function placeCarts(people, carts, maxRounds) {
const nearest = (p, centers) => {
let best = 0, bestDist = Infinity;
centers.forEach((c, j) => {
let dist = 0;
for (let d = 0; d < p.length; d++) dist += (p[d] - c[d]) * (p[d] - c[d]);
if (dist < bestDist) { best = j; bestDist = dist; } // strict: ties keep the lower index
});
return best;
};
let centers = carts.map((c) => c.slice());
let previous = null;
for (let round = 0; round < maxRounds; round++) {
// 1. Assign everyone using the current positions. Nobody moves yet.
const owner = people.map((p) => nearest(p, centers));
if (previous && owner.every((o, i) => o === previous[i])) break; // stable
// 2. Move each cart to the mean of its people. An empty cart stays put.
const sums = centers.map((c) => c.map(() => 0));
const counts = centers.map(() => 0);
people.forEach((p, i) => {
counts[owner[i]]++;
p.forEach((v, d) => { sums[owner[i]][d] += v; });
});
centers = centers.map((c, j) => (counts[j] ? sums[j].map((s) => s / counts[j]) : c));
previous = owner;
}
return centers;
}def place_carts(people, carts, max_rounds):
def nearest(p, centers):
best, best_dist = 0, float('inf')
for j, c in enumerate(centers):
dist = 0.0
for a, b in zip(p, c):
dist += (a - b) * (a - b)
if dist < best_dist: # strict: ties keep the lower index
best, best_dist = j, dist
return best
centers = [list(c) for c in carts]
previous = None
for _ in range(max_rounds):
# 1. Assign everyone using the current positions. Nobody moves yet.
owner = [nearest(p, centers) for p in people]
if owner == previous:
break # stable: nothing would move
# 2. Move each cart to the mean of its people. An empty cart stays put.
dims = len(carts[0])
sums = [[0.0] * dims for _ in centers]
counts = [0] * len(centers)
for p, o in zip(people, owner):
counts[o] += 1
for d, v in enumerate(p):
sums[o][d] += v
centers = [[s / counts[j] for s in sums[j]] if counts[j] else centers[j]
for j in range(len(centers))]
previous = owner
return centersRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.