-
Notifications
You must be signed in to change notification settings - Fork 7
1057. Campus Bikes #195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1057. Campus Bikes #195
Conversation
|
|
||
| public int[] assignBikes(int[][] workers, int[][] bikes) { | ||
|
|
||
| w = workers.length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there may be more bikes than workers, we need to have a variable for bikes' size also.
workerSize = workers.length;
bikeSize = bikes.length;
| private void assignToBuckets(int[][] workers, int[][] bikes) { | ||
| for (int i = 0; i < w; i++) { | ||
| for (int j = 0; j < w; j++) { | ||
| int dis = Math.abs(bikes[j][0] - workers[i][0]) + Math.abs(bikes[j][1] - workers[i][1]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, your solution is remarkably modular and very nicely organized.
Therefore we can also modularize the distance function as it has no direct dependence on the assignToBucketsFunction in which it will be injected.
private int dist(int[] w, int[] b) {
return Math.abs(w[0] - b[0]) + Math.abs(w[1] - b[1]);
}
| int[] res = new int[w]; | ||
| boolean[] bikeAssigned = new boolean[w]; | ||
| boolean[] workerAssigned = new boolean[w]; | ||
| short assigned = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not grasp why the assigned variable matters. We can simply remove it.
We could also check if all the workers are assigned with a bike like this:
workerAssigned.length<=workerSize
But, I am not sure it worths checking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
workerAssigned is an array, not a list. That's why I needed a variable like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you are right.
| for (int i = 0; i < buckets.length && assigned < w; i++) { | ||
| if (buckets[i] != null) { | ||
| for (int[] pair : buckets[i]) { | ||
| if (!workerAssigned[pair[0]] && !bikeAssigned[pair[1]]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of reading from the array in each iteration more than once, we can assign the value to a variable once:
int w = pair[0];
int b = pair[1];
| package algorithms.curated170.medium; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Comparator; | ||
| import java.util.PriorityQueue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can apply the same refactoring with the Bucket Sort solution.
| boolean[] bikeAssigned = new boolean[w]; | ||
| boolean[] workerAssigned = new boolean[b]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have fixed it, actually at the same moment without seeing this comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does the Priority Queue solution perform?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should work.
|
This question is a good example of modularity helping in managing code. If it hasn't been for the fact that we have separated the code into multiple methods, it would have been harder for us to debug it. |
|
Absolutely. |
altay9
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modular and clear code.


Issue.
Approach-1: Bucket Sort
The maximum possible dimension of the grid are 1000 X 1000, which means that the maximum distance is 2000.
This means that we can store all possible worker-bike pairs with respect to their distances in an array of length 20.
After this procedure check each List of pairs (bucket) in this array (buckets). we traverse this array naturally from 0 to its end.
In each bucket, if we have not encountered this bike and the worker, that means that these bike and the worker are their pair. We store the bike's index in an array.
The setting by indexing is also taken care of, as we built the buckets iteratively from the workers and the bikes.
We return the stored bike indices at the end.
Approach-2: Priority Queue:
Here, we use a priority queue that stores a triplet* of the numbers ( distance between bike and worker, worker id, bike id). Similarly, we iterate through each bike and worker and add the relative triplet to the priority queue.
After creating the priority queue, the items we poll from it are compared first by their distances. If same, by the worker ids, if same, by the bike ids. We poll each item from the priority queue until we have assigned all the workers a bike. In order to avoid multiple worker-bike pairs interfering, we keep two checkers for this as we had done in the previous question.
*https://en.wikipedia.org/wiki/Tuple Learning about the naming here is helpful.