• FB面经 Prepare: Task Schedule


    tasks has cooldown time, give an input task id array, output finish time
    input: AABCA
    A--ABCA
    output:7
     1 package fb;
     2 
     3 import java.util.*;
     4 
     5 public class Scheduler {
     6     
     7     public int task(int[] tasks, int cooldown) {
     8         int time = 0;
     9         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    10         
    11         StringBuilder output = new StringBuilder();
    12         
    13         for (int i=0; i<tasks.length; i++) {
    14             if (!map.containsKey(tasks[i]) || time>=map.get(tasks[i])) {
    15                 map.put(tasks[i], time+cooldown);
    16                 time++;
    17                 
    18                 output.append(tasks[i]);
    19             }
    20             else { // time < map.get(tasks[i])
    21                 for (int k=time; k<map.get(tasks[i]); k++) {
    22                     output.append("-");
    23                 }
    24                 
    25                 
    26                 time = map.get(tasks[i]);
    27                 map.put(tasks[i], time+cooldown);
    28                 time++;
    29                 
    30                 
    31                 output.append(tasks[i]);
    32             }
    33         }
    34         return time;
    35     }
    36     
    37     public static void main(String[] args) {
    38         Scheduler sc = new Scheduler();
    39         //int res = sc.task(new int[]{1,1,2,3,1}, 3);
    40         int res = sc.task(new int[]{1,2,1,2,3}, 3);
    41         //String output = sc.task(new int[]{1,2,2,1,3}, 3);
    42         System.out.println(res);
    43         //System.out.println(output);
    44     }
    45 }
  • 相关阅读:
    How to configure security of ActiveMQ ?
    CentOS 搭建 nginx + tomcat
    25个 Git 进阶技巧
    写给Git初学者的7个建议
    my links
    Shell scripts to Create a local dir base on the time.
    81For全栈技术网
    一款可视化的在线制作H5
    在线制作h5
    在线制作h5——上帝的礼物
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6288326.html
Copyright © 2020-2023  润新知