636. 函数的独占时间

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

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

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

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

示例 1:

输入:

n = 2

logs =

[“0:start:0”,

“1:start:2”,

“15”,

“06”]

输出:[3, 4]

说明:

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

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

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

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

说明:

输入的日志会根据时间戳排序,而不是根据日志Id排序。

你的输出会根据函数Id排序,也就意味着你的输出数组中序号为 0 的元素相当于函数 0 的执行时间。

两个函数不会在同时开始或结束。

函数允许被递归调用,直到运行结束。

1 <= n <= 100

PS:

其实这里就是输出的时候减去输入的时候,无疑只多了一步,就是我这个输出了,那么我栈里面的那个要减少这么多,因为本该他的时间被这个输出的占用了那么多

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

Java实现 LeetCode 636 函数的独占时间(栈)的更多相关文章

  1. Leetcode 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. java向oracle数据库中插入当前时间

    public class Test{public static void main (String args []){ java.util.Date a = new java.util.Date(); ...

  5. 转载:java中Thread.sleep()函数使用

    点我跳过黑哥的卑鄙广告行为,进入正文. Java多线程系列更新中~ 正式篇: Java多线程(一) 什么是线程 Java多线程(二)关于多线程的CPU密集型和IO密集型这件事 Java多线程(三)如何 ...

  6. Java中的回调函数学习-深入浅出

    Java中的回调函数一般来说分为下面几步: 声明回调函数的统一接口interface A.包括方法callback(); 在调用类caller内将该接口设置为私有成员private A XXX; 在c ...

  7. Java for LeetCode 121 Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  8. Java for LeetCode 090 Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  9. Loadrunner时间函数、用时间生成订单编号例子

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

随机推荐

  1. repo使用总结—从入门到入门

    文章目录 1 what's repo 2 官方文档 Repo 命令参考资料 help 帮助 init 初始化 sync 同步所有项目文件 upload 提交更改 diff download foral ...

  2. Awareness Kit让你的音乐APP脱颖而出,更懂用户,也更动人心

    让你的音乐APP脱颖而出,更懂用户,也更动人心. 场景 情景感知服务能带来什么?   作为音乐发烧友,闲下来的时候总想打开App,享受沉浸在音乐中的放松.然而,App推荐的歌单经常没法满足我的需要,如 ...

  3. python读取excel所有数据(cmd界面)

    python读取excel所有数据(cmd界面) cmd界面显示excel数据 代码 import xlrd import os from prettytable import PrettyTable ...

  4. 09JAVA基础-常用类

    1.Scanner //获取键盘输入 Scanner sc = new Scanner(System.in); int num = sc.nextIn(); String str = sc.nextL ...

  5. Web_php_unserialize

    0x01 <?php class Demo { private $file = 'index.php'; public function __construct($file) { $this-& ...

  6. 我的linux学习日记day8

    链接文件 linux中链接有两种,一种是硬链接,一种是软链接 一.硬链接 硬链接是指通过索引节点来进行链接创建硬链接的方法如下:ln 源文件 硬链接文件具有相同inode号的多个文件互为硬链接文件删除 ...

  7. 手机APP自动化环境搭建

    1 摘要 近年来,随着移动应用从数量上和逻辑复杂程度上的增长,以及产品发布周期的紧缩,使得回归测试迫在眉睫,鉴于此APP自动化测试变得越来流行,当前主流的APP自动化工具有:Appium.Roboti ...

  8. 10.2 Go redis

    10.2 Go redis redis是NoSQL数据, 不是传统的关系型数据库.linux,windows环境皆可安装. https://redis.io http://www.redis.cn r ...

  9. linux DRM/KMS 测试工具 modetest、kmscude、igt-gpu-tools (二)

    kmscube   kmscube is a little demonstration program for how to drive bare metal graphics without a c ...

  10. PHP持久配置容器Yaconf

    PHP持久配置容器Yaconf的安装及使用 Yaconf介绍:Yaconf是一个配置容器,它解析ini文件,在PHP启动时将结果存储在PHP中,配置存在于整个PHP生命周期中,这使得它非常快. 要求: ...