[LeetCode] Campus Bikes

1057. Campus Bikes

On a campus represented on the X-Y plane, there are n workers and m bikes, with n <= m.

You are given an array workers of length n where workers[i] = [xi, yi] is the position of the ith worker. You are also given an array bikes of length m where bikes[j] = [xj, yj] is the position of the jth bike. All the given positions are unique.

Assign a bike to each worker. Among the available bikes and workers, we choose the (workeri, bikej) pair with the shortest Manhattan distance between each other and assign the bike to that worker.

If there are multiple (workeri, bikej) pairs with the same shortest Manhattan distance, we choose the pair with the smallest worker index. If there are multiple ways to do that, we choose the pair with the smallest bike index. Repeat this process until there are no available workers.

Return an array answer of length n, where answer[i] is the index (0-indexed) of the bike that the ith worker is assigned to.

The Manhattan distance between two points p1 and p2 is Manhattan(p1, p2) = |p1.x - p2.x| + |p1.y - p2.y|.

  • Time : O(nm)
  • Space : O(nm)

    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
    class Solution {
    int dist(vector<int>& w, vector<int>& b) {
    return abs(w[0] - b[0]) + abs(w[1] - b[1]);
    }
    public:
    vector<int> assignBikes(vector<vector<int>>& workers, vector<vector<int>>& bikes) {
    vector<int> res(workers.size(), -1);
    unordered_set<int> takenBikes;
    map<int, vector<pair<int,int>>> dists;
    for(int i = 0; i < workers.size(); i++) {
    for (int j = 0; j < bikes.size(); j++) {
    dists[dist(workers[i], bikes[j])].push_back({i, j});
    }
    }
    for(int dis = 1; dis <= 2000; dis++) {
    for(auto& [worker, bike]: dists[dis]) {
    if(res[worker] == -1 and !takenBikes.count(bike)) {
    res[worker] = bike;
    takenBikes.insert(bike);
    }
    }
    }
    return res;
    }
    };
  • heap solution

  • Time : O(nm lognm)
  • Space : O(nm)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
int dist(vector<int>& w, vector<int>& b) {
return abs(w[0] - b[0]) + abs(w[1] - b[1]);
}
public:
vector<int> assignBikes(vector<vector<int>>& workers, vector<vector<int>>& bikes) {
vector<int> res(workers.size(), -1);
unordered_set<int> takenBikes;
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int,3>>> pq;
for(int i = 0; i < workers.size(); i++) {
for(int j = 0; j < bikes.size(); j++) {
pq.push({dist(workers[i],bikes[j]), i, j});
}
}
while(takenBikes.size() < workers.size()) {
auto [_, worker, bike] = pq.top(); pq.pop();
if(res[worker] != -1 or takenBikes.count(bike)) continue;
res[worker] = bike;
takenBikes.insert(bike);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/15/PS/LeetCode/campus-bikes/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.