-
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
Changes from all commits
ec6d7fe
7e887ff
046f39c
543cff1
feacd11
56778a4
ae29b37
7df51fc
8e3a4ef
772029c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.