[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- ...
随机推荐
- 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/ 本文出自方志朋的博客 一.spring ...
- Oracle EM12c 安装
EM12c系统由下列组件构成: 1.Oracle Management Agent(管理代理) 2.Oracle Management Service(管理服务) 3.Oracle Managemen ...
- 零基础Python知识点回顾(二)
开始了,继续说!字符串替换,就是预留着空间,后边再定义要填上什么,这种叫字符串格式化,其有两种方法: % 和 format %s 就是一个占位符,这个占位符可以被其它的字符串代替 >&g ...
- 使用Spring框架能带来那些好处?
1.Dependency Injection(DI)方法使得构造器和JavaBean properties文件中的依赖关系一目了然. 2.与EJB容器相比较,Ioc容器更加趋向于轻量级.这样一来Ioc ...
- vue+nodejs+express+mysql 建立一个在线网盘程序
vue+nodejs+express+mysql 建立一个在线网盘程序 目录 vue+nodejs+express+mysql 建立一个在线网盘程序 第一章 开发环境准备 1.1 开发所用工具简介 1 ...
- numpy的总结
一:基础篇 1)数值 import numpy as np np.set_printoptions(linewidth=200,suppress=True) a = np.array([1,2,3,4 ...
- Zabbix 3.x中使用Percona Monitoring Plugins监控MySQL
1.下载安装percona-zabbix-templates-1.1.7-2.noarch.rpm 下载地址:https://www.percona.com/downloads/percona-mon ...
- nignx 配置服务集群
前言:这里只是简单介绍Nginx简单APP Server集群的搭建和设置发向代理. 后续有时间我会陆续加上Nginx的基础知识.三种负载均衡的策略设置.实现算法的介绍.(最后如果有测试环境,再模拟Ng ...
- 【解决】MacOS下 Python3.7 使用 pyinstaller 打包后执行报错 Failed to execute script pyi_rth__tkinter
Fix tcl/tk libs inclusion in tkinter with Python3.7 under MacOS 使用 Pyinstaller 打包时候报错 3027 ERROR: Tc ...
- JS高级. 06 缓存、分析解决递归斐波那契数列、jQuery缓存、沙箱、函数的四种调用方式、call和apply修改函数调用方法
缓存 cache 作用就是将一些常用的数据存储起来 提升性能 cdn //-----------------分析解决递归斐波那契数列<script> //定义一个缓存数组,存储已经计算出来 ...