Tuesday, October 16, 2018

greedy algorithm https://leetcode.com/problems/task-scheduler/description/


# task-scheduler ( medium ) https://leetcode.com/problems/task-scheduler/description/ 매번 최대 count를 가진 character의 count를 줄여가면서 값을 찾음 ### 좋은 예제 - AAAABBCCDD inteval = 1 ```java public class Solution { public int leastInterval(char[] tasks, int n) { HashMap map = new HashMap(); Comparator comparator = new Comparator() { @Override public int compare(Number o1, Number o2) { return (o2.c - o1.c); } }; PriorityQueue numbers = new PriorityQueue(100, comparator); for(int i=0; i0){ count =0; ArrayList list = new ArrayList(); for(int i=0; i<=n; i++){ Number number = numbers.poll(); if(number == null){ break; } number.c--; count++; if(number.c ==0){ }else{ list.add(number); } } numbers.addAll(list); list.clear(); if(numbers.size() >0){ total += (n+1); } } total+= count; return total; } public class Number{ char n; int c; Number(char n , int c){ this.n = n; this.c = c; } } }```

No comments:

Post a Comment