You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: leetcode/1057. Campus Bikes/README.md
+98Lines changed: 98 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -82,4 +82,102 @@ public:
82
82
return ans;
83
83
}
84
84
};
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.
0 commit comments