621. Task Scheduler

Fancy
https://leetcode.com/problems/task-scheduler/discuss/104496/concise-Java-Solution-O(N)-time-O(26)-space

 

 1 class Solution {
 2     public int leastInterval(char[] tasks, int n) {
 3         if(tasks.length == 0) return 0;
 4         if(n == 0) return tasks.length;
 5         int[] count = new int[26];
 6         for(int i = 0; i < tasks.length; i++){
 7             count[tasks[i] - 'A']++;
 8         }
 9         Arrays.sort(count);
10         int max = count[25];
11         int num = 0;
12         for(int i = 0; i < 26; i++){
13             if(count[i] == max){
14                 num++;
15             }
16         }
17         return Math.max(tasks.length, (max-1) * (n+1) + num);
18         
19     }
20 }

 

 

 

 

 1 class Solution {
 2     public int leastInterval(char[] tasks, int n) {
 3         if(n == 0) return tasks.length;
 4         if(tasks.length == 0) return 0;
 5         int[][] record = new int[26][2];
 6         for(int i = 0; i < tasks.length; i++){
 7             record[tasks[i] - 'A'][0]++;
 8         }
 9         Arrays.sort(record, new Comparator<int[]>(){
10            @Override
11             public int compare(int[] a, int[] b){
12                 return b[0] - a[0];
13             }
14         });
15         int max = 1;
16         int i = 0;
17         while(record[i][0] == record[++i][0]) max++;
18         return Math.max(tasks.length, (record[0][0]-1) * (n+1) + max);
19         
20     }
21 }

 

posted @ 2018-10-20 14:29  jasoncool1  阅读(122)  评论(0编辑  收藏  举报