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
Find the task that appears for the most time. Use a map to count the number of the times the task appears then get the maximum count.The result is decided by the maximum count and the number of tasks with maximum count
Two conditions:
(1) 5 4 _ _ _ 5 4 _ _ _ 5 4 _ _ _ 5 4 the rest tasks cannot fill the empty slots
5 4 3 2 _ 5 4 3 2 _ 5 4 _ _ _ 5 4
the answer is (maxCount - 1) * (interval + 1) + CountOfMax
(2) 5 4 _ _ _ 5 4 _ _ _ 5 4 _ _ _ 5 4 the rest tasks cannot fill the empty slots
5 4 3 2 1 6 5 4 3 2 1 6 5 4 6 _ _ 5 4
the answer is the length of the nums
the task which does not have max count first fills the empty slots and then just insert any valid place
public class Solution {
public int taskScheduleUnorder(int[] nums, int interval) {
if (nums == null || nums.length == 0) {
return 0;
}
int max = 0;
int countOfMax = 0;
Map<Integer, Integer> map = new HashMap<>();
for (int task : nums) {
map.put(task, map.containsKey(task) ? map.get(task) + 1 : 1);
if (max == map.get(task)) {
countOfMax++;
} else if (max < map.get(task)) {
max = map.get(task);
countOfMax = 1;
}
}
return Math.max(nums.length, (max - 1) * (interval + 1) + countOfMax);
}
}