I have following problem statement
A lazy employee works at a multi-national corporation with N offices spread across the globe.
They want to work as few days over k weeks as possible.
Their company will pay for them to fly up to once per week (on weekends) to a remote office with a direct flight from their current office. Once in an office, they must stay there until the end of the week.
If an office observes a holiday while the employee is working there, they get that day off.
Find the path that maximizes the employee's vacation over k weeks.
Assume they start at a home office H on a weekend (so in the first week, they can be in any office connected to the home office).
For example:
H<--->A (It means there is a flight path from H to A and vice versa)
H<--->B
Here is my solution in Python.
vacation = [
{"H": 1, "A": 0, "B": 2}, # week 1
{"H": 0, "A": 2, "B": 0}
]
flights = [("H", "A"), ("H", "B")]
def get_optimal_path():
all_possible_paths = []
for offices in list(itertools.product(*vacation)):
total_cost = 0
week_level = 0
for office in offices:
vac_level = vacation[week_level]
total_cost += vac_level[office]
week_level += 1
if checkIfValidPath(offices[0], offices[1]):
all_possible_paths.append((offices, total_cost))
print(max(all_possible_paths, key=lambda x: x[1]))
def checkIfValidPath(source, destination):
return any(i for i in flights if i[0] == source and i[1] == destination
or i[0] == destination and i[1] == source)
get_optimal_path()
In this example best answer is for the employee to stay at office H in the first week and then move to office A in the second week so the total vacation days will be (1+2)=3. B and A cannot be the answer here because there is no path between them.
How can I optimize this code to increase its efficiency? Right now its complexity is nk (since we have k weeks).