Locker Room Algorithm [closed] - algorithm

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Note: Just to give a heads up, this is not an assignment for my school since I myself don't even know which school wrote this question. Hopefully no misunderstanding occurred.
I found this interesting questions about locker room:
The locker room of the Rec has N lockers that are labeled 1, 2, . . ., N .
Each locker is locked, but can be opened using its unique key.
Copies of the key to each locker are in its adjacent lockers; i.e. a
copy of the key to locker i is placed in locker i + 1 and i − 1 (the
key to locker 1 is only in locker 2 and the key to locker N is only in
locker N − 1).
T tennis balls are inside T distinct lockers (and you
know which of the lockers they are in). You are given keys to M of
the lockers and your goal is to collect all of the tennis balls by
opening the least number of lockers.
For pictures you can see it directly to the file here.
I have to give this questions to some freshman but I wanted to make sure first that I myself already have the correct answer beforehand.
What I'm thinking is that:
Need to check the ball one by one. So for each ball (ignore the other balls), each key have to visit by traversing to the designated ball. For each key, calculate the steps it takes to visit the ball. The smallest result is stored in an variable called "total steps".
Do this exact thing to the next ball and when I get the minimum steps for this current ball. I add this value to "total steps".
Special condition applied if there is a ball above the key, then key start traversing from i + 1 and i - 1.
My question is: am I right? I don't want to share the wrong algorithm to others since it's unprofessional. Looking forward to any comments, suggestions and inputs.

Your algorithm will not result in minimum number of steps. You can not consider balls independently. Lets consider the following case: You have only one key for locker number 1 and balls are in lockers 12, 10, 8, 6, 4, 2. If You consider the balls in the order I have given you will end up with total steps equal to 11 + 9 + 7 + 5 + 3 + 1, while you can solve the problem in a single pass of 11 steps. You should not ignore the boxes visited on the previous steps.

Here is a technique you can use:
Consider each locker (in which there is a ball, of which you have a key and which have none) to be a node in the graph. The nodes will be of two types:
A) Lockers having balls
B) Lockers of which you have keys.
C) Lockers which have none
Consider all lockers of type-A to be closed and type-B to be open.
From all open lockers, find paths to all closed lockers (from locker type-B) and open the locker in A with min path length. Also, open all lockers of type C which fall in this min-path and move them from category-C to B.
Repeat above step till all lockers in A are opened. Sum of all the min-path lengths that you have come across will be your answer.

Related

