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 sessionTime hours
  • ๐Ÿ”„ 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
Session Optimization ProcessTasks: [1,2,3,4]Session Limit: 51234Valid Single Sessionsโœ“ [1] โ†’ 1 โ‰ค 5โœ“ [2] โ†’ 2 โ‰ค 5โœ“ [3] โ†’ 3 โ‰ค 5โœ“ [4] โ†’ 4 โ‰ค 5โœ“ [1,2] โ†’ 3 โ‰ค 5โœ“ [1,3] โ†’ 4 โ‰ค 5โœ“ [1,4] โ†’ 5 โ‰ค 5โœ“ [2,3] โ†’ 5 โ‰ค 5โœ— [2,4] โ†’ 6 > 5DP Table Constructiondp[0000]=0dp[0001]=1dp[0010]=1dp[0011]=1...dp[1111]=?Optimal SolutionSession 1: [1,4]Session 2: [2,3]Minimum Sessions: 2Key Insight๐Ÿ’ก DP with bitmasks allows us to:โ€ข Represent any subset of tasks as a numberโ€ข Avoid recomputing the same subproblemsโ€ข Guarantee optimal solution in O(3^n) time
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

n
2n
โœ“ Linear Growth
Space Complexity
O(2^n)

DP array stores minimum sessions needed for each possible subset of tasks

n
2n
โš  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
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 22
34.2K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen