java one map solution, easy to understand


  • 2
    O
    public class Solution {
        public int leastInterval(char[] tasks, int cooldown) {
            HashMap<Character, Integer> map = new HashMap<>();
            int max = 0;
            int n = tasks.length;
            for (char c : tasks) {
                if(!map.containsKey(c)) {
                    map.put(c, 0);
                }
                map.put(c, map.get(c) + 1);
                max = Math.max(max, map.get(c));
            }
            int count = (max - 1) * (cooldown + 1);
            int extra = 0;
            for(int val : map.values()){
                if(val > max - 1){
                    extra++;
                }
            }
            return Math.max(count + extra, n);
        }
    }
    

  • 0
    S

    Hi

    Your solution seems sleek and well articulated. Could you please give an explanation too. Thanks


Log in to reply
 

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