📣 独立日限时特惠: VIP通行证立减$68
回复: 18
跳转到指定楼层
上一主题 下一主题
收起左侧

Amazon OA 2 Round Robin 求助

全局:

2016(1-3月) 码农类General 本科 全职@amazon - 内推 - 在线笔试  | | Other | 应届毕业生

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Round Robin Average Wait Time的题,对                                                                                                                                                                                                [size=11.000000pt]i
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
错了还是程序有小bug?                                       
                               
                       
               


上一篇:AMAZON - OA (Who's Our Boss? (Duplicate)),谁有题库、面经?
下一篇:Knight Capital (KCG) 面经
推荐
wbcustc 2016-1-6 07:27:07 | 只看该作者
全局:
public class Event{
            int arrive;
            int execute;
            public Event(int arrive,int execute){
                    this.arrive = arrive;
                    this.execute = execute;
            }
    }
   
    public double roundRobin(int[] arrive,int[] execute,int limit){
            int n = arrive.length;
            Event[] events = new Event[n];
            for(int i = 0;i<n;i++){
                    events[i] = new Event(arrive[i],execute[i]);
            }
            Arrays.sort(events,new Comparator<Event>(){
                    public int compare(Event e1,Event e2){
                            return e1.arrive-e2.arrive;
                    }
            });
            int nextIndex = 1,currTime = events[0].arrive,waitTime = 0;
            Queue<Event> queue = new LinkedList<>();
            queue.offer(events[0]);
            while(!queue.isEmpty()||nextIndex<n){
                    if(!queue.isEmpty()){
                            Event currEvent = queue.poll();
                            waitTime += currTime-currEvent.arrive;
                            boolean finish = true;
                            if(currEvent.execute<=limit){
                                    currTime += currEvent.execute;       
                            }else{
                                    currTime += limit;
                                    finish = false;
                            }
                           
                            for(int i = nextIndex;i<n;i++){
                                    if(events[i].arrive<=currTime){
                                            queue.offer(events[i]);
                                            nextIndex = i+1;
                                    }else{
                                            break;
                                    }
                            }
                           
                              if(!finish){
                                    Event remain = new Event(currTime,currEvent.execute-limit);
                                    queue.offer(remain);
                            }
                    }else{
                            currTime = events[nextIndex].arrive;
                            queue.offer(events[nextIndex]);
                            nextIndex++;
                    }
                     
            }
            return waitTime*1.0/n;
    }

这个代码debug过很多test case 应该没问题
回复

使用道具 举报

全局:
class Process{
  int arriveTime;
  int executeTime;
  Process(int arr, int exe){
    this.arriveTime = arr;
    this.executeTime = exe;
}
}

public static double roundRobin(int[] arrive, int[] execute, int q){
                if(arrive == null || execute == null || arrive.length != execute.length)
                        return 0.0;
                int len = arrive.length;
                int currTime = 0;
                int waitTime = 0;
                int i = 0;
                PriorityQueue<Process> pq = new PriorityQueue<Process>(len, new Comparator<Process>(){
                        @Override
                        public int compare(Process p1, Process p2){
                                return p1.arriveTime - p2.arriveTime;
                               
                        }

                       
                });
               
                while(!pq.isEmpty()  || i < len){
                        if(!pq.isEmpty()){
                                Process tmp = pq.poll();
                                waitTime += currTime - tmp.arriveTime;
                                if(tmp.executeTime > q){
                                        currTime += q;
                                        pq.offer(new Process(currTime,tmp.executeTime - q));
                                }else{
                                        currTime += tmp.executeTime;
                                }
                                for(; i < len; i++){
                                        if(arrive[i] <= currTime){
                                                pq.offer(new Process(arrive[i],execute[i]));
                                        }else
                                                break;
                                }
                        }else{
                                currTime = arrive[i];
                                pq.offer(new Process(arrive[i],execute[i]));
                                i++;
                        }
                }
                return waitTime * 1.0 / len;
               
        }

补充内容 (2016-1-5 13:15):
不知道对不对,还望楼主指出
回复

使用道具 举报

全局:
我的跟这个差不多,这是另一个帖子里面的
http://www.1point3acres.com/bbs/thread-142143-1-1.html
public class process {
        int arriveTime;
        int excuteTime;
        process(int arr, int exc) {
                arriveTime = arr;
                excuteTime = exc;
        }. From 1point 3acres bbs
}

// Assume arrive is sorted..1point3acres缃�
public double roundRobin(int[] arrive, int[] excute, int q) {
        LinkedList<process> queue = new LinkedList<>();
        int curTime = 0;
        int waitTime = 0;
        int nextProIdx = 0;
        while (!queue.isEmpty() || nextProIdx < arrive.length) {
                if (!queue.isEmpty()) {
                        process cur = queue.poll();
                        waitTime += curTime - cur.arriveTime;
                        curTime += Math.min(cur.excuteTime, q);
                        for (int i = nextProIdx; i < arrive.length; i ++) {
                                if (arrive[i] <= curTime) {
                                        queue.offer(new process(arrive[i], excute[i]));
                                        nextProIdx = i + 1;
                                } else {
                                        break;
                                }
                        }
                        if (cur.excuteTime > q) {
                                queue.offer(new process(curTime, cur.excuteTime - q);
                        }
                } else {
                        queue.offer(new process(arrive[nextProIdx], excute[nextProIdx]));
                        curTime = arrive[nextProIdx ++];
                }
        }
        
        return (double)waitTime / arrive.length;
}
回复

使用道具 举报

🔗
nycok 2016-1-5 11:46:41 | 只看该作者
全局:
我跑了我自己的代码(oa test cases all pass),答案也是1
回复

使用道具 举报

🔗
 楼主| leoyue 2016-1-5 12:05:25 | 只看该作者
全局:
nycok 发表于 2016-1-5 11:46
我跑了我自己的代码(oa test cases all pass),答案也是1

好的好的我再debug一下,顺便问一下层主跑   
int[] arrival1 = {0, 1, 4};
int[] run1 = {5, 2, 3};
int q1 = 3;
这个case是得到2.3333么,就是amazon给的case
回复

使用道具 举报

🔗
pengzewen37 2016-1-5 12:19:48 | 只看该作者
全局:
你用的是哪个面经的程序算的 我写的算出答案跟你一样
回复

使用道具 举报

🔗
 楼主| leoyue 2016-1-5 12:23:25 | 只看该作者
全局:
pengzewen37 发表于 2016-1-5 12:19
你用的是哪个面经的程序算的 我写的算出答案跟你一样

http://www.1point3acres.com/bbs/thread-144891-1-1.html 给的附件,还有另一个之前的总结贴吧,反正算出来都是1.25但是应该是错的,能看一下版主你的代码么?

补充内容 (2016-1-5 12:24):
难道是我改写成c++的时候写错了么==但我看了一下应该是对的啊。。。
回复

使用道具 举报

🔗
pengzewen37 2016-1-5 12:27:40 | 只看该作者
全局:
我的跟这个帖子里面3楼差不多http://www.1point3acres.com/bbs/thread-142143-1-1.html
回复

使用道具 举报

🔗
 楼主| leoyue 2016-1-5 12:29:13 | 只看该作者
全局:
pengzewen37 发表于 2016-1-5 12:27
我的跟这个帖子里面3楼差不多http://www.1point3acres.com/bbs/thread-142143-1-1.html

恩好像是我改写的时候出了错误。。。
回复

使用道具 举报

🔗
pengzewen37 2016-1-5 12:33:29 | 只看该作者
全局:
leoyue 发表于 2016-1-4 23:29
恩好像是我改写的时候出了错误。。。

啊?所以答案还是1.25?
回复

使用道具 举报

🔗
 楼主| leoyue 2016-1-5 12:34:10 | 只看该作者
全局:
pengzewen37 发表于 2016-1-5 12:33
啊?所以答案还是1.25?

不。。。是他们写的java跑出来也是1。。。但是我改写成c++就错了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表