123
返回列表 发新帖
楼主: tong-1324
跳转到指定楼层
上一主题 下一主题
收起左侧

Snapchat全职跪经

🔗
pingfanzhilu 2016-8-31 10:43:49 | 只看该作者
全局:
贴个2 pass的解法吧(the second pass is merge) 有1 pass的吗?

  1. import java.util.*;

  2. class Interval {
  3.     public int left;
  4.     public int right;

  5.     public Interval(int l, int r) {
  6.         this.left = l;
  7.         this.right = r;
  8.     }
  9. }

  10. public class Solution {
  11.     public static void main(String[] args) {
  12.         List<String[]> input = new ArrayList();
  13.         input.add(new String[]{"f1", "start", "1"});
  14.         input.add(new String[]{"f1", "start", "2"});
  15.         input.add(new String[]{"f2", "start", "4"});
  16.         input.add(new String[]{"f2", "end", "8"});
  17.         input.add(new String[]{"f1", "end", "16"});
  18.         input.add(new String[]{"f1", "end", "32"});
  19.         input.add(new String[]{"f3", "start", "64"});
  20.         input.add(new String[]{"f3", "end", "128"});

  21.         Solution sol = new Solution();
  22.         Map<String, List<Interval>> result = sol.solve(input);
  23.         for (String k : result.keySet()) {
  24.             System.out.print(k + " : ");
  25.             for (Interval itv : result.get(k)) {
  26.                 System.out.print("[" + itv.left + ", " + itv.right + "]");
  27.             }
  28.             System.out.println();
  29.         }
  30.     }

  31.     public Map<String, List<Interval>> solve(List<String[]> input) {
  32.         Map<String, List<Interval>> result = new HashMap();
  33.         String[] prev = null;
  34.         for (String[] current : input) {
  35.             if (current[1].equals("start")) {
  36.                 if (prev != null && prev[1].equals("start")) {
  37.                     if (!result.containsKey(prev[0])) {
  38.                         result.put(prev[0], new ArrayList<>());
  39.                     }

  40.                     result.get(prev[0]).add(new Interval(Integer.valueOf(prev[2]), Integer.valueOf(current[2])));
  41.                 }
  42.             } else { // end
  43.                 if (!result.containsKey(prev[0])) {
  44.                     result.put(current[0], new ArrayList<>());
  45.                 }

  46.                 result.get(current[0]).add(new Interval(Integer.valueOf(prev[2]), Integer.valueOf(current[2])));
  47.             }

  48.             prev = current;
  49.         }

  50.         for (String key : result.keySet()) {
  51.             result.put(key, merge(result.get(key)));
  52.         }

  53.         return result;
  54.     }

  55.     private List<Interval> merge(List<Interval> list) {
  56.         Stack<Interval> stack = new Stack();
  57.         for (Interval itv : list) {
  58.             if (stack.isEmpty()) {
  59.                 stack.push(itv);
  60.             } else {
  61.                 if (stack.peek().right < itv.left) {
  62.                     stack.push(itv);
  63.                 } else {
  64.                     Interval top = stack.pop();
  65.                     stack.push(new Interval(Math.min(top.left, itv.left), Math.max(top.right, itv.right)));
  66.                 }
  67.             }
  68.         }

  69.         List<Interval> ret = new ArrayList();
  70.         while (!stack.isEmpty()) {
  71.             ret.add(stack.pop());
  72.         }

  73.         return ret;
  74.     }
  75. }
复制代码
回复

使用道具 举报

全局:
所以需要merge么?
回复

使用道具 举报

🔗
laonong15 2016-9-3 07:55:46 | 只看该作者
全局:
写了个半伪码的code  懒得写map 操作  抛砖:
回复

使用道具 举报

🔗
laonong15 2016-9-3 07:56:21 | 只看该作者
全局:
laonong15 发表于 2016-9-3 07:55
写了个半伪码的code  懒得写map 操作  抛砖:

public Map<functioname,List<Interval> parseLog(List<Log> list) {
   //coner casese
   Map<functionanme,List<Interval> result = new HashMap<>();
   Stack<Log>  stack = new Stack<>();
   int intervalStart = 0;
   for(Log log:list){
         
       if(stack.isEmpty()){
           stack.push(log)
       } else {
           Log top = stack.top();
           if(top.functioname == log.functioname){
                   if(log.startorend is end){
                     Interval intvl = new Interval(top.timestamp,log.timestamp);
                     add intvl to the map;
                     stack.pop();
                   } else {
                       stack.push(log);
                   }
            } else {
                 Interval intvl = new Interval(top.timestamp,log.timestamp);
                     add intvl to the map by the key is top.functioname;
            }
       }
       intervalStrat = log.timestamp;
      }
      reutrn result;
      }
回复

使用道具 举报

🔗
importcoder 2016-9-8 19:32:36 | 只看该作者
全局:
我也只能想到先把相同function的interval放到map里,然后依次merge。

  1. //        assuming following situation will never occur:

  2. //        f1 start 1
  3. //        f2 start 4
  4. //        f1 end 7
  5. //        f2 end 10

  6. //        meaning if there is an "end", the previous must be a "start" with same ID


  7. public class LogParsing {
  8.        
  9.         public Map<String, List<List<Integer>>> logParser(String[] log) {
  10.                
  11.                 Map<String, List<List<Integer>>> result = new HashMap<>();
  12.                 Stack<String[]> stack = new Stack<>();
  13.                
  14.                 // put cpu occupied period for each function
  15.                 for (String line: log) {
  16.                        
  17.                         String[] cur = line.split("\t");
  18.                         result.putIfAbsent(cur[0], new ArrayList<>());
  19.                        
  20.                         if (stack.isEmpty()) {
  21.                                 stack.push(cur);
  22.                                 continue;
  23.                         }
  24.                        
  25.                         String[] pre = stack.peek();
  26.                         int start = Integer.parseInt(pre[2]);
  27.                         int end = Integer.parseInt(cur[2]);
  28.                         List<Integer> interval = Arrays.asList(start, end);
  29.                         result.get(pre[0]).add(interval);
  30.                        
  31.                         if (cur[1].equals("start")) {
  32.                                 stack.push(cur);
  33.                         } else if (cur[1].equals("end")) {
  34.                                 stack.pop();
  35.                                 if (!stack.isEmpty()) {
  36.                                         stack.peek()[2] = cur[2];
  37.                                 }
  38.                         }
  39.                        
  40.                 }
  41.                
  42.                 // merge occupied time lists
  43.                 for (String id: result.keySet()) {
  44.                         List<List<Integer>> list = mergeIntervals(result.get(id));
  45.                         result.put(id, list);
  46.                 }
  47.                
  48.                 return result;
  49.         }
  50.        
  51.         private List<List<Integer>> mergeIntervals(List<List<Integer>> intervals) {
  52.                
  53.                 List<List<Integer>> result = new ArrayList<>();
  54.                 List<Integer> pre = intervals.get(0);
  55.                
  56.                 for (int i = 1; i < intervals.size(); i++) {
  57.                         List<Integer> cur = intervals.get(i);
  58.                         if (overlap(pre, cur)) {
  59.                                 List<Integer> interval = Arrays.asList(Math.min(pre.get(0), cur.get(0)), Math.max(pre.get(1), cur.get(1)));
  60.                                 pre = interval;
  61.                         } else {
  62.                                 result.add(pre);
  63.                                 pre = cur;
  64.                         }
  65.                 }
  66.                 result.add(pre);
  67.                
  68.                 return result;
  69.         }
  70.        
  71.         private boolean overlap(List<Integer> pre, List<Integer> cur) {
  72.                 return pre.get(1) >= cur.get(0);
  73.         }
  74.        
  75.         public static void main(String[] args) {
  76.                
  77.                 LogParsing solution = new LogParsing();
  78.                
  79.                 String[] log = {"f1\tstart\t1", "f1\tstart\t2", "f2\tstart\t4", "f2\tend\t8", "f1\tend\t16", "f1\tend\t32", "f3\tstart\t64", "f3\tend\t128"};
  80.                
  81.                 System.out.println(solution.logParser(log));
  82.                
  83.         }
  84.        
  85.         public static void print(String[] log) {
  86.                 for (String s: log) {
  87.                         System.out.println(s);
  88.                 }
  89.         }

  90. }
复制代码
回复

使用道具 举报

🔗
eboyme 2016-10-2 06:39:28 | 只看该作者
全局:

