# 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; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up