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. DOM下的节点属性和操作小结

    属性: 1 .nodeName 节点名称,相当于tagName.属性节点返回属性名,文本节点返回#text.nodeName,是只读的. 2 .nodeType 值:1,元素节点:2,属性节点:3,文 ...

  2. OneDrive无法正常登录

    可能是DNS被污染 手动设置DNS 134.170.108.26 onedrive.live.com134.170.108.152 skyapi.onedrive.live.com

  3. mysql 存储过程 php版本

    <?php /** * PHP操作Mysql存储过程示例 * * @author flyer0126 * @date 2011-12-23 * */ //配置数据库连接信息 $hostname ...

  4. SQL代码创建表例子

    create table class ( code ) primary Key, name ) ); create table student ( code ) primary Key, name ) ...

  5. angularJS自定义指令间的“沟通”

    由此例子我们可以看出,angularJS使用指令时link的执行顺序<html> <head> <meta charset="utf-8"/> ...

  6. SQL SERVER中非聚集索引的覆盖,连接,交叉,过滤

    1.覆盖索引:select和where中包含的结果集中应存在“非聚集索引列”,这样就不用查找基表了,索引表即可搞定:   2.索引交叉:索引的交叉可以理解成建立多个非聚集索引之间的join,如表实体一 ...

  7. MVC4怎么设置@Html.TextBoxFor这样的输入框的css样式

    在传统webForm中,输入框的这样的: <input id="userName" name="userName" type="text&quo ...

  8. 【转】Android新组件Material Dialog,SwipeRefreshLayout,ListPopupWindow,PopupMenu等

    朝花夕拾----新组件的学习和使用 分类: Android UI2015-06-26 11:31 440人阅读 评论(0) 收藏 举报 uidialogMaterial   目录(?)[-] Mate ...

  9. imx6 MFG TOOL 分析

    之前分析过mfgtool的内容,最近从官网下载,返现新版的mfgtool工具将imx6各种版本的linux/android都使用一个工具进行烧录.所以从新分析一下. 新版与旧版的一个区别是烧写使用的u ...

  10. android 打开软件出现红框

    android打开软件的时候会出现红框,剑锋之前解了这个问题.fork过来,方便以后查看. 参考链接: http://www.cnblogs.com/zengjfgit/p/5377744.html ...