Task Schedule II

本文介绍了一种基于优先级队列的任务调度算法实现方法。通过使用哈希映射记录任务类型及其执行时间,并利用优先级队列按频率排序任务,确保高频率任务优先执行。此外,还介绍了如何处理任务超时情况。

参考:点击打开链接

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

这次得记录每个task的frequency,设计一个map,同时,用priorityQueue存最大的frequency,一个queue来存暂时超时不能运行的task

    public class Element {
        int val;
        int appr;
        public Element(int value, int appr) {
            this.val = value;
            this.appr = appr;
        }
    }
    
    //public int schedule(int[] arr, int recover) {
    public String schedule2(int[] arr, int recover) {
        HashMap<Integer/*tack type*/, Integer/*time*/> 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;
            }
        });
        HashMap<Integer/*tack type*/, Integer/*frequence*/> freMap = new HashMap<Integer, Integer>();
        for (int each : arr) {
            if (freMap.containsKey(each)) {
                freMap.put(each, freMap.get(each) + 1);
            } else {
                freMap.put(each, 1);
            }
        }
        for (Map.Entry<Integer, Integer> entry: freMap.entrySet()) {
            queue.offer(new Element(entry.getKey(), entry.getValue()));
        }

        int time = 0;
        Queue<Element> temp = new LinkedList<Element>();
        StringBuilder sb = new StringBuilder();
        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++;
                    sb.append(cur.val + ",");
                }
                
                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++;
                        sb.append(cur.val + ",");
                    }
                    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++;
                sb.append("_,");
            }
        }
        //return time;
        System.out.println("time:" + time + " strLen:" + sb.length());
        return sb.toString();
    }



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值