621. Task Scheduler

本文深入探讨了LeetCode上任务调度器问题的解决方案,通过两种Java实现方式,详细讲解了如何利用计数和记录数组来优化任务执行的时间间隔,特别关注了最大任务频率及其对调度的影响。

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 }

 

转载于:https://www.cnblogs.com/goPanama/p/9821586.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值