Dynamic Programming: Tennis Balls, Lockers, and Keys [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am having trouble finding the Optimal Dynamic Programming solution to a problem and would greatly appreciate any help. The optimal solution is O(T^2+M); I can only find a solution which is O(NM^2).
The problem is:
There are N lockers that are labeled 1,2...N. Each locker is locked, but can be opened using it's unique key. Copies of the key to each locker are in its adjacent lockers; i.e. a copy of the key to locker i is placed in locker i+1 and i-1 (the key to locker 1 is only in locker 2 and the key to locker N is only in locker N-1)
T tennis balls are inside T distinct lockers (and you know which lockers they are in). You are given keys to M of the lockers and your goal is to collect all of the tennis balls by opening the least number of lockers.
My only hint given is:
Hint: Can you efficiently decide whether there exist at least one tennis ball between the ith and jth locker?
First place a dummy cell at N + 1 with a key and no tennis ball.
Now start at the second key, and proceed until the last key (which, you should note, is a dummy key).
At each iteration, calculate the optimal solution to the tennis balls to the left of the current key (inclusive) for two cases: a. the current key is used, and b. the current key is not used. The optimal solution should also record the rightmost key actually used.
If the current key is not used, then use the previous best two solutions to update the cost, using the rightmost key actually used for covering the balls to the left of the current key (inclusive). If the current key is used, then for each of the balls to the left of the current key (inclusive), calculate whether it pays to open from this key or the previously used key (it depends on the midpoint between the keys).
The overall solution is the one that is at the dummy N + 1 cell, and doesn't use the key in it.

Powers of a half that sum to one [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Call every subunitary ratio with its denominator a power of 2 a perplex.
Number 1 can be written in many ways as a sum of perplexes.
Call every sum of perplexes a zeta.
Two zetas are distinct if and only if one of the zeta has as least one perplex that the other does not have. In the image shown above, the last two zetas are considered to be the same.
Find all the numbers of ways 1 can be written as a zeta with N perplexes. Because this number can be big, calculate it modulo 100003.
Please don't post the code, but rather the algorithm. Be as precise as you can.
This problem was given at a contest and the official solution, written in the Romanian language, has been uploaded at https://www.dropbox.com/s/ulvp9of5b3bfgm0/1112_descr_P2_fractii2.docx?dl=0 , as a docx file. (you can use google translate)
I do not understand what the author of the solution meant to say there.
Well, this reminds me of BFS algorithms(Breadth first search), where you radiate out from a single point to find multiple solutions w/ different permutations.
Here you can use recursion, and set the base case as when N perplexes have been reached in that 1 call stack of the recursive function.
So you can say:
function(int N <-- perplexes, ArrayList<Double> currentNumbers, double dividedNum)
if N == 0, then you're done - enter the currentNumbers array into a hashtable
clone the currentNumbers ArrayList as cloneNumbers
remove dividedNum from cloneNumbers and add 2 dividedNum/2
iterate through index of cloneNumbers
for every number x in cloneNumbers, call function(N--, cloneNumbers, x)
This is a rough, very inefficient but short way to do it. There's obviously a lot of ways you can prune the algorithm(reduce the amount of duplicates going into the hashtable, prevent cloning as much as possible, etc), but because this shows the absolute permutation of every number, and then enters that sequence into a hashtable, the hashtable will use its equals() comparison to see that the sequence already exists(such as your last 2 zetas), and reject the duplicate. That way, you'll be left with the answer you want.
The efficiency of the current algorithm: O(|E|^(N)), where |E| is the absolute number of numbers you can have inside of the array at the end of all insertions, and N is the number of insertions(or as you said, # of perplexes). Obviously this isn't the most optimal speed, but it does definitely work.
Hope this helps!

How many combinations are there to put n chapters into k(<n)volumes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Also, how can algorithmicly generate them within polynomial of n? Pseudo-code is ok.
This problem comes down to selecting k-1 borders, which divide n elements in k parts. As such, k-1 positions need to be selected out of n+k-1 positions, resulting in
possibilities. As an example, let's say we need to divide four sweets among three children. In this case, a first possibility would be to put the two borders on the first two positions, leaving the four sweets in positions 3-6:
As such, the first two children get no candy, while the third child gets all four. Another possibility would be to put the first border at position 2 and the second border at position 4:
Now the first two children get one candy each (positions 1 and 3), while the third child gets two (positions 5-6). Iterating over all positions results in a total of 15 possibilities.
Note that the answer is different when all volumes need to contain at least one chapter. In this case, k items are already given to one volume each, leaving n-k chapters. As such, there are
possibilities. Note that the condition k<n is important in this case.

Which algorithm is fastest in walking in binary tree to any given node at a any level of tree [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
for example:-there are computers connected in a binary tree structure. There are 20 rooms consider each room as a level in the binary tree starting from room 0 (level 0) in which the main computer is placed and all its children nodes (computers) are in room 1 (level 1) and so on. Each computer is numbered according to its place in the room like computer1 , computer 2, computer3 and computer4 (maximum number of computers in room2 is equals to the 2 raised to the power 2)in room 1 (level 1).Now starting from the root if I want to get to the computer756 in room 12 what is the fastest algorithm or method.
considering the above image as an example level 4 has total 16 nodes (1 to 16 say)that is 2 raised to the power 4. If the tree is huge (say tree with 50 levels) which is the fastest algorithm to access the node numbered 1,099,511,628,800 at level 50
If I understand your question correctly, given N and L you want to find a path from the root to the Nth node in the Lth level. I'll assume that the nodes in each level are numbered left-to-right, and that N and L are zero-based.
This is simple if you use the binary representation of N: Represent N as a binary number of L bits. Now go through these bits from most significant to least significant. A 0 means that you need to go to the left child, and a 1 means that you need to go the the right child.
For example, to find node #3 in level #3: The node number in binary is 011. So from the root, you go left, right, right.

What algorithm can be used here [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
This is an interview question, not a homework.
N friends are playing a game. Each of them has a list of numbers in front of himself.
Each of N friends chooses a number from his list and reports it to the game administrator. Then the game administrator sorts the reported numbers and shouts the K-th largest number.
I must the count all possible numbers that the game administrator can shout.
For example, if N = 3 and K = 3, and the lists for the 3 friends are 2 5 3, 8 1 6, 7 4 9. The output is 6, since the possible values are 4 5 6 7 8 9.
Can anybody suggest a decent algorithm for this problem? What I am doing is to create all possible permutations taking one number from each list, then sorting the resultant and printing the kth-largest. But that takes too much time.
To know if a number is present in the result or not, you need to know for each other list if there are numbers above and if there are numbers below. List where there are both numbers above and below are not a problem as you can choose a number in them as it suits you. The problem are lists where there are only numbers above or only numbers below. The first ones must be at most N-K, the second ones must be at most K. If this is not true, your number cannot be picked. If this is true, you can always choose numbers in the lists where there are both number above and below so that your number is picked.
This can be checked in linear time, or even better if your first sort your lists, thus giving an overall complexity of O(n.log(n)) where n is the number of numbers in total. Without sorting you got a n² complexity.
In your example with lists :
{2 5 3}, {8 1 6}, {7 4 9}
say we are looking for the 2-greatest number. For each number we ask if it can be shout by the administator. For each of them we look if in the other list there are both numbers below and numbers above. Let's look further for some of them
For 5 : there is numbers above and below in both other lists. So "5" can be shout by the administrator.
For "2" : there is numbers above and below in the second list so I can freely choose a number above or below in this list. But there are only numbers above in the third list, so the picked number in this list will always be greater. Since i can freely choose a number below in the second list, thus making my "2" the 2nd greatest number.
For "1" : there is only numbers above in the second list, so "1" will always be the smallest element.
For "9" : this is the other way round, it is always the greatest.
take the smallest number from each set. find the K-th -largest of these. This is the smallest number that is in the result. Similarly, find the largest number in the result. Prove that Each number between the two is in the result.

Resources