1 pass。大概写了一下。感觉 这个应当假定输入总是合法的。比如f1 start 1, f2 start 2, f1 end 4这种情况 不应该存在,否则CPU在时间[2,4]内的运行状态是不确定的。

  1. import java.util.*;
  2. class Log{
  3.         String program;
  4.         String status;
  5.         int timeStamp;
  6.        
  7.         public Log(String program, String status, int timeStamp){
  8.                 this.program = program;
  9.                 this.status = status;
  10.                 this.timeStamp = timeStamp;
  11.         }
  12. }

  13. public class FunctionLogParsing {
  14.        
  15.         public static Map<String, List<int[]>> parsingLogFile(Log[] file){
  16.                 Map<String, List<int[]>> res = new HashMap<String, List<int[]>>();
  17.                
  18.                 if (file == null || file.length == 0)
  19.                         return res;
  20.                
  21.                 Log prev = file[0];
  22.                 int start = prev.timeStamp;
  23.                 int end = -1;
  24.                 for (int i = 1; i < file.length; i++){
  25.                         Log curr = file[i];
  26.                        
  27.                         if (curr.status.equals("start")){
  28.                                 if (curr.program.equals(prev.program)){
  29.                                         if (prev.status.equals("end")){
  30.                                                 addRes(start, end, res, prev);
  31.                                         }
  32.                                 }
  33.                                 else{
  34.                                         if (prev.status.equals("start")){
  35.                                                 end = curr.timeStamp;
  36.                                         }
  37.                                        
  38.                                         addRes(start, end, res, prev);
  39.                                         start = curr.timeStamp;
  40.                                         end = -1;
  41.                                         prev = curr;
  42.                                 }
  43.                         }
  44.                         else {
  45.                                 if (curr.program.equals(prev.program)){
  46.                                         end = curr.timeStamp;
  47.                                 }
  48.                                 else{
  49.                                         addRes(start, end, res, prev);
  50.                                         start = end;
  51.                                         end = -1;
  52.                                         prev = curr;
  53.                                 }
  54.                         }
  55.                        
  56.                 }
  57.                
  58.                 addRes(start, end, res, prev);
  59.                
  60.                 return res;
  61.         }
  62.        
  63.         private static void addRes(int start, int end, Map<String, List<int[]>> res, Log prev){
  64.                 int[] interval = {start, end};
  65.                 if (!res.containsKey(prev.program)){
  66.                         res.put(prev.program, new ArrayList<int[]>());
  67.                 }
  68.                 res.get(prev.program).add(interval);
  69.         }

  70.         public static void main(String[] args) {
  71.                 // TODO Auto-generated method stub
  72.                 Log[] logs = {
  73.                                 new Log("f1", "start", 1),
  74.                                 new Log("f1", "start", 2),
  75.                                 new Log("f2", "start", 4),
  76.                                 new Log("f2", "end", 8),
  77.                                 new Log("f1", "end", 16),
  78.                                 new Log("f1", "end", 32),
  79.                                 new Log("f3", "start", 64),
  80.                                 new Log("f3", "end", 128),
  81.                 };
  82.                
  83.                 Map<String, List<int[]>> map = parsingLogFile(logs);
  84.                 for (String key: map.keySet()){
  85.                         System.out.print(key+": ");
  86.                         for (int[] pair: map.get(key)){
  87.                                 System.out.print("[" + pair[0] + "," + pair[1] + "] ");
  88.                         }
  89.                         System.out.println();
  90.                 }
  91.         }

  92. }
复制代码
回复

使用道具 举报

🔗
freemail165 2016-10-13 14:16:08 | 只看该作者
全局:
eboyme 发表于 2016-10-2 06:39
1 pass。大概写了一下。感觉 这个应当假定输入总是合法的。比如f1 start 1, f2 start 2, f1 end 4这种情 ...

为什么不确定

既然单线程,当f2 start 2, we should get f1[1,2] f1 won't run
then f1 end 4 , we should get f2[2,4]
回复

使用道具 举报

🔗
sophie729 2016-10-13 14:47:08 | 只看该作者
全局:
楼主好赞! 希望加面通过。
想咨询下 onsite 调环境的时候是在自己电脑还是公司给安排一个电脑呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表