由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教F最近超爱的面试题任务安排的follow up怎么做
相关主题
fb国内申请的曲折经历+电面VMWare Interview question
求两道题目的代码,学习一下贡献个面试题,目前狗狗都没找到.....
L家coding question一问求大牛指教,字符串分割的DP做法!
请教一道面试题Ebay三电面,攒人品
帮我看下这是份什么工作,不是骗钱的吧?这个题没见过大家可以很快做出来吗?
我也来贡献一下亚马的题Public Health PhD 业界求职经验3-面试
MathWorks 面经On-site的一点点经历
any open source project for this problem?MS On Campus 题目
相关话题的讨论汇总
话题: task话题: string话题: int话题: cooldown话题: character
进入JobHunting版参与讨论
1 (共1页)
y*****e
发帖数: 712
1
这题我第一次看到是在板上,在这里
http://www.mitbbs.com/article_t/JobHunting/32876295.html
Tasks: AABABCD
Cooldown Time: 2
A__AB_ABCD
Output: 10
后来发现这题很快变成fb的新欢,各种follow up啊。。。今天看到一个实在心塞的,
越想越复杂,想发上来问问大家有啥好主意
follow up是给你一个task队列, 要求你自己排列要求最后的总时间最小,比如
aaaabbbccd,可以排成abcabcabda。
看到有人建议说每次排gap + 1个任务,取distinct tasks,也就是尽量分开同种任务
,放的时候从frequency高的开始,我觉得这就需要一个treemap了吧,先abc放完,然
后再放一轮abc,放一个减少count,count == 0的时候从treemap里去掉。如果是aaab怎
样的呢?放完ab只能再空等一次才能再放a,这样的话是不是应该有个deque来实现?
b******i
发帖数: 914
2
刚写了一个,没有好好test,请指点:
不过好像你主要是问follow-up?那我再想想
顺便问一下,我就见过之前一次,你还在哪儿的面经看到过这个题的
class Solution {
public:
int task_scheduler(string s, int t) {
int n = s.length();
if(n == 0)
return 0;
if(t <= 0)
return n;
vector lastindex(256, -1);
int shift = 0;
for(int i = 0; i < n; i++) {
char c = s[i];
int curr = shift + i;
if(lastindex[c] != -1) {
shift += max(0, lastindex[c] + t + 1 - curr);
}
lastindex[c] = shift + i;
}
return shift + n;
}
};
int main() {
Solution solution;
cout << solution.task_scheduler("AABABCD", 2) << endl;
}

【在 y*****e 的大作中提到】
: 这题我第一次看到是在板上,在这里
: http://www.mitbbs.com/article_t/JobHunting/32876295.html
: Tasks: AABABCD
: Cooldown Time: 2
: A__AB_ABCD
: Output: 10
: 后来发现这题很快变成fb的新欢,各种follow up啊。。。今天看到一个实在心塞的,
: 越想越复杂,想发上来问问大家有啥好主意
: follow up是给你一个task队列, 要求你自己排列要求最后的总时间最小,比如
: aaaabbbccd,可以排成abcabcabda。

h****3
发帖数: 89
3
感觉要用 priority queue + stack
(1) 先按照每个task出现的次数建立pq
(2) 如果给定的cool down time是n,那么从heap顶端poll n个元素放入stack,同时
用这n个元素建立新task的顺序
(3) 再把stack中的每个元素出现次数-1,放入pq
重复只到pq为0
不知道这个想法对不对,请大家指正
h****3
发帖数: 89
4
public static String bestTime(String tasks, int cooldown){
if(tasks==null || tasks.length()==0 || cooldown<0){
throw new IllegalArgumentException("input not valid");
}
int waitTime = cooldown;
int curRep =0;
StringBuilder res = new StringBuilder();
Stack stack = new Stack();
HashMap map = new HashMap();
PriorityQueue pq = new PriorityQueue(11, new Comparator<
Task>(){
@Override
public int compare(Task a, Task b){
return b.rep - a.rep;
}
});
for(int i=0;i char c = tasks.charAt(i);
if(map.containsKey(c)){
map.put(c, map.get(c)+1);
}else{
map.put(c, 1);
}
}
Iterator it = map.keySet().iterator();
while(it.hasNext()){
char tempChar = (Character)it.next();
Task newTask = new Task(tempChar,map.get(tempChar));
pq.offer(newTask);
}
while(pq.isEmpty()==false){
while(waitTime>=0){
Task tempTask = pq.poll();
res.append(tempTask.name);
curRep = tempTask.rep;
if(curRep>1){
tempTask.setRep(tempTask.rep-1);
stack.push(tempTask);
}
waitTime--;
if(pq.isEmpty()){
break;
}
}
waitTime = cooldown;
while(stack.isEmpty()==false){
pq.offer(stack.pop());
}
}
return res.toString();
}

public static void main(String [] args){
String str = "aababcd";
String str2 = "aaaab";
int cooldown = 2;
String best = bestTime(str,cooldown);
String best2 = bestTime(str2,cooldown);
System.out.println(best2);
}
h****3
发帖数: 89
5
把这个followup 谢了一下,请大牛指点一下对不对
task 用一个字母表示
class 定义如下,返回的是最优的task排序
public char name;
/* how many time the same task has been scheduled */
public int rep;
public Task(char name, int rep){
this.name = name;
this.rep = rep;
}
public void setRep(int rep){
this.rep = rep;
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
MS On Campus 题目帮我看下这是份什么工作,不是骗钱的吧?
[合集] OS interview questions我也来贡献一下亚马的题
贡献top 20 behavioral questions和自己的心得(补充)MathWorks 面经
找工作经验-3any open source project for this problem?
fb国内申请的曲折经历+电面VMWare Interview question
求两道题目的代码,学习一下贡献个面试题,目前狗狗都没找到.....
L家coding question一问求大牛指教,字符串分割的DP做法!
请教一道面试题Ebay三电面,攒人品
相关话题的讨论汇总
话题: task话题: string话题: int话题: cooldown话题: character