package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * https://leetcode.com/problems/task-scheduler/
 */
public class Problem621 {
    public int leastInterval(char[] tasks, int n) {
        Map<Character, Integer> charCountMap = new HashMap<>();
        for (int i = 0; i < tasks.length; i++) {
            if (!charCountMap.containsKey(tasks[i])) {
                charCountMap.put(tasks[i], 1);
            } else {
                charCountMap.put(tasks[i], charCountMap.get(tasks[i]) + 1);
            }
        }
        List<CharCount> charCountList = new ArrayList<>();
        charCountMap.forEach((key, value) -> {
            charCountList.add(new CharCount(key, value));
        });
        Collections.sort(charCountList, (a, b) -> Integer.compare(b.count, a.count));
        int result = 0;
        int count = 0;
        int i = 0;
        while (!charCountList.isEmpty()) {
            CharCount charCount = charCountList.get(i);
            charCount.count--;
            if (charCount.count == 0) {
                charCountList.remove(i);
                i--;
            }
            count++;
            result++;
            i++;
            if (i == 0) {
                break;
            }
            i = i % charCountList.size();
        }
        return result;
    }

    private static class CharCount {
        private final char ch;
        private int count;

        public CharCount(char ch, int count) {
            this.ch = ch;
            this.count = count;
        }

        @Override
        public String toString() {
            return ch + " -> " + count;
        }
    }

    public static void main(String[] args) {
        Problem621 prob = new Problem621();
//        System.out.println(prob.leastInterval(new char[]{'A','A','A','B','B','B'}, 2)); // 8
//        System.out.println(prob.leastInterval(new char[]{'A','A','A','B','B','B'}, 1)); // 6
//        System.out.println(prob.leastInterval(new char[]{'A','A','A','B','B','B'}, 3)); // 10
//        System.out.println(prob.leastInterval(new char[]{'A','B','B','B'}, 2)); // 7
//        System.out.println(prob.leastInterval(new char[]{'A','B','B','B'}, 1)); // 5
//        System.out.println(prob.leastInterval(new char[]{'A','B','C'}, 1)); // 3
//        System.out.println(prob.leastInterval(new char[]{'A','B','C'}, 2)); // 3
//        System.out.println(prob.leastInterval(new char[]{'A','B','C'}, 3)); // 3
//        System.out.println(prob.leastInterval(new char[]{'A','B','C','A'}, 3)); // 4
//        System.out.println(prob.leastInterval(new char[]{'A','B','C','A','C'}, 3)); // 6
//        System.out.println(prob.leastInterval(new char[]{'A','B','C','D','C'}, 3)); // 5
//        System.out.println(prob.leastInterval(new char[]{'A','B','B','C','C','D','D','D','E'}, 3)); // 9
        System.out.println(prob.leastInterval(new char[]{'A','B','B','C','C','D','D','D','E'}, 4)); // 11
    }
}

@@ -911,21 +911,17 @@
     int 
-count
+i
  = 0;%0A  
@@ -922,33 +922,64 @@
  0;%0A        
-int i = 0
+List%3CCharacter%3E list = new ArrayList%3C%3E()
 ;%0A        wh
@@ -1063,24 +1063,438 @@
 ist.get(i);%0A
+            int size = list.size();%0A            for (int j = size - 1; j %3E= (size - n) && j %3E= 0; j--) %7B%0A                if (list.get(j) == charCount.ch) %7B%0A                    for (int k = 0; k %3C n - (size - j - 1); k++) %7B%0A                        list.add(' ');%0A                        result++;%0A                    %7D%0A                    break;%0A                %7D%0A            %7D%0A%0A            list.add(charCount.ch);%0A
             
@@ -1632,29 +1632,8 @@
   %7D%0A
-            count++;%0A
     
@@ -2223,18 +2223,16 @@
 m621();%0A
-//
         
@@ -2316,18 +2316,16 @@
 ); // 8%0A
-//
         
@@ -2401,34 +2401,32 @@
 'B'%7D, 1)); // 6%0A
-//
         System.o
@@ -2503,18 +2503,16 @@
 ; // 10%0A
-//
         
@@ -2588,18 +2588,16 @@
 ); // 7%0A
-//
         
@@ -2665,34 +2665,32 @@
 'B'%7D, 1)); // 5%0A
-//
         System.o
@@ -2746,34 +2746,32 @@
 'C'%7D, 1)); // 3%0A
-//
         System.o
@@ -2827,34 +2827,32 @@
 'C'%7D, 2)); // 3%0A
-//
         System.o
@@ -2916,18 +2916,16 @@
 ); // 3%0A
-//
         
@@ -3001,18 +3001,16 @@
 ); // 4%0A
-//
         
@@ -3090,18 +3090,16 @@
 ); // 6%0A
-//
         
@@ -3179,18 +3179,16 @@
 ); // 5%0A
-//
         

Update problem 621