A certain bathroom has N + 2 stalls in a single row; the stalls on the left and right ends are permanently occupied by the bathroom guards. The other N stalls are for users.
When picking a stall, instead of considering two pieces, Rs, Ls, of state, all you need to know is the size of groups of free stalls. You always pick the middle stall of the largest group of sequential free stalls (in case of ties, you pick the leftmost group, in case the size is even, you pick the half-left stall).
Order doesn't matter - the problem doesn't ask for which stall the Kth person is going to pick, it asks indirectly for the size of the group (given the size of the group you can easily compute max(Ls, Rs) and min(Ls, Rs).
You can group stall groups by size - so this is a little trickier, but you can consider the choices of stalls as a binary tree (the solution above seemed to be going in that direction). Each choice corresponds to a group of stalls. At each level of the binary tree, you'll have a lot of repeated group sizes. In particular, each level of the binary tree has at most 2 different group sizes (I'll leave this as an exercise for you to prove). So, instead of splitting each group one at a time, since order doesn't matter, we can split all groups of the same time in one step, and just add groups for the next level.
At the end you get a nice concise O(log(K)) runtime and O(1) space solution.
Here's an example of how this works by manually solving N=1000 and K=100:
You start with a group of size 1000 for free stalls, so your group size -> number of groups maps looks like this:
Step 1:
1000 -> 1
k = 100
You split 1000 into two groups, one of 499, and one of size 500, and this represents the choice of 1 person, since there is 1 group of size 1000. So now our state looks like:
Step 2:
500 -> 1
499 -> 1
k = 99
Now you split 500 into 249 and 250:
Step 3:
499 -> 1
249 -> 1
250 -> 1
k = 98
Now I'll just draw the next steps, without explaining:
Now things get special, because there are 41 groups of 15 free stalls, but only 36 people left to choose a stall. Therefore, the last person will choose the middle stall of a group of 15 free stalls, and thus Ls = 7 and Rs = 7!
Ah -- I must have introduced an issue while refactoring -- it passed the tests during the competition. But then I refactored, should have checked again!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Friday
Bathroom Stalls (Code Jam):
A certain bathroom has N + 2 stalls in a single row; the stalls on the left and right ends are permanently occupied by the bathroom guards. The other N stalls are for users.
link
Python solution!
So I was curious since this solution didn't seem correct, and tried to run this through the codejam grader, and it seems like it isn't correct.
I had some fun with this problem, though (it entertained some part of a plane ride), and came up with a nice concise solution:
A few insights here:
At the end you get a nice concise O(log(K)) runtime and O(1) space solution.
Here's an example of how this works by manually solving N=1000 and K=100:
You start with a group of size 1000 for free stalls, so your group size -> number of groups maps looks like this:
You split 1000 into two groups, one of 499, and one of size 500, and this represents the choice of 1 person, since there is 1 group of size 1000. So now our state looks like:
Now you split 500 into 249 and 250:
Now I'll just draw the next steps, without explaining:
Now things get special, because there are 41 groups of 15 free stalls, but only 36 people left to choose a stall. Therefore, the last person will choose the middle stall of a group of 15 free stalls, and thus Ls = 7 and Rs = 7!
Ah -- I must have introduced an issue while refactoring -- it passed the tests during the competition. But then I refactored, should have checked again!