Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Task scheduler with cooldown

    Task scheduler with cooldown

    Mediumgreedycountingheap

    Each entry in tasks is a letter naming a task that takes one time unit. Two runs of the same letter need at least n units between them (filled by other tasks or idle time). Return the least time to finish every task.

    Example 1

    Input:  tasks = ["A","A","A","B","B","B"], n = 2
    Output: 8

    A B idle A B idle A B = 8.

    Example 2

    Input:  tasks = ["A","C","A","B","D","B"], n = 1
    Output: 6

    Constraints

    • 1 ≤ tasks.length ≤ 10⁴
    • tasks[i] is an uppercase letter
    • 0 ≤ n ≤ 100

    Submit checks your solution against 8 tests, including hidden ones.

    /**
     * @param {string[]} tasks
     * @param {int} n
     * @return {int}
     */
    function leastInterval(tasks, n) {
      
    }
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.