-
Notifications
You must be signed in to change notification settings - Fork 7
1066. Campus Bikes II #275
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
Conversation
|
I have also added a array converter for converting Python/LeetCode/Java arrays in console print to actual 2 dimensional Java arrays. What should we do with it? |
1 similar comment
|
I have also added a array converter for converting Python/LeetCode/Java arrays in console print to actual 2 dimensional Java arrays. What should we do with it? |
We can add into repo just like a usual class file. Nice idea. |
| System.out.println(sol); | ||
| } | ||
|
|
||
| public static int[][] leetCodeIntegerGridConverter(String arr) { |
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.
You can create a package named "utils" or "util" and move this under a custom class.
You can name the class something like LeetCodeUtil as we can maintain and enhance it in the future.
| int[][] dp; | ||
|
|
||
| public int assignBikes(int[][] workers, int[][] bikes) { | ||
| dp = new int[workers.length][1<<bikes.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.
It looks we may not really need which specific worker has the bike. If we use "mask" as a database to persist the state of bike assignment, dp can be 1D.
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.
True, this holds valid because only n many digits could have been 0'ed when 2 workers are assigned.
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.
Added.
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.
Thank you Erdem.
Your collaboration is so valuable as always.
Approved.
I have added an explanation now. |
It is remarkably fast. |
|
May I get an approval again? |

Note: I tried recording a video for this but things didn't work out, so I will try to explain it with some text and drawings:
Let's say we have 4 workers, indexed as in the question:


0 1 2 3And 4 bikes, indexed again as such:
0 1 2 3We can represent the state of which bikes have been picked using a binary representation
1's or0's or with boolean values oftrueandfalse.Binary representation is the one used in this question. For example, our initial 4 unpicked bikes will have the binary representation of
1111. In this example we need to find the minimum sum of distances for this given state of bikes.To solve this problem, we need to solve sub-problems consisting of making decision about which worker should get which bike.
First, we begin by assigning the bikes to our worker at index
0and increase the index of our worker for consistency.This gives rise to the following decision pattern:
After assigning a bike to the
0th and the1st worker, we have to also have to make a decision about how the remaining two bikes should be assigned to worker2and3. But in the decision tree, it is visible that we are going to make the same calculations again. Instead of calculating them again, we can store the minimum distance result of that state in an array, so that we do not have to calculate that state again:Here are few states that reappear again. The careful reader can notice even more.
With that, we can solve this question in order of O(2 ^ b) time, as we only have to make decisions for each state of bikes. The actual time complexity will however be something like O ( w * b * 2 ^ b), because for each worker, we loop through each bike, and resolving a decision about every bike at that state.
The binary operations required for this algorithm are quite simple. To obtain an integer with binary representation of such, we just have to shift
1bbits to the left and subtract1from it:As for the example:
Shift 4 bits:
10000Substract 1:
10000-1 = 1111We can also turn off bits to signal taken bikes using the xor operator.