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 0
started at the beginning of timestamp 3
. "1:end:2"
means the function with id 1
ended 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[]);
}
}
}
最新文章
- POSIX字符集
- 嵌入式Linux驱动学习之路(一)嵌入式系统的软硬件架构
- Oracle限制某个用户的连接数及PROFILE介绍
- class打包成exe方式
- Regex Failure - Bug Fixing #2
- script加defer=";defer"; 的意义
- HTM5新增结构化元素&;非结构化元素&;新增属性详解
- windbg命令详解
- CentOS下安装JDK7
- 深入理解jQuery中的each方法
- python - 爬虫入门练习 爬取链家网二手房信息
- linux环境下mongodb启动操作
- java课堂笔记2
- Delphi: 获取控件文本宽度(像素)
- 0302-对IT行业的感思
- McAfee 与 360使用感受
- Codeforces Round #362(Div1) D Legen...(AC自动机+矩阵快速幂)
- python一些内建函数(map,zip,filter,reduce,yield等)
- 获取Windows安装日期
- 最新SQL手工注入语句&;SQL注入大全