Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
There are n tasks assigned to you. The task times are represented as an integer array tasks of length n, where the ith 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:
Given tasks and sessionTime, return the minimum number of work sessions needed to finish all the tasks following the conditions above.
The tests are generated such that sessionTime is greater than or equal to the maximum element in tasks[i].
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.
Constraints:
n == tasks.length1 <= n <= 141 <= tasks[i] <= 10max(tasks[i]) <= sessionTime <= 15Problem summary: There are n tasks assigned to you. The task times are represented as an integer array tasks of length n, where the ith 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 may complete the tasks in any order. Given tasks and sessionTime, return the minimum number of work sessions needed to finish all the tasks following the conditions above. The tests are generated such that sessionTime is greater than or equal to the maximum element in tasks[i].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Backtracking · Bit Manipulation
[1,2,3] 3
[3,1,3,1,1] 8
[1,2,3,4,5] 15
smallest-sufficient-team)fair-distribution-of-cookies)find-minimum-time-to-finish-all-jobs)find-minimum-time-to-finish-all-jobs-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1986: Minimum Number of Work Sessions to Finish the Tasks
class Solution {
public int minSessions(int[] tasks, int sessionTime) {
int n = tasks.length;
boolean[] ok = new boolean[1 << n];
for (int i = 1; i < 1 << n; ++i) {
int t = 0;
for (int j = 0; j < n; ++j) {
if ((i >> j & 1) == 1) {
t += tasks[j];
}
}
ok[i] = t <= sessionTime;
}
int[] f = new int[1 << n];
Arrays.fill(f, 1 << 30);
f[0] = 0;
for (int i = 1; i < 1 << n; ++i) {
for (int j = i; j > 0; j = (j - 1) & i) {
if (ok[j]) {
f[i] = Math.min(f[i], f[i ^ j] + 1);
}
}
}
return f[(1 << n) - 1];
}
}
// Accepted solution for LeetCode #1986: Minimum Number of Work Sessions to Finish the Tasks
func minSessions(tasks []int, sessionTime int) int {
n := len(tasks)
ok := make([]bool, 1<<n)
f := make([]int, 1<<n)
for i := 1; i < 1<<n; i++ {
t := 0
f[i] = 1 << 30
for j, x := range tasks {
if i>>j&1 == 1 {
t += x
}
}
ok[i] = t <= sessionTime
}
for i := 1; i < 1<<n; i++ {
for j := i; j > 0; j = (j - 1) & i {
if ok[j] {
f[i] = min(f[i], f[i^j]+1)
}
}
}
return f[1<<n-1]
}
# Accepted solution for LeetCode #1986: Minimum Number of Work Sessions to Finish the Tasks
class Solution:
def minSessions(self, tasks: List[int], sessionTime: int) -> int:
n = len(tasks)
ok = [False] * (1 << n)
for i in range(1, 1 << n):
t = sum(tasks[j] for j in range(n) if i >> j & 1)
ok[i] = t <= sessionTime
f = [inf] * (1 << n)
f[0] = 0
for i in range(1, 1 << n):
j = i
while j:
if ok[j]:
f[i] = min(f[i], f[i ^ j] + 1)
j = (j - 1) & i
return f[-1]
// Accepted solution for LeetCode #1986: Minimum Number of Work Sessions to Finish the Tasks
/**
* [1986] Minimum Number of Work Sessions to Finish the Tasks
*
* 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 may complete the tasks in any order.
*
* Given tasks and sessionTime, return the minimum number of work sessions needed to finish all the tasks following the conditions above.
* The tests are generated such that sessionTime is greater than or equal to the maximum element in tasks[i].
*
* 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.
*
*
* Constraints:
*
* n == tasks.length
* 1 <= n <= 14
* 1 <= tasks[i] <= 10
* max(tasks[i]) <= sessionTime <= 15
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/
// discuss: https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn min_sessions(tasks: Vec<i32>, session_time: i32) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1986_example_1() {
let tasks = vec![1, 2, 3];
let session_time = 3;
let result = 2;
assert_eq!(Solution::min_sessions(tasks, session_time), result);
}
#[test]
#[ignore]
fn test_1986_example_2() {
let tasks = vec![3, 1, 3, 1, 1];
let session_time = 8;
let result = 2;
assert_eq!(Solution::min_sessions(tasks, session_time), result);
}
#[test]
#[ignore]
fn test_1986_example_3() {
let tasks = vec![1, 2, 3, 4, 5];
let session_time = 15;
let result = 1;
assert_eq!(Solution::min_sessions(tasks, session_time), result);
}
}
// Accepted solution for LeetCode #1986: Minimum Number of Work Sessions to Finish the Tasks
function minSessions(tasks: number[], sessionTime: number): number {
const n = tasks.length;
const ok: boolean[] = new Array(1 << n).fill(false);
for (let i = 1; i < 1 << n; ++i) {
let t = 0;
for (let j = 0; j < n; ++j) {
if (((i >> j) & 1) === 1) {
t += tasks[j];
}
}
ok[i] = t <= sessionTime;
}
const f: number[] = new Array(1 << n).fill(1 << 30);
f[0] = 0;
for (let i = 1; i < 1 << n; ++i) {
for (let j = i; j > 0; j = (j - 1) & i) {
if (ok[j]) {
f[i] = Math.min(f[i], f[i ^ j] + 1);
}
}
}
return f[(1 << n) - 1];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.