FAANG | OA | Work Scheduler
You are given the schedule of a worker for a week. The working hours for some days of the week are provided, while the working hours for the other days of the week are blank. You are given the target working hours for the week, and the maximum allowed work hours per day.
Generate all possible permutations of valid work hours for the unscheduled days, fill the permutations in the pattern, and return the list of all permutations in lexicographic order.
Ex1: Pattern for the week is: pattern = "426?5?5" workHours = 26 dayHours = 4
Here, the pattern "426?5?5" represents the schedule for the week. The digit at a position indicates the working hours for that day, while the "?" indicates the days that haven't been scheduled yet. workHours is the target working hours for the week, while the dayHours is the maximum hours that can be scheduled for the blank days.
The remaining work hours for this week will be 26 - (4+2+6+5+5) = 4.
So, the 2 unscheduled days can be scheduled as (0,4), (1,3), (2,2), (3,1), (4,0) in lexicographic order.
Hence, the answer would be ["4260545", "4261535", "4262525", "4263515", "4264505"]
Ex2: pattern = "???8???", workHours = 56, dayHours = 8
Here the answer would be ["8888888"]
My code (best I could):
def workScheduler(pattern, workHours, dayHours):
unscheduledDays = 0
remainingWorkHours = workHours
for day in pattern:
if day == "?":
unscheduledDays += 1
else:
remainingWorkHours -= int(day)
def getFilledPattern(combo):
p = 0
filledPattern = ""
for day in pattern:
if day == "?":
filledPattern += str(combo[p])
p += 1
else:
filledPattern += day
return filledPattern
def getScheduleCombinations(curCombo, remainingHours):
#check if this combo is valid
if remainingHours == 0 and len(curCombo) == unscheduledDays:
result.append(getFilledPattern(curCombo))
return
#if remainingHours is -ve or remainingHours > 0 and required combo length is achieved and no more can be added, this is invalid
elif remainingHours < 0 or len(curCombo) == unscheduledDays:
return
for i in range(0, dayHours+1):
curCombo.append(i)
getScheduleCombinations(curCombo, remainingHours - i)
curCombo.pop()
result = []
getScheduleCombinations([], remainingWorkHours)
return result