-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTaskScheduler2.java
35 lines (28 loc) · 932 Bytes
/
TaskScheduler2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*https://leetcode.com/problems/task-scheduler-ii/*/
class Solution {
public long taskSchedulerII(int[] tasks, int space) {
HashMap<Integer,Long> map = new HashMap<Integer,Long>();
long days = 0;
int i = 0;
while (i < tasks.length)
{
if (!map.containsKey(tasks[i]) || days-(Long)map.get(tasks[i]) > space)
map.put(tasks[i++],days);
else
days = (Long)map.get(tasks[i])+space;
++days;
}
return days;
}
}
class Solution {
public long taskSchedulerII(int[] tasks, int space) {
long currentDay = 0;
Map<Integer, Long> map = new HashMap<>();
for (int num : tasks){
currentDay = Math.max(currentDay, map.getOrDefault(num, 0L));
map.put(num, ++currentDay + space);
}
return currentDay;
}
}