Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package algorithms.curated170.medium;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CampusBikesBucketSort {

int w, b;
List<int[]>[] buckets = new List[2001];

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;

b = bikes.length;

assignToBuckets(workers, bikes);

return assignFromBuckets();
}

private void assignToBuckets(int[][] workers, int[][] bikes) {
for (int i = 0; i < w; i++) {
for (int j = 0; j < b; j++) {
int dist = dist(workers[i], bikes[j]);
if (buckets[dist] == null) {
buckets[dist] = new ArrayList<>();
}

buckets[dist].add(new int[] { i, j });
}
}
}

private int[] assignFromBuckets() {

int[] wo = new int[w];
boolean[] bikeAssigned = new boolean[b];
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]) {

int w_ = pair[0];
int b_ = pair[1];

if (!workerAssigned[w_] && !bikeAssigned[b_]) {
wo[w_] = b_;

bikeAssigned[b_] = true;
workerAssigned[w_] = true;
assigned++;
}
}
}
}
return wo;
}

private int dist(int[] w, int[] b) {
return Math.abs(w[0] - b[0]) + Math.abs(w[1] - b[1]);
}

public static void main(String[] args) {
int[][] workers0 = {{664,994},{3,425},{599,913},{220,352},{145,348},{604,428},{519,183},{732,148}};

int[][] bikes0 = {{611,698},{113,338},{579,770},{276,588},{948,679},{731,525},{925,877},{182,281},{39,299}};


var solution = new CampusBikesBucketSort();
System.out.println(Arrays.toString(solution.assignBikes(workers0, bikes0)));
solution.printBuckets();
}

void printBuckets()
{
StringBuilder sb = new StringBuilder();
for(List<int[]> k : buckets)
{
if(k==null)
{
continue;
}
for(int[] j : k)
{
sb.append(Arrays.toString(j));
}
sb.append("/");
}
System.out.println(sb.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package algorithms.curated170.medium;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
Comment on lines +1 to +5
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.


public class CampusBikesPriorityQueue {

int w, b;
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
return a[0] != b[0] ? a[0] - b[0] : (a[1] != b[1] ? a[1] - b[1] : (a[2] - b[2]));
}
});

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

w = workers.length;
b = bikes.length;

createHeap(workers, bikes);

return assignFromHeap();
}

private void createHeap(int[][] workers, int[][] bikes) {
for (int i = 0; i < w; i++) {
for (int j = 0; j < b; j++) {

int dist = dist(workers[i], bikes[j]);
pq.offer(new int[] { dist, i, j });
}
}
}

private int[] assignFromHeap() {

int[] wo = new int[w];

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

int assigned = 0;
while (!pq.isEmpty() && assigned < w) {
int[] entry = pq.poll();

int w_ = entry[1];
int b_ = entry[2];

if (!workerAssigned[w_] && !bikeAssigned[b_]) {
wo[w_] = b_;

workerAssigned[w_] = true;
bikeAssigned[b_] = true;
assigned++;
}
}

return wo;
}

private int dist(int[] w, int[] b) {
return Math.abs(w[0] - b[0]) + Math.abs(w[1] - b[1]);
}

public static void main(String[] args) {
int[][] workers0 = {{664,994},{3,425},{599,913},{220,352},{145,348},{604,428},{519,183},{732,148}};

int[][] bikes0 = {{611,698},{113,338},{579,770},{276,588},{948,679},{731,525},{925,877},{182,281},{39,299}};


var solution = new CampusBikesPriorityQueue();
System.out.println(Arrays.toString(solution.assignBikes(workers0, bikes0)));
}
}