oneby7

2-D Dynamic Programming Bitmask DP

599. Minimum Number of Work Sessions to Finish the Tasks

Medium

You have n tasks, and task i takes tasks[i] units of time. You work in sessions. One session lasts at most sessionTime units, and you take a break between sessions.

A task cannot be split: once you start it, you finish it in the same session. You may do the tasks in any order, and a session may hold any number of tasks, as long as their times add up to at most sessionTime.

Return the smallest number of sessions needed to finish every task.

Example 1

Input: tasks = [3, 8, 2, 5, 4, 6], sessionTime = 10

Output: 3

Explanation: One way is the sessions [8, 2], [6, 4] and [3, 5]. Two sessions are not enough: they hold at most 20 units, and the tasks add up to 28.

Example 2

Input: tasks = [9, 9, 9], sessionTime = 9

Output: 3

Explanation: Each task fills a whole session, so every task needs a session of its own.

Example 3

Input: tasks = [1, 2, 3, 4], sessionTime = 1000000000

Output: 1

Explanation: All four tasks fit in one session.

Check your understanding

Work out the answer for this input before you write any code.

tasks = [3, 9, 3, 3, 9, 3], sessionTime = 10

Constraints

  • n = tasks.length
  • 1 ≤ n ≤ 18
  • 1 ≤ sessionTime ≤ 109
  • 1 ≤ tasks[i] ≤ sessionTime

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.