followup是tasks是无序的.
一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... followup是无序的,就是不用按给的顺序执行,也就是可以先执行task1,然后task1还没恢复时,先执行task2, etc......

正确的做法应该是统计每个task的frequency,然后每次选frequency最高并且可以执行的task执行。

用maxHeap存每个task的剩余frequency

 package TaskSchedule;
import java.util.*; public class Solution2 {
public class Element {
int val;
int appr;
public Element(int value) {
this.val = value;
this.appr = 1;
}
} public int schedule(int[] arr, int recover) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
PriorityQueue<Element> queue = new PriorityQueue<Element>(11, new Comparator<Element>() {
public int compare(Element e1, Element e2) {
return e2.appr-e1.appr;
}
});
Element[] summary = new Element[10];
for (int each : arr) {
if (summary[each] == null) {
summary[each] = new Element(each);
}
else summary[each].appr++;
}
for (Element elem : summary) {
if (elem != null)
queue.offer(elem);
} int time = 0;
LinkedList<Element> temp = new LinkedList<Element>();
while (!queue.isEmpty() || !temp.isEmpty()) {
if (!queue.isEmpty()) {
Element cur = queue.poll();
if (!map.containsKey(cur.val)) {
map.put(cur.val, time+recover+1);
cur.appr--;
if (cur.appr > 0) temp.offer(cur);
while (!temp.isEmpty()) {
queue.offer(temp.poll());
}
time++;
} else { //map contains cur.val
if (time >= map.get(cur.val)) { //time is feasible
map.put(cur.val, time+recover+1);
cur.appr--;
if (cur.appr > 0) temp.offer(cur);
while (!temp.isEmpty()) {
queue.offer(temp.poll());
}
time++;
}
else { //time is not feasible
temp.offer(cur);
}
}
} else { //queue is empty, but temp is not empty
while (!temp.isEmpty()) {
queue.offer(temp.poll());
}
time++;
}
}
return time;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution2 sol = new Solution2();
System.out.println(sol.schedule(new int[]{1, 1, 2, 2, 3, 3}, 2));
} }

FB面经prepare: task schedule II的更多相关文章

  1. FB面经prepare: Task Schedule

    每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间. 用HashMap保存每一个task的下一次可以开始执行的最早时间 package TaskS ...

  2. FB面经 Prepare: Task Schedule

    tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...

  3. [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

    Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...

  4. HDU 3572 Task Schedule(拆点+最大流dinic)

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  5. Task schedule 分类: 比赛 HDU 查找 2015-08-08 16:00 2人阅读 评论(0) 收藏

    Task schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. HDU3572 Task Schedule 【最大流】

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  7. HDU4907——Task schedule(BestCoder Round #3)

    Task schedule Description有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务.有m个询问,每个询问有一个数字q,表示如 ...

  8. hdu 3572 Task Schedule

    Task Schedule 题意:有N个任务,M台机器.每一个任务给S,P,E分别表示该任务的(最早开始)开始时间,持续时间和(最晚)结束时间:问每一个任务是否能在预定的时间区间内完成: 注:每一个任 ...

  9. hdoj 3572 Task Schedule【建立超级源点超级汇点】

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. android ListView详解继承ListActivity

    [转]http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html 在android开发中ListView是比较常用的组件,它以列表的形式展 ...

  2. 算导Ch34. NP Complete

    1.图灵停机问题:无论在多长时间内都无法被任何一台计算机解决 问题描述:问题为H,H的输入数据为P(P是一段程序(程序也是一串字符串数据)),判定P在输入w下是否能够最终停止 H(P(w))=0 若P ...

  3. C++ 简易时间类

    .h file #ifndef LIBFRAME_DATETIME_H_ #define LIBFRAME_DATETIME_H_ #include <stdint.h> #include ...

  4. php--递归调用

  5. angularJS中controller的通信

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. java对象中继承和变量初始化顺序浅析

    先上例子代码 public class F { int age = 5; public F() { print(); } public void print() { System.out.printl ...

  7. 20145211 《Java程序设计》第1周学习总结——小荷才露尖尖角

    教材学习内容总结 Java语言概述 Java是SUN1995年推出的一门高级编程语言,完全面向对象,安全可靠,具有跨平台性(用其编写的语言在任何系统上都能运行,只需安装一个JVM) Java三大平台包 ...

  8. 位与(&)常用编程技巧

    补充知识:1)正整数的补码与原码相同:                2)求负整数的补码:原码 符号位不变,数值位各位取反,最后整个数加1得到补码:                3)按位与& ...

  9. The Beginner’s Guide to iptables, the Linux Firewall

    Iptables is an extremely flexible firewall utility built for Linux operating systems. Whether you’re ...

  10. git 项目初始化

    1.在git服务器界面右上角“+” .create  new  project ,写上项目名字生成一个新的组 2.如果机器第一次与git 建立连接,需要让机器生成一个id_rsa和id_rsa.pub ...