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 ...
随机推荐
- E. Compress Words(Hash,KMP)
E. Compress Words time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- fanout(Publish/Subscribe)发布/订阅
引言 它是一种通过广播方式发送消息的路由器,所有和exchange建立的绑定关系的队列都会接收到消息 不处理路由键,只需要简单的将队列绑定到交换机上 fanout交换机转发消息是最快的,它不需要做路由 ...
- Android学习_Fragment
Fragment 使用Fragment 我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理.从而可以更加方便的在运行过程中动态地更新Activity的用户界面.另外Fragment并不能单独 ...
- easyui编辑editor
$.extend($.fn.datagrid.defaults.editors, { textarea: { init: function(container, options){ var input ...
- 三、Reids(高性能)key-value服务器知识整合
一.Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. 知识链接:https://www.runoob.com/redis/redis-backup.html ht ...
- spring boot 下 mapper接口与xml文件映射问题
1. @MapperScan @MapperScan("com.streamax.ums.business.dao") 注解扫描的包路径是否有问题 2. 目录结构 mapper接口 ...
- wps技巧
1.页面颜色设置页面布局->背景 2.图片等比例缩放按住shift 3.想任意挪动图片 设置为文字环绕型 4.word由分节符构成.可以看左下角节的显示.一个页可以划分多节.页总数<=节总 ...
- Linux 特殊权限
Linux 有三个高级权限suid,sgid,sticky. 1.suid suid 属性只能运用在可执行文件上,含义是开放文件所有者的权限给其他用户,即当用户执行该执行文件时,会拥有该执行文件所有者 ...
- Mac平台最好用的万能开源免费播放器-IINA
1.安装 1)官网下载地址 https://iina.io/ 2)brew 方式安装 testdeMacBook-Pro:~ test$ brew cask install iina Updating ...
- JvmOverloads kotlin(14)(转)
在Kotlin中@JvmOverloads注解的作用就是:在有默认参数值的方法中使用@JvmOverloads注解,则Kotlin就会暴露多个重载方法.可能还是云里雾里,直接上代码,代码解释一切:如果 ...