Java O(n) solution with one size 26 map


  • 0
    C
    public class Solution {
        public int leastInterval(char[] tasks, int n) {
            int l=tasks.length;
            if(l<=1) return l;
            int[] count=new int[26];
            for(int i=0;i<l;i++) count[tasks[i]-'A']++;
            Arrays.sort(count);
            int max=count[25],total=0;
            for(int i=24;i>=0;i--){
                if(count[i]!=max) break;
                total++;
            }
            return Math.max((n+1)*max-n+total,l);
        }
    }
    

Log in to reply
 

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