Skip to content

Conversation

@ErdemT09
Copy link
Collaborator

@ErdemT09 ErdemT09 commented May 19, 2021

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 3
And 4 bikes, indexed again as such: 0 1 2 3
We can represent the state of which bikes have been picked using a binary representation 1's or 0's or with boolean values of true and false.
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 0 and increase the index of our worker for consistency.
This gives rise to the following decision pattern:
image
After assigning a bike to the 0th and the 1st worker, we have to also have to make a decision about how the remaining two bikes should be assigned to worker 2 and 3. 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:
image
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 1 b bits to the left and subtract 1 from it:
As for the example:
Shift 4 bits: 10000
Substract 1: 10000-1 = 1111
We can also turn off bits to signal taken bikes using the xor operator.

@ErdemT09 ErdemT09 marked this pull request as ready for review May 19, 2021 12:46
@ErdemT09
Copy link
Collaborator Author

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
@ErdemT09
Copy link
Collaborator Author

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?

@altay9
Copy link
Collaborator

altay9 commented May 19, 2021

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

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

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.

Copy link
Collaborator Author

@ErdemT09 ErdemT09 May 19, 2021

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added.

altay9
altay9 previously approved these changes May 20, 2021
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.

Thank you Erdem.
Your collaboration is so valuable as always.
Approved.

@ErdemT09
Copy link
Collaborator Author

Thank you Erdem.
Your collaboration is so valuable as always.
Approved.

I have added an explanation now.
How does this algorithm perform on LeetCode?

@ErdemT09 ErdemT09 linked an issue May 20, 2021 that may be closed by this pull request
@altay9
Copy link
Collaborator

altay9 commented May 20, 2021

Thank you Erdem.
Your collaboration is so valuable as always.
Approved.

I have added an explanation now.
How does this algorithm perform on LeetCode?

It is remarkably fast.
Thanks again for the explanation by the way. No worries about the video. You can do it whenever you want and I am sure you will nail it too.

image

@ErdemT09
Copy link
Collaborator Author

ErdemT09 commented May 20, 2021

May I get an approval again?
I had to remove an unused variable that I forgot.

@ErdemT09 ErdemT09 merged commit bdc9919 into master May 20, 2021
@ErdemT09 ErdemT09 deleted the 1066.-Campus-Bikes-II branch May 20, 2021 10:28
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.

1066. Campus Bikes II

3 participants