Find go-live hours that meet every gap rule

Agent buildHard40 min

Your task

A new version of your support agent goes live one customer group at a time. The on-call team sets gap rules between groups. A soak rule leaves one group time to show problems before another goes live. A coverage rule keeps two go-lives close together, because the support team booked for both is only around for so long.

Write go_live(n, rules) that returns the earliest hour each group can go live.

Input and output:

  • n: the number of groups, 1 to 300. Groups are numbered 0 to n - 1.
  • rules: 0 to 3,000 rules, each [a, b, kind, h]. a and b are two different groups, kind is "after" or "within", and h is a whole number of hours from 0 to 1,000,000. The same pair may appear in several rules.
  • Return a list of n whole numbers, the go-live hour of each group in order. Return an empty list when no plan meets every rule.

Rules:

  • "after": group b goes live at least h hours after group a.
  • "within": group b goes live at most h hours after group a. This rule never stops b from going live before a.
  • Every group goes live at hour 0 or later. Several groups may go live in the same hour.
  • Return each group's earliest hour over all plans that meet every rule. Those hours, taken together, also meet every rule.

Examples

go_live(n: int, rules: list[list]) -> list[int]
Example 1
Inputgo_live(3, [[0, 1, "after", 4], [1, 2, "after", 2]])
Output[0, 4, 6]
Example 2
Inputgo_live(3, [[0, 1, "after", 10], [2, 1, "within", 3]])
Output[0, 10, 7]
Example 3
Inputgo_live(2, [[0, 1, "after", 5], [0, 1, "within", 3]])
Output[]