Ctrl K

Inspector Visit Schedule

Medium
A construction site has n sections numbered from 1. Section j still needs work[j - 1] units of work, and every section has to be signed off by an inspector. The inspector follows the schedule visits: at second s (from 1 to m) they stand at section visits[s - 1]. Every second, the crew does exactly one of three things: - does one unit of work on any section; - gets the section where the inspector stands signed off, provided it has no work left; - does nothing. Return the earliest second by the end of which every section can be signed off, or -1 if it cannot be done within m seconds.
Example 1
work=[1, 0], visits=[2, 1, 2, 1] → 3

Second 1: work on site 1, finishing it. Second 2: the inspector is at site 1 and signs it off. Second 3: the inspector is at site 2, which needed no work, and signs it off. The answer is 3.

Example 2
work=[2, 1], visits=[1, 2, 2, 1, 1] → 5

There are 3 seconds of work and 2 sign-offs. Site 2 can be signed off at second 3 and site 1 at second 5, so everything fits in exactly 5 seconds.

Example 3
work=[0, 0, 1], visits=[1, 2, 1, 2] → -1

The inspector never visits site 3, so not everything can be signed off.

Constraints
  • 1 ≤ n = len(work) ≤ 10⁵
  • 0 ≤ work[i] ≤ 10⁹
  • 1 ≤ m = len(visits) ≤ 10⁵
  • 1 ≤ visits[i] ≤ n
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFind in a Shifted Sorted Array MediumFirst and Last Copy MediumFind in a Row-Chained Grid MediumFind in a Shifted Array With Repeats MediumMinimum of a Shifted List Medium
loading Python…
Input
work=[1, 0], visits=[2, 1, 2, 1]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 116 tests on the server, hidden ones included.