#1986

Minimum Number of Work Sessions to Finish the Tasks

expert · 1070 · lc medium +32 · verified · 34.6% accepted · 1,185 likes · top 12%

Description

You have n tasks with durations given in the array tasks, where tasks[i] is the number of hours the i-th task takes. Each work session lasts at most sessionTime consecutive hours, after which you must stop.

Rules: once a task starts it must finish in the same session; tasks can be done in any order; the next task may begin immediately after the previous ends. It is guaranteed that sessionTime is at least as large as the longest single task.

Return the minimum number of sessions required to complete all tasks.

Example 1:

Input: tasks = [1,2,3], sessionTime = 3
Output: 2
Explanation: You can finish the tasks in two work sessions.
- First work session: finish the first and the second tasks in 1 + 2 = 3 hours.
- Second work session: finish the third task in 3 hours.

Example 2:

Input: tasks = [3,1,3,1,1], sessionTime = 8
Output: 2
Explanation: You can finish the tasks in two work sessions.
- First work session: finish all the tasks except the last one in 3 + 1 + 3 + 1 = 8 hours.
- Second work session: finish the last task in 1 hour.

Example 3:

Input: tasks = [1,2,3,4,5], sessionTime = 15
Output: 1
Explanation: You can finish all the tasks in one work session.

Code

1
2
3