16. Minimum Number of Workers
β GFG solution to the Minimum Number of Workers problem using greedy interval coverage. Efficiently determine the minimum workers needed to cover the entire day. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an array arr[], where arr[i] denotes the range of working hours a person at position i can cover.
If
arr[i] β -1, the person at indexican work and cover the time interval[i - arr[i], i + arr[i]].If
arr[i] = -1, the person is unavailable and cannot cover any time.
The task is to find the minimum number of people required to cover the entire working day from 0 to n - 1. If it is not possible to fully cover the day, return -1.
π Examples
Example 1
Input: arr[] = [1, 2, 1, 0]
Output: 1
Explanation: The person at index 1 can cover the interval [-1, 3]. After adjusting to valid
bounds, this becomes [0, 3], which fully covers the entire working day 0 to n-1. Therefore,
only 1 person is required to cover the whole day.Example 2
Input: arr[] = [2, 3, 4, -1, 2, 0, 0, -1, 0]
Output: -1
Explanation: Persons up to index 2 cover interval [0β¦6], but working hour 7 cannot be covered
as arr[7] = -1. Since the 7th hour cannot be covered by any person, it is impossible to cover
the full working day.Example 3
π Constraints
$1 \le \text{arr.size()} \le 10^5$
$-1 \le \text{arr}[i] \le \text{arr.size()}$
β
My Approach
This problem is a classic Interval Coverage Problem that can be optimally solved using a Greedy Algorithm combined with Interval Merging:
Greedy Interval Selection
Generate Coverage Intervals:
For each valid worker (where
arr[i] β -1), calculate their coverage range:[max(0, i - arr[i]), min(n-1, i + arr[i])].Store these intervals as pairs of
(start, end)positions.
Sort Intervals:
Sort all intervals by their starting position to process them in order.
This allows us to greedily select workers that extend coverage as far as possible.
Greedy Selection:
Initialize
pos = -1(representing the last covered position) andcnt = 0(worker count).While
pos < n - 1:Find all workers whose intervals start at or before
pos + 1(they can extend current coverage).Among these workers, select the one that extends coverage the farthest.
If no such worker exists, return
-1(impossible to cover).Increment worker count and update
posto the new farthest reachable position.
Validation:
If we successfully reach
pos >= n - 1, return the worker count.Otherwise, return
-1.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n), where n is the size of the array. The sorting of intervals dominates the time complexity, while the greedy selection pass takes O(n) time as each interval is processed at most once.
Expected Auxiliary Space Complexity: O(n), as we store at most n intervals in the vector for workers who can cover some range (arr[i] β -1).
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated