首页 > 其他 > 详细

FB面经 Prepare: Task Schedule

时间:2017-01-16 07:27:01      阅读:233      评论:0      收藏:0      [点我收藏+]
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 }

 

FB面经 Prepare: Task Schedule

原文:http://www.cnblogs.com/EdwardLiu/p/6288326.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!