Minimum Number of Work Sessions to Finish the Tasks - Complete Solution Guide
Minimum Number of Work Sessions to Finish the Tasks is LeetCode problem 1986, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
There are n tasks assigned to you. The task times are represented as an integer array tasks of length n , where the i th task takes tasks[i] hours to finish. A work session is when you work for at most sessionTime consecutive hours and then take a break. You should finish the given tasks in a way that satisfies the following conditions: If you start a task in a work session, you must complete it in the same work session. You can start a new task immediately after finishing the previous one. You
Detailed Explanation
The problem asks us to find the minimum number of work sessions required to complete a set of tasks, given a maximum time limit for each session. Each task must be completed within a single session, and tasks can be completed in any order. The input consists of an array `tasks` representing the time each task takes, and `sessionTime` which is the maximum time available in a work session. The constraints limit the number of tasks to a maximum of 14, implying that a bitmasking approach is feasible.
Solution Approach
The solution uses dynamic programming with bitmasking. A `dp` array is created where `dp[mask]` stores a tuple (or pair) representing the minimum number of sessions needed to complete the tasks represented by `mask`, and the remaining time in the last session. The algorithm iterates through all possible subsets of tasks (represented by bitmasks). For each subset, it considers each task within that subset. It tries to either add the task to the current session (if there is enough time) or start a new session with that task. The `dp` array is updated to store the best (minimum number of sessions) solution for each state. The algorithm leverages the constraints on the size of the input to create an efficient approach.
Step-by-Step Algorithm
- Step 1: Initialize a DP array `dp` of size `2^n`, where n is the number of tasks. Each element `dp[mask]` is a tuple (sessions, time_left), initialized to (n+1, sessionTime+1), representing an impossibly high number of sessions and time left. `dp[0]` is initialized to (0, 0), because no tasks done requires 0 sessions and 0 time spent.
- Step 2: Iterate through all possible masks from 1 to `2^n - 1`. Each mask represents a subset of tasks.
- Step 3: For each mask, iterate through all tasks from 0 to `n - 1`.
- Step 4: Check if the `i`-th task is present in the current mask (i.e., if `(mask >> i) & 1` is true).
- Step 5: If the `i`-th task is present, consider the previous state, `prev_mask = mask ^ (1 << i)`, which is the mask without the `i`-th task.
- Step 6: Retrieve the number of sessions and time left from `dp[prev_mask]`. If `dp[prev_mask].sessions == 0`, this is the initial state and thus start first session, new_sessions = 1, new_time = tasks[i]
- Step 7: If adding the current task to the previous session is possible (`dp[prev_mask].time_left + tasks[i] <= sessionTime`), then update the new session with the previous sessions and reduce the remaining time. new_sessions = prev_sessions, new_time = prev_time + tasks[i]
- Step 8: Otherwise, a new session needs to be started, so the current task will take the full amount of the current session. new_sessions = prev_sessions + 1, new_time = tasks[i]
- Step 9: Update `dp[mask]` with the minimum number of sessions and minimum remaining time.
- Step 10: After iterating through all masks, the minimum number of sessions required to complete all tasks is stored in `dp[(1 << n) - 1].sessions`.
Key Insights
- Insight 1: Since the number of tasks is small (<=14), we can use bitmasking to represent the state of completed tasks. Each bit in the mask corresponds to a task, with a '1' indicating that the task has been completed and a '0' indicating it hasn't.
- Insight 2: Dynamic programming can be used to efficiently determine the minimum number of sessions needed for each possible state (combination of completed tasks).
- Insight 3: The DP state represents the minimum number of sessions required to complete the tasks represented by the bitmask, along with the remaining time left in the current session.
Complexity Analysis
Time Complexity: O(n * 2^n)
Space Complexity: O(2^n)
Topics
This problem involves: Array, Dynamic Programming, Backtracking, Bit Manipulation, Bitmask.
Companies
Asked at: Swiggy.