Exclusive Time of Functions
On a single threaded CPU, we execute some functions. Each function has a unique id between 0 and N-1.
We store logs in timestamp order that describe when a function is entered or exited.
Each log is a string with this format: "{function_id}:{"start" | "end"}:{timestamp}". For example, "0:start:3" means the function with id 0started at the beginning of timestamp 3. "1:end:2" means the function with id 1ended at the end of timestamp 2.
A function's exclusive time is the number of units of time spent in this function. Note that this does not include any recursive calls to child functions.
The CPU is single threaded which means that only one function is being executed at a given time unit.
Return the exclusive time of each function, sorted by their function id.
分析:
因为开始和结束永远是一对的。而且如果当前是结束的话,之前一个一定是开始。所以,我们用stack来存之前的log,拿到end就pop,然后更新当前function的执行时间以及调用这个function
的执行时间。
class Solution {
public int[] exclusiveTime(int n, List<String> logs) {
Stack<Log> stack = new Stack<>();
int[] result = new int[n];
for (String content : logs) {
Log log = new Log(content);
if (log.isStart) {
stack.push(log);
} else {
Log top = stack.pop();
int runningTime = log.time - top.time + ;
result[top.id] += runningTime;
if (!stack.isEmpty()) {
result[stack.peek().id] -= runningTime;
}
}
}
return result;
}
public static class Log {
public int id;
public boolean isStart;
public int time;
public Log(String content) {
String[] strs = content.split(":");
id = Integer.valueOf(strs[]);
isStart = strs[].equals("start");
time = Integer.valueOf(strs[]);
}
}
}
Exclusive Time of Functions的更多相关文章
- Leetcode 之 Exclusive Time of Functions
636. Exclusive Time of Functions 1.Problem Given the running logs of n functions that are executed i ...
- [LeetCode] Exclusive Time of Functions 函数的独家时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- [Swift]LeetCode636. 函数的独占时间 | Exclusive Time of Functions
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- [leetcode]636. Exclusive Time of Functions函数独占时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- 636. Exclusive Time of Functions 进程的执行时间
[抄题]: Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU ...
- [LeetCode] 636. Exclusive Time of Functions 函数的独家时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- 【LeetCode】636. Exclusive Time of Functions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 模拟函数调用 Simulation Exclusive Time of Functions
2018-04-28 14:10:33 问题描述: 问题求解: 个人觉得这是一条很好的模拟题,题目大意就是给了一个单线程的处理器,在处理器上跑一个函数,但是函数里存在调用关系,可以是调用其他函数,也可 ...
- 636. Exclusive Time of Functions
// TODO: need improve!!! class Log { public: int id; bool start; int timestamp; int comp; // compasa ...
随机推荐
- LYK loves music
Description LYK喜欢听音乐,它的歌单里共有n首音乐,而且它每次听音乐时都是连续地听m首, 它甚至能记得自己给每首音乐的评分ai. 现在它想选择一首歌开始听,使得接下来连续m首歌的评分&l ...
- 数位dp入门(内容一样,新版格式)
顾名思义,数位dp,是一种用来计数的dp,就是把一个数字拆成一个一个数位去统计 如果现在给你一道题,需要你求在区间[l,r]内满足条件的解的个数,我们很容易想到去暴力枚举,但要是数据范围太大这种办法就 ...
- Comet OJ - Contest #3 D 可爱的菜菜子 线段树+线性基
题意 给你一个长度为 \(n\) 的整数序列 \(a_1, a_2, \ldots, a_n\),你需要实现以下两种操作,每个操作都可以用四个整数 \(opt\ l\ r\ v\) 来表示: \(op ...
- linux_svn命令操作
转至元数据起始 linux下svn命令大全 1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录)例如:svn checkout svn://192. ...
- python中的定时器threading.Timer
由浅入深学SQL Server 2012 --> python开发中用到,定时操作.例如每隔1s执行一次,发现 threading.Timer,这个东西,可以直接用. 其原理为执行函数中置定时 ...
- oracle报错:The Network Adapter could not establish the connection的解决
进入oracle安装目录\product\11.2.0\dbhome_1\NETWORK\ADMIN,修改listener.ora和tnsnames.ora,修改host 我的网络IP为192.168 ...
- 【黑马JavaSE】1.2.算术\赋值\比较\逻辑\三元运算符、方法入门、JShell编译器
文章目录 1_1_6_05_算术运算符_四则与取模运算 1_1_6_06_算术运算符_加号的多种 1_1_6_07_算术运算符_自增自减运算 1_1_6_08_赋值运算符 这里挺关键的,为什么一个by ...
- springboot2.0数据制作为excel表格
注意:由于公司需要大量导出数据成excel表格,因此在网上找了方法,亲测有效. 声明:该博客参考于https://blog.csdn.net/long530439142/article/details ...
- layui时间控件选择时间范围
layui.use([ 'laydate'], function(){ var $ = layui.$; var laydate = layui.laydate; var max = ${nowYea ...
- 阶段3 3.SpringMVC·_04.SpringMVC返回值类型及响应数据类型_4 响应之返回值是ModelAndView类型
ModelAndView是SpringMvc提供的一个对象 ModelAndView底层源码用也是ModelMap.ModelMap实现过Model的接口 ModelAndView可以直接new出来. ...