Task Schedule
思路
public class Solution {
public static int taskSchedule(int[] tasks, int cooldown) {
if (tasks == null || tasks.length == 0) {
return 0;
}
HashMap<Integer, Integer> map = new HashMap<>();
int slots = 0;
for (int task : tasks) {
// if task is already used or need to wait
if (map.containsKey(task) && map.get(task) > slots) {
slots = map.get(task);
}
map.put(task, slots + cooldown + 1); // next available slot for task
slots++; // used 1 slot for current task
}
return slots;
}
}Follow Up
Last updated