---
# System prepended metadata

title: 2365. Task Scheduler II
tags: [Leetcode, HashMap, Medium]

---

# 2365. Task Scheduler II
###### tags: `Leetcode` `Medium` `HashMap`
Link: https://leetcode.com/problems/task-scheduler-ii/description/
## 思路
记录每一个task上一次完成的时间
如果当前时间和上一次完成的时间差小于等于space 说明需要额外休息
## Code
```java=
class Solution {
    public long taskSchedulerII(int[] tasks, int space) {
        Map<Integer, Long> map = new HashMap<>();
        int n = tasks.length;
        long ans = 0;
        for(int i=0; i<n; i++){
            ans++;
            if(map.containsKey(tasks[i]) && ans-map.get(tasks[i])<=space){
                ans = map.get(tasks[i])+space+1;
            }
            map.put(tasks[i], ans);
        }
        return ans;
    }
}
```