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
// @Title: 任务调度器 (Task Scheduler)
// @Author: 15816537946@163.com
// @Date: 2020-12-05 13:33:17
// @Runtime: 20 ms
// @Memory: 6.1 MB
func leastInterval(tasks []byte, n int) int {
	cnt := make(map[byte]int)
	for _, t := range tasks {
		cnt[t]++
	}

	maxExec, maxExecCnt := 0, 0
	for _, c := range cnt {
		if c > maxExec {
			maxExec, maxExecCnt = c, 1
		} else if c == maxExec {
			maxExecCnt++
		}
	}

	if time := (maxExec-1)*(n+1) + maxExecCnt; time > len(tasks) {
		return time
	}
	return len(tasks)

}