Minimum Number of Work Sessions to Finish the Tasks - Problem
You're a productivity consultant helping someone optimize their work schedule! ๐
Given n tasks with specific durations and a maximum session time, you need to find the minimum number of work sessions required to complete all tasks.
Key Rules:
- ๐ฏ Each task must be completed within a single work session (no splitting)
- โฑ๏ธ Each work session can last at most
sessionTimehours - ๐ You can complete tasks in any order
- โ Tasks can be done back-to-back within the same session
Goal: Return the minimum number of work sessions needed to finish all tasks.
Example: If you have tasks [1,2,3] and sessionTime = 3, you could do [3] in session 1 and [1,2] in session 2, requiring just 2 sessions.
Input & Output
Basic Example
$
Input:
tasks = [1,2,3], sessionTime = 3
โบ
Output:
2
๐ก Note:
We can group tasks as [3] and [1,2]. Task 3 takes one full session, while tasks 1 and 2 can fit together in another session (1+2=3 โค 3).
All Tasks in One Session
$
Input:
tasks = [2,1,3], sessionTime = 6
โบ
Output:
1
๐ก Note:
All tasks can fit in a single session since 2+1+3 = 6 โค 6. Order doesn't matter: we could do [2,1,3] or any other arrangement.
Each Task Needs Its Own Session
$
Input:
tasks = [3,4,3], sessionTime = 4
โบ
Output:
3
๐ก Note:
Tasks [3,4,3] with sessionTime=4 means: task 1 (3โค4) โ, task 2 (4โค4) โ, task 3 (3โค4) โ, but no two tasks can fit together (3+4>4, 3+3>4, 4+3>4). Each needs its own session.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Valid Groups
Find all combinations of tasks that can fit in one session
2
Build DP Table
For each subset of tasks, calculate minimum sessions needed
3
Combine Optimally
Use previously computed optimal solutions to build larger ones
4
Extract Answer
The DP entry for all tasks gives us the minimum sessions
Key Takeaway
๐ฏ Key Insight: By representing task subsets as bitmasks and using dynamic programming, we can efficiently explore all possible groupings while avoiding redundant calculations, leading to the optimal minimum number of work sessions.
Time & Space Complexity
Time Complexity
O(3^n)
For each of 2^n subsets, we check all its subsets, leading to sum of C(n,k)*2^k = 3^n
โ Linear Growth
Space Complexity
O(2^n)
DP array stores minimum sessions needed for each possible subset of tasks
โ Quadratic Space
Constraints
- 1 โค n โค 14 (small enough for bitmask DP)
- 1 โค tasks[i] โค 10
- max(tasks[i]) โค sessionTime โค 15
- All tasks are guaranteed to fit within a single session
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code