I think you need to do this recursively, but you need to make sure that you will not share the same group again and again. This will give you exponential runtime. In my solution, it looks like I have O (n * n) (you can check it out for me;), see Results below. Another thing is the desirability function that you mentioned. I do not know how such a function might look, but instead you can compare 2 sections. for example, the section 1 + 1 + 2 + 4 is less desirable, then 1 + 2 + 2 + 3, because it has two “units”. A general rule may be that “a section is less desirable if it has more people grouped than another section.” It makes sense the more people sit together, the better. My solution uses this approach to compare two possible groupings, and I get the result you wanted to achieve. Let me show you some results first, and then the code.
var sut = new BrainTeaser(); for (int n = 1; n <= 6; n++) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("{0} person{1}: ", n, n > 1 ? "s" : ""); var array = sut.Solve(n).Select(x => x.ToString()).ToArray(); sb.AppendLine(string.Join(", ", array)); Console.WriteLine(sb.ToString()); }
1 person: 1
2 people: 2, 1 + 1
3 people: 3, 1 + 2, 1 + 1 + 1
4 people: 4, 2 + 2, 1 + 3, 1 + 1 + 2, 1 + 1 + 1 + 1
5 people: 5, 2 + 3, 1 + 4, 1 + 2 + 2, 1 + 1 + 3, 1 + 1 + 1 + 2, 1 + 1 + 1 + 1 + 1
6 people: 6, 3 + 3, 2 + 4, 2 + 2 + 2, 1 + 5, 1 + 2 + 3, 1 + 1 + 4, 1 + 1 + 2 + 2, 1 + 1 + 1 + 3 , 1 + 1 + 1 + 1 + 2, 1 + 1 + 1 + 1 + 1 + 1
performance looks like O (n * n):
var sut = new BrainTeaser(); for (int n = 1; n <= 40; n++) { Stopwatch watch = new Stopwatch(); watch.Start(); var count = sut.Solve(n).Count(); watch.Stop(); Console.WriteLine("Problem solved for {0} friends in {1} ms. Number of solutions {2}", n, watch.ElapsedMilliseconds, count); } Problem solved for 1 friends in 17 ms. Number of solutions 1 Problem solved for 2 friends in 49 ms. Number of solutions 2 Problem solved for 3 friends in 2 ms. Number of solutions 3 Problem solved for 4 friends in 1 ms. Number of solutions 5 Problem solved for 5 friends in 0 ms. Number of solutions 7 Problem solved for 6 friends in 2 ms. Number of solutions 11 Problem solved for 7 friends in 0 ms. Number of solutions 15 Problem solved for 8 friends in 0 ms. Number of solutions 22 Problem solved for 9 friends in 1 ms. Number of solutions 30 Problem solved for 10 friends in 1 ms. Number of solutions 42 Problem solved for 11 friends in 4 ms. Number of solutions 56 Problem solved for 12 friends in 4 ms. Number of solutions 77 Problem solved for 13 friends in 7 ms. Number of solutions 101 Problem solved for 14 friends in 9 ms. Number of solutions 135 Problem solved for 15 friends in 15 ms. Number of solutions 176 Problem solved for 16 friends in 21 ms. Number of solutions 231 Problem solved for 17 friends in 30 ms. Number of solutions 297 Problem solved for 18 friends in 43 ms. Number of solutions 385 Problem solved for 19 friends in 61 ms. Number of solutions 490 Problem solved for 20 friends in 85 ms. Number of solutions 627 Problem solved for 21 friends in 117 ms. Number of solutions 792 Problem solved for 22 friends in 164 ms. Number of solutions 1002 Problem solved for 23 friends in 219 ms. Number of solutions 1255 Problem solved for 24 friends in 300 ms. Number of solutions 1575 Problem solved for 25 friends in 386 ms. Number of solutions 1958 Problem solved for 26 friends in 519 ms. Number of solutions 2436 Problem solved for 27 friends in 677 ms. Number of solutions 3010 Problem solved for 28 friends in 895 ms. Number of solutions 3718 Problem solved for 29 friends in 1168 ms. Number of solutions 4565 Problem solved for 30 friends in 1545 ms. Number of solutions 5604 Problem solved for 31 friends in 2025 ms. Number of solutions 6842 Problem solved for 32 friends in 2577 ms. Number of solutions 8349 Problem solved for 33 friends in 3227 ms. Number of solutions 10143 Problem solved for 34 friends in 4137 ms. Number of solutions 12310 Problem solved for 35 friends in 5300 ms. Number of solutions 14883 Problem solved for 36 friends in 6429 ms. Number of solutions 17977 Problem solved for 37 friends in 8190 ms. Number of solutions 21637 Problem solved for 38 friends in 10162 ms. Number of solutions 26015 Problem solved for 39 friends in 12643 ms. Number of solutions 31185
Let me post 3 classes involved in the solution:
public class BrainTeaser {
Now to the solution:
The brainteaser class performs recursion. One trick in this class is to use a custom mapper ( PossibleGroupingComparer ) in a hashset. This will make sure that when calculating new groupings (for example, 1 + 1 + 2 versus 2 + 1 + 1) they will be considered the same (our set will contain only one representative for each equivalent group). This should reduce exponential runtime to O (n ^ 2).
The next trick is that ordering the result is possible because our PossibleGroupings class implements IComparable. The implementation of the Compare () method uses the above idea. This method essentially contains salt in this solution, and if you want it to be grouped in different ways, you only need to modify this method.
I hope you understand the code, otherwise let me know. I tried to make it readable and didn't really care about performance. For example, you can order groupings only before you return them to the caller; ordering inside recursions will not bring much.
One comment: a typical scenario may be that the cinema has already "reserved" many seats and will not allow "any" partitions. Here you need to get all the sections, and then check one by one, if possible for the current movie theater. It works, but it costs an extra processor. Instead, we could use input to reduce the number of recursions and improve the overall execution time. Maybe someone wants to post a solution for this;)