githubEdit

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 Linkarrow-up-right

🧩 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 index i can 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

  1. 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.

  2. 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.

  3. Greedy Selection:

    • Initialize pos = -1 (representing the last covered position) and cnt = 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 pos to the new farthest reachable position.

  4. 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++)

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Greedy with Early Termination

πŸ’‘ Algorithm Steps:

  1. Create intervals for each worker based on their coverage range.

  2. Sort intervals by start position for efficient processing.

  3. Use greedy selection to pick workers with maximum coverage extension.

  4. Track covered positions and select minimal workers needed.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n log n) - Sorting dominates the time complexity

  • Auxiliary Space: πŸ’Ύ O(n) - Storage for intervals array

βœ… Why This Approach?

  • Early termination on impossible coverage scenarios

  • Clearer boundary handling with explicit range clamping

  • Enhanced sorting comparator for optimal interval selection

πŸ“Š 3️⃣ Stack-Based Interval Selection

πŸ’‘ Algorithm Steps:

  1. Generate coverage intervals for all available workers.

  2. Sort intervals and use stack to track coverage progression.

  3. Select workers that extend coverage beyond current maximum.

  4. Validate complete coverage from position 0 to n-1.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n log n) - Sorting is the bottleneck operation

  • Auxiliary Space: πŸ’Ύ O(n) - Space for worker interval storage

βœ… Why This Approach?

  • Additional validation at the end for complete coverage

  • Clear progression tracking with extend variable

  • Explicit check for coverage improvement

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

🎯 Basic Greedy

🟒 O(n log n)

🟒 O(n)

πŸš€ Simple and efficient

πŸ”§ Minimal error checking

πŸ›‘οΈ Early Termination

🟒 O(n log n)

🟒 O(n)

βœ… Robust error handling

πŸ“Š Slightly more complex

πŸ“š Stack-Based

🟒 O(n log n)

🟒 O(n)

⭐ Comprehensive validation

🐌 Additional final check

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Competitive programming

πŸ₯‡ Basic Greedy

β˜…β˜…β˜…β˜…β˜…

πŸ“– Production code

πŸ₯ˆ Early Termination

β˜…β˜…β˜…β˜…β˜…

🎯 High reliability needed

πŸ… Stack-Based

β˜…β˜…β˜…β˜…β˜…

β˜• Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated