This issue is from CodeEval 118 Problem
Your team is moving to a new office. To make them feel comfortable in the new place, you decide to let them choose the places they want. Each team member has provided you with a list of places that he / she will find acceptable. Your goal is to determine if everyone can be satisfied using these lists.
The seats in the new office are numbered from 1 to N. And the list of seat preferences that each team member provided you is unsorted.
Example input and output:
1:[1, 3, 2], 2:[1], 3:[4, 3], 4:[4, 3] --> Yes # possible 1:[1, 3, 2], 2:[1], 3:[1] --> No # not possible
How to solve it?
What have i tried? I believe that the solution will be recursive, and thatโs what I came up with, but I donโt think I am breaking the problem down correctly into its smaller subtasks.
def seat_team(num_seats, preferences, assigned): if len(preferences) == 1: for seat in range(len(preferences)): print preferences seat_wanted = preferences[0][1][seat] if not assigned[seat_wanted-1]: assigned[seat_wanted-1] = True return True, assigned return False, assigned else: for i in range(len(preferences)): current = preferences[i] for seat in current[1]: found, assigned = seat_team(num_seats, [preferences[i]], assigned) if not found: return False found, assigned = seat_team(num_seats, preferences[i+1:], assigned) if not found: return False return True num_seats = 4 preferences = [(1,[1,3,2]), (2,[1]), (3,[4,3]), (4,[4,3])] assigned = [False] * num_seats print seat_team(4, preferences, assigned)
Any ideas? I am sure that there is a common name for such problems and an algorithm for solving it, but I could not find similar problems (or solutions) on the Internet. Please share examples, if you know about them, I would really appreciate it.