[leetcode-636-Exclusive Time of Functions]
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find the exclusive time of these functions.
Each function has a unique id, start from 0 to n-1. A function may be called recursively or by another function.
A log is a string has this format : function_id:start_or_end:timestamp
. For example, "0:start:0"
means function 0 starts from the very beginning of time 0. "0:end:0"
means function 0 ends to the very end of time 0.
Exclusive time of a function is defined as the time spent within this function, the time spent by calling other functions should not be considered as this function's exclusive time. You should return the exclusive time of each function sorted by their function id.
Example 1:
Input:
n = 2
logs =
["0:start:0",
"1:start:2",
"1:end:5",
"0:end:6"]
Output:[3, 4]
Explanation:
Function 0 starts at time 0, then it executes 2 units of time and reaches the end of time 1.
Now function 0 calls function 1, function 1 starts at time 2, executes 4 units of time and end at time 5.
Function 0 is running again at time 6, and also end at the time 6, thus executes 1 unit of time.
So function 0 totally execute 2 + 1 = 3 units of time, and function 1 totally execute 4 units of time.
Note:
- Input logs will be sorted by timestamp, NOT log id.
- Your output should be sorted by function id, which means the 0th element of your output corresponds to the exclusive time of function 0.
- Two functions won't start or end at the same time.
- Functions could be called recursively, and will always end.
- 1 <= n <= 100
思路:
用一个数组记录当前id的时间,使用一个栈来保存当前的id,类似于后进先出,
如果当前id为end,那么栈顶一定也是相同的id,更新数组里面相应id的时间。
如果当前id为start,那么先更新上一个栈顶的id的时间,再将此id入栈。
最后保存当前的time,留作下一次更新。
vector<int> exclusiveTime(int n, vector<string>& logs)
{
vector<int>ret(n), sta;
int id, time, last;
for (auto log:logs)
{
for (auto& c : log) if (c == ':')c = ' ';
stringstream ss(log);
char s[];
ss >> id >> s >> time;
//sscanf(log.c_str(), "%d:%[^:]:%d", &id, s, &time);
//cout << id << " " << s << " " << time<<endl;
if (s[]=='s')
{
if (sta.size() > )ret[sta.back()] += time - last;
sta.push_back(id);
}
else
{
ret[sta.back()] += ++time - last;
sta.pop_back();
}
last = time;
}
return ret;
}
[leetcode-636-Exclusive Time of Functions]的更多相关文章
- [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函数独占时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- Leetcode 之 Exclusive Time of Functions
636. Exclusive Time of Functions 1.Problem Given the running logs of n functions that are executed i ...
- 【LeetCode】636. Exclusive Time of Functions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 【leetcode】636. Exclusive Time of Functions
题目如下: 解题思路:本题和括号匹配问题有点像,用栈比较适合.一个元素入栈前,如果自己的状态是“start”,则直接入栈:如果是end则判断和栈顶的元素是否id相同并且状态是“start”,如果满足这 ...
- 636. Exclusive Time of Functions 进程的执行时间
[抄题]: Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU ...
- 636. Exclusive Time of Functions
// TODO: need improve!!! class Log { public: int id; bool start; int timestamp; int comp; // compasa ...
- [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 ...
- Exclusive Time of Functions
On a single threaded CPU, we execute some functions. Each function has a unique id between 0 and N- ...
随机推荐
- Entity Framework 二
本篇主要介绍:创建了实体数据模型,生成了那些文件以及其代表意义 创建实体数据模型 上一篇的最后,我们创建了数据库,现在我们利用数据库来生成我们的实体数据模型,这种形式我们称为数据库优先,后面会介绍代码 ...
- 关于python的GIL
转自依云在知乎上的回答,链接为https://www.zhihu.com/question/27245271/answer/462975593 侵删. python的多线程,其实不是真的多线程,它会通 ...
- 亚马逊VPS
添加用户: 第一步 注册亚马逊云(已注册直接进入第二步)1.开始前需要准备好:有外币支付功能的信用卡一张(注册需要,不扣钱).Email地址.电话(手机更方便一些)2.访问 https://amazo ...
- 【ospf-路由聚合】
- vue组件中的样式属性--scoped
Scoped CSS Scoped CSS规范是Web组件产生不污染其他组件,也不被其他组件污染的CSS规范. vue组件中的style标签标有scoped属性时表明style里的css样式只适用于当 ...
- js判断是否为数字
function isNumber(value) { var patrn = /^(-)?\d+(\.\d+)?$/; if (patrn.exec(value) == null || value = ...
- php post提交xml文件
<?php header("Content-type: text/xml;"); // xml code demo $xmlData = '<?xml version= ...
- (数据科学学习手札31)基于Python的网络数据采集(初级篇)
一.简介 在实际的业务中,我们手头的数据往往难以满足需求,这时我们就需要利用互联网上的资源来获取更多的补充数据,但是很多情况下,有价值的数据往往是没有提供源文件的直接下载渠道的(即所谓的API),这时 ...
- [BZOJ2809][Apio2012]dispatching(左偏树)
首先对于一个节点以及它的子树,它的最优方案显然是子树下选最小的几个 用左偏树维护出每棵子树最优方案的节点,记录答案 然后它的这棵树可以向上转移给父节点,将所有子节点的左偏树合并再维护就是父节点的最优方 ...
- 数列分块入门 1 LOJ6277
题目描述 给出一个长为 n 的数列,以及 n 个操作,操作涉及区间加法,单点查值. 输入格式 第一行输入一个数字 n. 第二行输入 n 个数字,第 iii 个数字为 ai,以空格隔开. 接下来输 ...