FB面经prepare: task schedule II
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的更多相关文章
- FB面经prepare: Task Schedule
每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间. 用HashMap保存每一个task的下一次可以开始执行的最早时间 package TaskS ...
- FB面经 Prepare: Task Schedule
tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...
- [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)
Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...
- HDU 3572 Task Schedule(拆点+最大流dinic)
Task Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 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 ...
- HDU3572 Task Schedule 【最大流】
Task Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU4907——Task schedule(BestCoder Round #3)
Task schedule Description有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务.有m个询问,每个询问有一个数字q,表示如 ...
- hdu 3572 Task Schedule
Task Schedule 题意:有N个任务,M台机器.每一个任务给S,P,E分别表示该任务的(最早开始)开始时间,持续时间和(最晚)结束时间:问每一个任务是否能在预定的时间区间内完成: 注:每一个任 ...
- hdoj 3572 Task Schedule【建立超级源点超级汇点】
Task Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- Centos下安装Scrapy
Scrapy是一个开源的机遇twisted框架的python的单机爬虫,该爬虫实际上包含大多数网页抓取的工具包,用于爬虫下载端以及抽取端. 安装环境: centos5.4 python2.7.3 安装 ...
- 【转】Java 5种字符串拼接方式性能比较。
最近写一个东东,可能会考虑到字符串拼接,想了几种方法,但对性能未知,于是用Junit写了个单元测试. 代码如下: import java.util.ArrayList; import java.uti ...
- 【转】C#高性能大容量SOCKET并发(二):SocketAsyncEventArgs封装
http://blog.csdn.net/sqldebug_fan/article/details/17557341 1.SocketAsyncEventArgs介绍 SocketAsyncEvent ...
- jquery 事件 多次绑定,多次触发,怎么清除历史绑定事件
Jquery在处理事件的时候有时候会遇到预想不到的结果 如下 <a id="link_foo">Foo</a> <script type=" ...
- Oracle客户端显示乱码解决
1.查找当前服务器的字符集: 2.使用查询得到的结果集,设置本地local字符集:(不要照抄图片)
- SqlServer数据组织结构
page页 每个页面8KB,连续的8个页面称之为一个区extents, 如:2.18MB的一个DB的区大约有 2.18 MB (2,293,760 字节)=2,293,760b/8kb=280个页面= ...
- Delphi HOOK示例
本应用程序的Hook: unit UFrmMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...
- spring3 mvc:方法返回值的学习
新建后台代码用以测试返回类型,在这里我新建的如下: /** * 项目名称:Spring3mvc demo * Copyright ? 2010-2012 spartacus.org.cn All Ri ...
- String 与StringBuffer比较
package String比较; /* * String 与StringBuffer比较 * String 不可变,一旦赋值,就不能被修改 * StringBuffer可变的字符串. * Strin ...
- Average Cost (AVCO) Method
http://accountingexplained.com/financial/inventories/avco-method Average Cost (AVCO) Method Aver ...