Java 11ms solution


  • 0
    D
        public int leastInterval(char[] tasks, int n) {
            if (tasks.length == 0) return 0;
            if (n == 0) return tasks.length;
            int[] hash = new int[26];
            int max = 0;
            List<Integer> list = new ArrayList<Integer>();
            
            for (char i : tasks) {
                hash[i - 'A']++;
            }
            for (int i = 0; i < 26; i++) {
                if (hash[i] > max) {
                    max = hash[i];
                    list.clear();
                    list.add(i);
                }else if (hash[i] == max) {
                    list.add(i);
                }
            }
            int numOfMax = list.size();
            return Math.max(tasks.length, (max - 1) * (n + 1) + numOfMax);    
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.