← Questions

Days needed to finish recurring tasks

0
AmazonCoding

You run n recurring tasks. Task i runs for the first time on day 1 and then again every intervals[i] days, so it runs on days 1, 1 + intervals[i], 1 + 2 * intervals[i], .... Any number of task runs can happen on the same day.

Given intervals and a target goal, return the smallest day d such that the total number of task runs on days 1 through d is at least goal.

def min_days(intervals: list[int], goal: int) -> int: ...

Constraints

  • 1 <= len(intervals) <= 10^5
  • 1 <= intervals[i] <= 10^5
  • 1 <= goal <= 10^9

Example 1

intervals = [3, 5], goal = 5  ->  7

The 3-day task runs on days 1, 4, 7. The 5-day task runs on days 1, 6. By day 6 there are 4 runs; day 7 brings the total to 5.

Example 2

intervals = [2, 2, 2], goal = 7  ->  5

All three tasks run on days 1, 3, 5. That is 3 runs by day 1, 6 by day 3, and 9 by day 5, so day 5 is the first day with at least 7.

Reconstructed from the reported question; details may differ from what interviewers ask.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding