• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

FB面经prepare: Task Schedule

每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间。

用HashMap保存每一个task的下一次可以开始执行的最早时间

 1 package TaskSchedule;
 2 import java.util.*;
 3 
 4 public class Solution {
 5     public int schedule(int[] str, int recover) {
 6         if (str==null || str.length==0) return 0;
 7         if (recover == 0) return str.length;
 8         int pos = 0;
 9         int time = 0;
10         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
11         for (; pos<str.length; pos++) {
12             int cur = str[pos];
13             if (!map.containsKey(cur)) {
14                 map.put(cur, time+recover+1);
15             }
16             else {
17                 int lastApr = map.get(cur);
18                 if (time >= lastApr) {
19                     map.put(cur, time);
20                 }
21                 else {
22                     pos--;
23                 }
24             }
25             time++;
26         }
27         return time;
28     }
29 
30     /**
31      * @param args
32      */
33     public static void main(String[] args) {
34         // TODO Auto-generated method stub
35         Solution sol = new Solution();
36         System.out.println(sol.schedule(new int[]{1, 2, 3, 1, 2, 3}, 3));
37 
38     }
39 
40 }

 

posted @ 2016-01-11 07:09  neverlandly  阅读(1633)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3