Leetcode - Top K Frequent Elements
Leetcode - Plus One Linked List

Leetcode - Task Scheduler

violet posted @ Jul 29, 2020 01:21:45 AM in 算法 with tags Algorithm Java Greedy , 10967 阅读


You are given a char array representing tasks CPU need to do. It contains capital letters A to Z where each letter represents a different task. Tasks could be done without the original order of the array. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.

However, there is a non-negative integer n that represents the cooldown period between two same tasks (the same letter in the array), that is that there must be at least n units of time between any two same tasks.

You need to return the least number of units of times that the CPU will take to finish all the given tasks.


Example 1:

Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
A -> B -> idle -> A -> B -> idle -> A -> B
There is at least 2 units of time between any two same tasks.

Example 2:

Input: tasks = ["A","A","A","B","B","B"], n = 0
Output: 6
Explanation: On this case any permutation of size 6 would work since n = 0.
And so on.

Example 3:

Input: tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
Output: 16
One possible solution is
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A



  • The number of tasks is in the range [1, 10000].
  • The integer n is in the range [0, 100].


class Solution {
    public int leastInterval(char[] tasks, int n) {
        int[] count = new int[26];
        int max = 0;
        int maxCount = 0;
        for (char t : tasks) {
            if (max == count[t-'A']) {
            } else if (max < count[t-'A']) {
                max = count[t-'A'];
                maxCount = 1;
        int partCount = max-1;
        int partLen = n - (maxCount-1);
        int emptySlot = partCount * partLen;
        int availableTask = tasks.length - max * maxCount;
        int idles = Math.max(0, emptySlot - availableTask);
        return tasks.length + idles;
Andhra Bank retail l 说:
Aug 10, 2022 04:43:11 PM

The customer who has got their ATM card, Date of birth, and mobile number linked with an Account number can go for self-online registration of internet banking services, Andhra Bank retail login else they need to get this detail verified and linked with your Union Bank of India Account and use the process to activate their UBI net banking. The Union Bank of India is now merged with Andhra Bank and Corporation Bank, and the customer of Union Bank of India can still use the online services from its official website along with experiencing the Internet Banking features.

AP SSC Urdu Question 说:
Sep 17, 2022 01:26:09 AM

Urdu is one of the main languages in the state, and this is the first language for Urdu Medium students, there are fewer schools are working in all districts of the state, all the applicable students also can download AP SSC Urdu Model Paper 2023 Pdf in chapter wise for all lessons of the course, AP SSC Urdu Question Paper download, and practice the Ibtedai Question bank to get better rank in all exams conducted by BSEAP. Urdu is one of the main languages in the state, and this is the first language for Urdu Medium students, there are fewer schools are working in all districts of the state, all the applicable students also can download AP SSC Urdu Model Paper 2023 Pdf in chapter wise for all lessons of the course.

BSKY Apply 说:
Oct 27, 2022 06:13:18 PM

Health care is a significant aspect of everyone's life. However, it's also challenging and expensive, forcing many people to seek cheap services. Many people live with threatening illnesses due to a lack of good facilities and money. BSKY Apply The Odisha state government caters to the residents' health and works to provide the best facilities. The government has developed a health care scheme, namely Odisha Biju Swasthya Kalyan Yojana.

Amma Two Wheeler Sch 说:
Oct 28, 2022 05:35:11 PM

Generally, women occupy the largest population globally. They also fall among the vulnerable groups in many countries. These advocates for governments to establish social schemes which boost women livelihood. Amma Two Wheeler Scheme Application 2023 The Tamil Nadu state government has initiated an exclusive scheme to help women earn a better living. The Amma Two Wheeler is a women development scheme established in 2018. The Tamil Nadu government provides women with a two-wheeler to help when going to work.

Emma 说:
Nov 15, 2022 08:19:54 PM

Leetcode's Task Scheduler is a great tool for managing your TODO list and keeping track of your progress on coding projects. The interface is simple and easy to use, and the Southern California bands features are robust and helpful. The ability to see your progress on a project over time is particularly useful, as it can help you identify when you're falling behind and need to step up your game.

पुरानेमॉडलपत्र.com 说:
May 01, 2023 03:28:19 PM

Hindi Model Question Papers Bihar School 10th Exam Board BSEB, Bihar Board 10th Class Model Papers 2023 Matric Exam Pattern, BSEB Patna Board Matric 10th Model Papers 2023 Sample Questions, Bihar Board 10th Model Papers 2023 Download, पुरानेमॉडलपत्र.com BSEB Matric Bihar matric model paper 2023 bseb 10th sample paper 2023, bihar board 10th model paper 2023 pdf, bihar board 10th class model test paper 2023, bseb matric previous sample paper class 10th bihar board model question paper, bseb matric model question paper 2023.

alexahills 说:
Jun 19, 2024 09:11:57 PM

I am bookmarking this webpage in my favorite's folder so I can return again. You made valid points in this content that I concludes require further exploration. And I concur with much of this info.<a href="https://www.dimple-gupta.com/">Delhi Escort</a>||
<a href="http://www.callgirlsnearhotels.com/">Delhi Hotel Escorts</a>||
<a href="https://delhicallgirl.in/">Escort in Delhi</a>||
<a href="https://www.modelescortsindelhi.com/aerocity-escorts/">Aerocity Escort Service</a>||

登录 *

loading captcha image...
or Ctrl+Enter