关闭

task schedule

63人阅读 评论(0) 收藏 举报
分类:
private String schedule11(int [] array, int recover) {
        Map<Integer, Integer> map = new HashMap<>();
        int time = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (!map.containsKey(array[i])) {
                map.put(array[i], time + 1 + recover);
                sb.append(array[i] + ",");
            } else {
                if (map.get(array[i]) <= time) {
                    map.put(array[i], time + 1 + recover);
                    sb.append(array[i] + ",");
                } else {
                    i--;
                    sb.append("_,");
                }
            }
            time++;
        }
        
        return sb.toString() + " time:" + time;
    }
    
    class Task {
        int type;
        int num;
        public Task (int type, int num) {
            this.type = type;
            this.num = num;
        }
    }
    
    private String schedule21(int [] array, int recover) {
        Map<Integer, Task> map = new HashMap<>();
        for (int i: array) {
            if (map.containsKey(i)) {
                Task t = map.get(i);
                t.num++;
                map.put(i, t);
            } else {
                map.put(i, new Task(i, 1));
            }
        }
        PriorityQueue<Task> pq = new PriorityQueue<>(11, new Comparator<Task>(){
            @Override
            public int compare(Task a, Task b){
                return b.num - a.num;
            }
        });
        for (Map.Entry<Integer, Task> entry: map.entrySet())  {
            pq.offer(entry.getValue());
        }
        StringBuilder sb = new StringBuilder();
        int time = 0;
        Queue<Task> waitingList = new LinkedList<>();
        Map<Integer, Integer> timeMap = new HashMap<>();
        while (!pq.isEmpty() || !waitingList.isEmpty()) {
            if (!pq.isEmpty()) {
                Task t = pq.poll();
                if (!timeMap.containsKey(t.type)) {
                    timeMap.put(t.type, time + 1 + recover);
                    t.num--;
                    if (t.num > 0) {
                        pq.offer(t);
                    }
                    while (!waitingList.isEmpty()) {
                        pq.offer(waitingList.poll());
                    }
                    time++;
                    sb.append(t.type + ",");
                } else {
                    int ttime = timeMap.get(t.type);
                    if (ttime <= time) {
                        timeMap.put(t.type, time + 1 + recover);
                        t.num--;
                        if (t.num > 0) {
                            pq.offer(t);
                        }
                        while (!waitingList.isEmpty()) {
                            pq.offer(waitingList.poll());
                        }
                        time++;
                        sb.append(t.type + ",");
                    } else {
                        if (t.num > 0)
                            waitingList.offer(t);
                    }
                }
            } else {
                while (!waitingList.isEmpty()) {
                    pq.offer(waitingList.poll());
                }
                time++;
                sb.append("_,");
            }
        }
        return sb.toString() + " time:" + time;
    }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:63330次
    • 积分:8106
    • 等级:
    • 排名:第2488名
    • 原创:795篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类