Skip to content

Conversation

@ErdemT09
Copy link
Collaborator

@ErdemT09 ErdemT09 commented May 11, 2021

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.

@ErdemT09 ErdemT09 marked this pull request as ready for review May 11, 2021 17:41
@ErdemT09 ErdemT09 linked an issue May 12, 2021 that may be closed by this pull request

public int[] assignBikes(int[][] workers, int[][] bikes) {

w = workers.length;
Copy link
Collaborator

@altay9 altay9 May 12, 2021

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]);
Copy link
Collaborator

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;
Copy link
Collaborator

@altay9 altay9 May 12, 2021

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator

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]]) {
Copy link
Collaborator

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];

Comment on lines +1 to +5
package algorithms.curated170.medium;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
Copy link
Collaborator

@altay9 altay9 May 12, 2021

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.

Comment on lines 37 to 38
boolean[] bikeAssigned = new boolean[w];
boolean[] workerAssigned = new boolean[b];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we fix these:

            boolean[] bikeAssigned = new boolean[b];
            boolean[] workerAssigned = new boolean[w];

image

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

@ErdemT09 ErdemT09 May 14, 2021

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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a little bit slow.
But, maybe it is normal for PQ.
image

@ErdemT09
Copy link
Collaborator Author

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.
Besides, making such attention mistakes is something that we should not do again in the future.

@altay9
Copy link
Collaborator

altay9 commented May 14, 2021

Absolutely.

Copy link
Collaborator

@altay9 altay9 left a 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.

@ErdemT09 ErdemT09 merged commit 6143b10 into master May 14, 2021
@ErdemT09 ErdemT09 deleted the 1057.-Campus-Bikes branch May 14, 2021 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1057. Campus Bikes

3 participants