🧠
Optimized Tracking with Hash Map Only (Greedy)
💡 This approach is the most optimal and clean, using a hash map to track the earliest day each task can be scheduled next, greedily scheduling tasks as soon as possible.
Intuition
For each task, schedule it on the maximum of the current day or the earliest day it can be scheduled (based on cooldown). Update the earliest next available day for that task accordingly.
Algorithm
- Initialize a hash map to store the earliest next available day for each task.
- Initialize current day to 1.
- For each task, update current day to max(current day, earliest next available day for that task).
- Schedule the task on current day.
- Update the earliest next available day for the task to current day + space + 1.
- Increment current day and continue until all tasks are scheduled.
- Return the last scheduled day.
💡 This greedy approach avoids explicit idle simulation by jumping the current day forward to the earliest allowed day for each task.
def task_scheduler_optimized(tasks, space):
next_available = {}
day = 1
for t in tasks:
if t in next_available:
day = max(day, next_available[t])
next_available[t] = day + space + 1
day += 1
return day - 1
# Driver code
if __name__ == '__main__':
tasks = [1,2,1,2,3,1]
space = 3
print(task_scheduler_optimized(tasks, space)) # Expected output: 9
Line Notes
next_available = {}Tracks earliest day each task can be scheduled next
day = 1Start day count at 1
for t in tasks:Iterate over each task in order
if t in next_available:Check if task has a cooldown constraint
day = max(day, next_available[t])Jump day forward if cooldown not satisfied
next_available[t] = day + space + 1Update next available day for task after scheduling
day += 1Increment day after scheduling
return day - 1Return the last day when all tasks are completed
import java.util.*;
public class TaskSchedulerOptimized {
public static int taskScheduler(int[] tasks, int space) {
Map<Integer, Integer> nextAvailable = new HashMap<>();
int day = 1;
for (int t : tasks) {
if (nextAvailable.containsKey(t)) {
day = Math.max(day, nextAvailable.get(t));
}
nextAvailable.put(t, day + space + 1);
day++;
}
return day - 1;
}
public static void main(String[] args) {
int[] tasks = {1,2,1,2,3,1};
int space = 3;
System.out.println(taskScheduler(tasks, space)); // Expected: 9
}
}
Line Notes
Map<Integer, Integer> nextAvailable = new HashMap<>();Tracks earliest next available day per task
int day = 1;Initialize day counter starting at 1
for (int t : tasks) {Iterate over each task in order
if (nextAvailable.containsKey(t)) {Check if task has cooldown
day = Math.max(day, nextAvailable.get(t));Jump day forward if cooldown not satisfied
nextAvailable.put(t, day + space + 1);Update next available day after scheduling
day++;Increment day after scheduling
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int taskSchedulerOptimized(const vector<int>& tasks, int space) {
unordered_map<int, int> nextAvailable;
int day = 1;
for (int t : tasks) {
if (nextAvailable.count(t)) {
day = max(day, nextAvailable[t]);
}
nextAvailable[t] = day + space + 1;
day++;
}
return day - 1;
}
int main() {
vector<int> tasks = {1,2,1,2,3,1};
int space = 3;
cout << taskSchedulerOptimized(tasks, space) << endl; // Expected: 9
return 0;
}
Line Notes
unordered_map<int, int> nextAvailable;Tracks earliest next available day per task
int day = 1;Start day count at 1
for (int t : tasks) {Iterate over each task in order
if (nextAvailable.count(t)) {Check if task has cooldown
day = max(day, nextAvailable[t]);Jump day forward if cooldown not satisfied
nextAvailable[t] = day + space + 1;Update next available day after scheduling
day++;Increment day after scheduling
function taskSchedulerOptimized(tasks, space) {
const nextAvailable = new Map();
let day = 1;
for (const t of tasks) {
if (nextAvailable.has(t)) {
day = Math.max(day, nextAvailable.get(t));
}
nextAvailable.set(t, day + space + 1);
day++;
}
return day - 1;
}
// Test
const tasks = [1,2,1,2,3,1];
const space = 3;
console.log(taskSchedulerOptimized(tasks, space)); // Expected: 9
Line Notes
const nextAvailable = new Map();Map to track earliest next available day per task
let day = 1;Initialize day counter starting at 1
for (const t of tasks) {Iterate over each task in order
if (nextAvailable.has(t)) {Check if task has cooldown
day = Math.max(day, nextAvailable.get(t));Jump day forward if cooldown not satisfied
nextAvailable.set(t, day + space + 1);Update next available day after scheduling
day++;Increment day after scheduling
Each task is processed once with O(1) hash map operations, making it optimal for large inputs.
💡 For 100,000 tasks, this approach efficiently computes the schedule in linear time without simulating idle days explicitly.
Interview Verdict: Accepted
This is the best approach to implement in interviews due to its simplicity and efficiency.