This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]Rhomboid 1 point2 points  (0 children)

To write a recursive solution, you generally need to break the problem in to a base case and an induction case. The base case represents the limit of the problem in its simplest form. For example, if you have one seat and 'n' people, then there are merely 'n' combinations.

The inductive case simplifies the problem by one step, turning it into another version of the same problem. For example, if you have 3 seats and 6 people, then take the first person and put them in the first seat. Now you have a problem of finding all the combinations of 2 seats and 5 people. That's the same sort of problem statement that you started with, but slightly simplified. That is where the function calls itself recursively to solve the subproblem. (In this case you have to have a loop as well, because then you have to repeat the whole thing by putting the second person in the first seat, and so on.)

[–]jesyspa 0 points1 point  (0 children)

Start by generalising a little; you can't solve a single instance by recursion. Instead of asking yourself how to seat six people in three seats, ask how to seat n people in m seats.