leetcode-621-任务调度器

81 阅读1分钟

image.png leetcode原题

解题思路

  • 此题的关键是计算出(数量最多的任务数 - 1)*(冷却时间 + 1) + 数量最多的任务种数 和 所有任务数的较大值
var leastInterval = function(tasks, n) {
    const obj = {}
    for (let i = 0; i < tasks.length; i++) {
        if (!obj[tasks[i]]) {
            obj[tasks[i]] = 1
        } else {
            obj[tasks[i]]++
        }
    }
    // 获取数量最多的任务种类的任务数
    const max = Math.max(...Object.values(obj))
    let maxCount = 0
    // 获取数量最多的任务种数
    Object.values(obj).forEach(i => {
        if (i === max) maxCount++
    })
    // 获取两者之间的较大值
    return Math.max((max - 1) * (n + 1) + maxCount, tasks.length)
};