636. Exclusive Time of Functions

1.Problem

Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find the exclusive time of these functions.

Each function has a unique id, start from 0 to n-1. A function may be called recursively or by another function.

A log is a string has this format : function_id:start_or_end:timestamp. For example, "0:start:0" means function 0 starts from the very beginning of time 0. "0:end:0" means function 0 ends to the very end of time 0.

Exclusive time of a function is defined as the time spent within this function, the time spent by calling other functions should not be considered as this function's exclusive time. You should return the exclusive time of each function sorted by their function id.

Example 1:

Input:
n = 2
logs =
["0:start:0",
"1:start:2",
"1:end:5",
"0:end:6"]
Output:[3, 4]
Explanation:
Function 0 starts at time 0, then it executes 2 units of time and reaches the end of time 1.
Now function 0 calls function 1, function 1 starts at time 2, executes 4 units of time and end at time 5.
Function 0 is running again at time 6, and also end at the time 6, thus executes 1 unit of time.
So function 0 totally execute 2 + 1 = 3 units of time, and function 1 totally execute 4 units of time.

Note:

  1. Input logs will be sorted by timestamp, NOT log id.
  2. Your output should be sorted by function id, which means the 0th
    element of your output corresponds to the exclusive time of function 0.
  3. Two functions won't start or end at the same time.
  4. Functions could be called recursively, and will always end.
  5. 1 <= n <= 100

2.Solution

logs数组中的一条数据代表:functionID + type + timesliceID ,stack中存functionID,因为函数是递归调用的,完全可以用栈来模拟。prev用来标记上一个函数开始执行时的时间片ID(timesliceID)
  遍历logs(List),对取出的每一个字符串进行分割成functionID ,type 和 timesliceID
IF type == “start”
       IF 栈不空
          timesliceID - prev 累加到栈顶元素的执行时间上去。
       将functionID 压栈;
       更新prev = timesliceID;
ELSE type == “end”
       弹栈,取得一个functionID,更新这个functionID的执行时间为 (累加) timesliceID - prev + 1; (为什么 + 1 ,因为 end指令 那一个时间片函数依然再执行,也是函数执行的最后一个时间片)
       prev = timesliceID + 1; 同样的道理,这个timeliceID结束后,才开始执行别的函数

3.Code

class Solution {
public int[] exclusiveTime(int n, List<String> logs) {
Stack<Integer> stack = new Stack<>();
//Initialize
int[] res = new int[n];
int prevous = 0;
for ( String s : logs ) {
String[] ss = s.split(":");
if ( ss[1].equals("start") ) {
if ( !stack.empty() ) {
res[stack.peek()] += Integer.parseInt(ss[2]) - prevous;
}
stack.push(Integer.parseInt(ss[0]));
prevous = Integer.parseInt(ss[2]);
} else {
res[Integer.parseInt(ss[0])] += Integer.parseInt(ss[2]) - prevous + 1;
stack.pop();
prevous = Integer.parseInt(ss[2]) + 1;
}
}
return res;
}
}

4.提交Leetcode的代码

Leetcode 之 Exclusive Time of Functions的更多相关文章

  1. [LeetCode] 636. Exclusive Time of Functions 函数的独家时间

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  2. [leetcode]636. Exclusive Time of Functions函数独占时间

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  3. [LeetCode] Exclusive Time of Functions 函数的独家时间

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  4. 【LeetCode】636. Exclusive Time of Functions 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  5. 【leetcode】636. Exclusive Time of Functions

    题目如下: 解题思路:本题和括号匹配问题有点像,用栈比较适合.一个元素入栈前,如果自己的状态是“start”,则直接入栈:如果是end则判断和栈顶的元素是否id相同并且状态是“start”,如果满足这 ...

  6. [Swift]LeetCode636. 函数的独占时间 | Exclusive Time of Functions

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...

  7. 636. Exclusive Time of Functions 进程的执行时间

    [抄题]: Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU ...

  8. Exclusive Time of Functions

    On a single threaded CPU, we execute some functions.  Each function has a unique id between 0 and N- ...

  9. 模拟函数调用 Simulation Exclusive Time of Functions

    2018-04-28 14:10:33 问题描述: 问题求解: 个人觉得这是一条很好的模拟题,题目大意就是给了一个单线程的处理器,在处理器上跑一个函数,但是函数里存在调用关系,可以是调用其他函数,也可 ...

随机推荐

  1. 在元素的装载数量明确的时候HashMap的大小应该如何选择。

    今天看到美团招聘给出了一道小题目,关于HashMap的性能问题.问题如下: java hashmap,如果确定只装载100个元素,new HashMap(?)多少是最佳的,why? 要回答这个问题,首 ...

  2. 【问题记录】javaweb项目的jar无法识别注解的bean

    问题:eclipse中javaweb项目,打成jar包供其它项目使用,发现无法识别使用注解的bean. 原因参考: http://blog.csdn.net/wangpeng047/article/d ...

  3. 基于ffmpeg 直播推流和播放rtmp (IOS源码)

    ios直播推流每秒能达到30帧,比安卓要强,视频采用软编码的话手机会发烫,得采用码编码,播放视频采用opengl渲染. ffmpeg初始化代码如下: int init_Code(int width, ...

  4. hdu3879 Base Station 最大权闭合子图 边权有正有负

    /** 题目:hdu3879 Base Station 最大权闭合子图 边权有正有负 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 题意:给出n个 ...

  5. 如何执行静默(无人参与)Java 安装?

    http://www.java.com/zh_CN/download/help/silent_install.xml

  6. va_list中的_vsntprintf使用介绍

    相信大家都用过sprintf这个函数,就是下面这样: int sprintf( char *buffer, const char *format [, argument] ... ); 在之前看到了用 ...

  7. javaEE面试重点

    Hibernate工作原理及为什么要用? 原理: 1. 读取并解析配置文件 2. 读取并解析映射信息.创建SessionFactory 3. 打开Sesssion 4. 创建事务Transation ...

  8. log4j详解(转)

    转载自:http://blog.csdn.net/evatian/article/details/8501517 Log4j – 如何配置多个logger? 分类: java2013-01-14 16 ...

  9. openwrt U盘启动

    参考链接: http://m.blog.csdn.net/blog/zcynical/44892785

  10. sql 注入入门

    =============安全性篇目录============== 毕业开始从事winfrm到今年转到 web ,在码农届已经足足混了快接近3年了,但是对安全方面的知识依旧薄弱,事实上是没机会接触相关 ...