FB面经prepare: Task Schedule
Task 面经 prepare Schedule FB
2023-09-11 14:14:07 时间
每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间。
用HashMap保存每一个task的下一次可以开始执行的最早时间
1 package TaskSchedule; 2 import java.util.*; 3 4 public class Solution { 5 public int schedule(int[] str, int recover) { 6 if (str==null || str.length==0) return 0; 7 if (recover == 0) return str.length; 8 int pos = 0; 9 int time = 0; 10 HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); 11 for (; pos<str.length; pos++) { 12 int cur = str[pos]; 13 if (!map.containsKey(cur)) { 14 map.put(cur, time+recover+1); 15 } 16 else { 17 int lastApr = map.get(cur); 18 if (time >= lastApr) { 19 map.put(cur, time); 20 } 21 else { 22 pos--; 23 } 24 } 25 time++; 26 } 27 return time; 28 } 29 30 /** 31 * @param args 32 */ 33 public static void main(String[] args) { 34 // TODO Auto-generated method stub 35 Solution sol = new Solution(); 36 System.out.println(sol.schedule(new int[]{1, 2, 3, 1, 2, 3}, 3)); 37 38 } 39 40 }
相关文章
- SV中的task和function
- [AngularJS + Gulp] Gulp task
- [Functional Programming] Capture Side Effects in a Task / Async
- spark重要参数调优建议:spark.default.parallelism设置每个stage默认的task数量
- Hadoop MapReduce实例:按照手机号归属地分区统计手机上网流量(Reduce Task进程数设置)
- 一个简单的模拟实例说明Task及其调度问题
- Android问题笔记 - 编译报错Task :app:compileDebugJavaWithJavac FAILED
- Dynamics CRM2016 业务流程之Task Flow(二)
- 【Linux 内核】调度器 ④ ( sched_class 调度类结构体分析 | yield_task 函数 | heck_preempt_curr 函数 | task_struct 函数 )
- 【Android Gradle 插件】自定义 Gradle 任务 ③ ( Gradle 自定义任务创建方法 Project#task 函数 | Task#doFirst 函数用法 )
- 【看表情包学Linux】进程的概念 | 进程控制块 PCB | 父进程与子进程 | 进程 ID | task_struct