函数的独占时间

给出一个非抢占单线程CPU的 n 个函数运行日志,找到函数的独占时间。

每个函数都有一个唯一的 Id,从 0 n-1,函数可能会递归调用或者被其他函数调用。

日志是具有以下格式的字符串:function_id:start_or_end:timestamp。例如:"0:start:0" 表示函数 0 从 0 时刻开始运行。"0:end:0" 表示函数 0 在 0 时刻结束。

函数的独占时间定义是在该方法中花费的时间,调用其他函数花费的时间不算该函数的独占时间。你需要根据函数的 Id 有序地返回每个函数的独占时间。

示例 1:

输入:

n = 2

logs =

["0:start:0",

"1:start:2",

"1:end:5",

"0:end:6"]

输出:[3, 4]

说明:

函数 0 在时刻 0 开始,在执行了 2个时间单位结束于时刻 1。

现在函数 0 调用函数 1,函数 1 在时刻 2 开始,执行 4 个时间单位后结束于时刻 5。

函数 0 再次在时刻 6 开始执行,并在时刻 6 结束运行,从而执行了 1 个时间单位。

所以函数 0 总共的执行了 2 +1 =3 个时间单位,函数 1 总共执行了 4 个时间单位。

说明:

  1. 输入的日志会根据时间戳排序,而不是根据日志Id排序。
  2. 你的输出会根据函数Id排序,也就意味着你的输出数组中序号为 0 的元素相当于函数 0 的执行时间。
  3. 两个函数不会在同时开始或结束。
  4. 函数允许被递归调用,直到运行结束。
  5. 1 <= n <= 100

 import java.util.List;
import java.util.Stack; public class Solution {
public int[] exclusiveTime(int n, List<String> logs) {
Stack<Integer> stack = new Stack<>();
int[] res = new int[n];
String[] s = logs.get(0).split(":");
stack.push(Integer.parseInt(s[0]));
int i = 1, time = Integer.parseInt(s[2]);
while (i < logs.size()) {
s = logs.get(i).split(":");
while (time < Integer.parseInt(s[2])) {
res[stack.peek()]++;
time++;
}
if (s[1].equals("start"))
stack.push(Integer.parseInt(s[0]));
else {
res[stack.peek()]++;
time++;
stack.pop();
}
i++;
}
return res;
}
}

 public class Solution {
public int[] exclusiveTime(int n, List < String > logs) {
Stack < Integer > stack = new Stack < > ();
int[] res = new int[n];
String[] s = logs.get(0).split(":");
stack.push(Integer.parseInt(s[0]));
int i = 1, prev = Integer.parseInt(s[2]);
while (i < logs.size()) {
s = logs.get(i).split(":");
if (s[1].equals("start")) {
if (!stack.isEmpty())
res[stack.peek()] += Integer.parseInt(s[2]) - prev;
stack.push(Integer.parseInt(s[0]));
prev = Integer.parseInt(s[2]);
} else {
res[stack.peek()] += Integer.parseInt(s[2]) - prev + 1;
stack.pop();
prev = Integer.parseInt(s[2]) + 1;
}
i++;
}
return res;
}
}

Leetcode 636.函数的独占时间的更多相关文章

  1. Java实现 LeetCode 636 函数的独占时间(栈)

    636. 函数的独占时间 给出一个非抢占单线程CPU的 n 个函数运行日志,找到函数的独占时间. 每个函数都有一个唯一的 Id,从 0 到 n-1,函数可能会递归调用或者被其他函数调用. 日志是具有以 ...

  2. [Swift]LeetCode636. 函数的独占时间 | 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. Loadrunner时间函数、用时间生成订单编号例子

    Loadrunner中取时间函数.用时间函数生成订单编号例子: <如要转载,请注明网络来源及作者:Cheers_Lee> 问题的提出: (1)有时候在Loadrunner中用C语言设计脚本 ...

  5. php归档函数(按时间)实现

    今日开发本站需要用到按时间归档文章的功能,即按文档发布时间将文章文类,以实现检索和统计功能,于是自己写了一个, 现分享给大家,相信大家工作和学习中有可能会用到,实现原理很简单,即取出文章发布时间戳的年 ...

  6. PHP 中 Date 函数与实际时间相差8小时的解决方法

    PHP 中的 date() 函数显示的时间是格林威治时间,和北京时间正好相差8个小时,其他时间相关的函数,如 strtotime() 也有相同的问题,同样可以通过下面的方法来解决: 1. 修改php. ...

  7. 各个函数消耗的时间profiling和内存泄漏valgrind

    来源:http://06110120wxc.blog.163.com/blog/static/37788161201333112445844/ ARM(hisi)上面的profiling和valgri ...

  8. MySQL字符串函数、日期时间函数

    MySQL字符串函数.日期时间函数 一.常见字符串函数: 1.CHAR_LENGTH  获取长度(字符为单位) 2.FORMAT  格式化 3.INSERT  替换的方式插入 4.INSTR  获取位 ...

  9. Loadrunner 中时间戳函数 web_save_timestamp_param(时间返回数值)

    web_save_timestamp_param("tStamp", LAST); lr_output_message("Moon1:%s",lr_eval_s ...

随机推荐

  1. yum安装软件并保留下载的软件

    使用yum插件downloadonly下载安装软件需要的依赖包并保留到指定的文件 安装yum-downloadonly或 yum-plugin-downloadonly 软件包. yum instal ...

  2. pyinstaller打包python源程序访问hive

    1.需求 使用hvie server一段时间后,业务部门需要自己不定时的查询业务数据,之前这一块都是他们提需求我们来做,后来发现这样重复一样的工作放在我们这边做是在没有效率,遂提出给他们工具或者web ...

  3. Codeforces Round #327 (Div. 2) B Rebranding(映射)

    O(1)变换映射,最后一次性替换. #include<bits/stdc++.h> using namespace std; typedef long long ll; ; char s[ ...

  4. 浅谈前端性能优化(二)——对HTTP传输进行压缩

    1.前端性能优化的一点: 对js.css.图片等进行压缩,尽可能减小文件的大小,减少文件下载的时间,从而减少网页响应的时间. 2.前端性能优化的另一点: 对HTTP传输进行压缩,即在js,css.图片 ...

  5. 前端三大框架 Vue.js、AngularJS、React 的区别

    Vue.js Vue.js 是一种构建数据驱动的Web界面的渐进式框架,Vue.js 采用自底向上增量开发的设计. Vue.js 轻量高效,数据双向绑定(响应式数据绑定), 它会自动响应数据的变化情况 ...

  6. gearmand 编译 could not find gperf

    安装步骤: #wget https://launchpad.net/gearmand/1.2/1.1.8/+download/gearmand-1.1.8.tar.gz #tar zxvf gearm ...

  7. 问题 B: Curriculum Vitae

    问题 B: Curriculum Vitae 时间限制: 1 Sec  内存限制: 128 MB提交: 109  解决: 25[提交][状态][讨论版][命题人:acm4302] 题目描述 Hideo ...

  8. 2017.12.4 JavaWeb中EL表达式的运用

    <%@ page contentType="text/html; charset=gb2312"%> <html> <head> <tit ...

  9. OneNote- 面经

  10. 关于小程序 scroll-view中设置scroll-top无效 和小说图书阅读进度条小案例

    在最近的项目有做到关于小说阅读的进度条功能,其中用到scroll-view和slider组件,发现scroll-view中的scroll-top在设置值后无效,出现这种情况大概是以下几种问题: 1.s ...