Skip to content

Commit 0ddad01

Browse files
committed
1057
1 parent f55a9a8 commit 0ddad01

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

leetcode/1057. Campus Bikes/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,102 @@ public:
8282
return ans;
8383
}
8484
};
85+
```
86+
87+
## Solution 2. Bucket Sort
88+
89+
Note that this problem is not asking an assignment with which the total Manhattan distance is minimized. It asked us to go through the pairs from shortest distance to longest distance, and choose the pairs in ascending order of worker index then in ascending order of bike index.
90+
91+
Take the example testcase for example,
92+
93+
```
94+
[[0,0],[2,1]]
95+
[[1,2],[3,3]]
96+
```
97+
98+
The total Manhattan distance of the answer is `2 + 6 = 8`, but assignment `0->0, 1->1` has total distance of `3 + 3 = 6`.
99+
100+
So, we can put the pairs in buckets where each bucket is the distance. Since we loop in ascending order of worker index then in ascending order of bike index, so everything is already sorted in the required way.
101+
102+
```cpp
103+
// OJ: https://leetcode.com/problems/campus-bikes/
104+
// Author: github.com/lzl124631x
105+
// Time: O(NM + K) where `K` is the maximum Manhattan distance. It's 1998 in this problem
106+
// Space: O(NM + K)
107+
class Solution {
108+
public:
109+
vector<int> assignBikes(vector<vector<int>>& W, vector<vector<int>>& B) {
110+
int N = W.size(), M = B.size(), minDist = INT_MAX, cnt = 0;
111+
vector<array<int, 2>> v[1999];
112+
auto dist = [](auto &a, auto &b) {
113+
return abs(a[0] - b[0]) + abs(a[1] - b[1]);
114+
};
115+
for (int i = 0; i < N; ++i) {
116+
for (int j = 0; j < M; ++j) {
117+
int d = dist(W[i], B[j]);
118+
v[d].push_back({i, j});
119+
minDist = min(minDist, d);
120+
}
121+
}
122+
vector<bool> used(M);
123+
vector<int> ans(N, - 1);
124+
for (int d = minDist; d < 1999 && cnt < N; ++d) {
125+
for (auto &[w, b] : v[d]) {
126+
if (ans[w] != -1 || used[b]) continue;
127+
used[b] = true;
128+
ans[w] = b;
129+
++cnt;
130+
}
131+
}
132+
return ans;
133+
}
134+
};
135+
```
136+
137+
## Solution 3. Priority Queue
138+
139+
The idea is similar to the priority queue solution to merging K sorted list.
140+
141+
For each worker, we maintain a list of bike indexes in ascending order of distance.
142+
143+
We maintain a priority queue of `N` elements each of which is a current bike index corresponding to a worker.
144+
145+
```cpp
146+
// OJ: https://leetcode.com/problems/campus-bikes/
147+
// Author: github.com/lzl124631x
148+
// Time: O(MNlogN)
149+
// Space: O(MN)
150+
class Solution {
151+
public:
152+
vector<int> assignBikes(vector<vector<int>>& W, vector<vector<int>>& B) {
153+
int N = W.size(), M = B.size();
154+
vector<vector<array<int, 2>>> v(N, vector<array<int, 2>>(M));
155+
auto dist = [](auto &a, auto &b) {
156+
return abs(a[0] - b[0]) + abs(a[1] - b[1]);
157+
};
158+
for (int i = 0; i < N; ++i) {
159+
for (int j = 0; j < M; ++j) {
160+
v[i][j] = { dist(W[i], B[j]), j };
161+
}
162+
sort(begin(v[i]), end(v[i]));
163+
}
164+
priority_queue<array<int, 4>, vector<array<int, 4>>, greater<>> pq;
165+
for (int i = 0; i < N; ++i) pq.push({ v[i][0][0], i, v[i][0][1], 0 });
166+
vector<bool> used(M);
167+
vector<int> ans(N, -1);
168+
for (int i = 0; i < N; ) {
169+
auto [d, w, b, j] = pq.top();
170+
pq.pop();
171+
if (used[b]) {
172+
int nb = v[w][j + 1][1], nd = dist(W[w], B[nb]);
173+
pq.push({nd, w, nb, j + 1});
174+
continue;
175+
}
176+
used[b] = true;
177+
ans[w] = b;
178+
++i;
179+
}
180+
return ans;
181+
}
182+
};
85183
```

0 commit comments

Comments
 (0)