Clear Java solution, O(1) space O(n) time


  • 0
    L
    public class Solution {
        public int leastInterval(char[] tasks, int n) {
            int maxSize = 0;
            int maxCount = 0;
            Map<Integer, Integer> groups = new HashMap();
            for (int task : tasks) {
                int count = groups.getOrDefault(task, 0) + 1;
                if (count == maxSize) {
                    maxCount += 1;
                } else if (count > maxSize) {
                    maxSize = count;
                    maxCount = 1;
                }
                groups.put(task, count);
            }
            // time to finish all tasks VS all largest groups [(maxSize - 1) * (n + 1) + maxCount]
            return Math.max(tasks.length, (maxSize - 1) * (n + 1) + maxCount);
        }
        
    }
    

Log in to reply
 

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