当前位置:网站首页>leetcode-636:函数的独占时间
leetcode-636:函数的独占时间
2022-08-08 10:56:00 【菊头蝙蝠】
题目
题目连接
有一个 单线程 CPU 正在运行一个含有 n 道函数的程序。每道函数都有一个位于 0 和 n-1 之间的唯一标识符。
函数调用 存储在一个 调用栈 上 :当一个函数调用开始时,它的标识符将会推入栈中。而当一个函数调用结束时,它的标识符将会从栈中弹出。标识符位于栈顶的函数是 当前正在执行的函数 。每当一个函数开始或者结束时,将会记录一条日志,包括函数标识符、是开始还是结束、以及相应的时间戳。
给你一个由日志组成的列表 logs ,其中 logs[i] 表示第 i 条日志消息,该消息是一个按 “{function_id}:{“start” | “end”}:{timestamp}” 进行格式化的字符串。例如,“0:start:3” 意味着标识符为 0 的函数调用在时间戳 3 的 起始开始执行 ;而 "1:end:2
" 意味着标识符为 1 的函数调用在时间戳 2 的 末尾结束执行。注意,函数可以 调用多次,可能存在递归调用 。
函数的 独占时间 定义是在这个函数在程序所有函数调用中执行时间的总和,调用其他函数花费的时间不算该函数的独占时间。例如,如果一个函数被调用两次,一次调用执行 2 单位时间,另一次调用执行 1 单位时间,那么该函数的 独占时间 为 2 + 1 = 3 。
以数组形式返回每个函数的 独占时间 ,其中第 i 个下标对应的值表示标识符 i 的函数的独占时间。
示例 1:
输入:n = 2, logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]
输出:[3,4]
解释:
函数 0 在时间戳 0 的起始开始执行,执行 2 个单位时间,于时间戳 1 的末尾结束执行。
函数 1 在时间戳 2 的起始开始执行,执行 4 个单位时间,于时间戳 5 的末尾结束执行。
函数 0 在时间戳 6 的开始恢复执行,执行 1 个单位时间。
所以函数 0 总共执行 2 + 1 = 3 个单位时间,函数 1 总共执行 4 个单位时间。
示例 2:
输入:n = 1, logs = ["0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"]
输出:[8]
解释:
函数 0 在时间戳 0 的起始开始执行,执行 2 个单位时间,并递归调用它自身。
函数 0(递归调用)在时间戳 2 的起始开始执行,执行 4 个单位时间。
函数 0(初始调用)恢复执行,并立刻再次调用它自身。
函数 0(第二次递归调用)在时间戳 6 的起始开始执行,执行 1 个单位时间。
函数 0(初始调用)在时间戳 7 的起始恢复执行,执行 1 个单位时间。
所以函数 0 总共执行 2 + 4 + 1 + 1 = 8 个单位时间。
示例 3:
输入:n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:6","1:end:6","0:end:7"]
输出:[7,1]
解释:
函数 0 在时间戳 0 的起始开始执行,执行 2 个单位时间,并递归调用它自身。
函数 0(递归调用)在时间戳 2 的起始开始执行,执行 4 个单位时间。
函数 0(初始调用)恢复执行,并立刻调用函数 1 。
函数 1在时间戳 6 的起始开始执行,执行 1 个单位时间,于时间戳 6 的末尾结束执行。
函数 0(初始调用)在时间戳 7 的起始恢复执行,执行 1 个单位时间,于时间戳 7 的末尾结束执行。
所以函数 0 总共执行 2 + 4 + 1 = 7 个单位时间,函数 1 总共执行 1 个单位时间。
示例 4:
输入:n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:7","1:end:7","0:end:8"]
输出:[8,1]
示例 5:
输入:n = 1, logs = ["0:start:0","0:end:0"]
输出:[1]
解题
方法一:栈模拟
栈中存储的是 函数的id
上次调用的时间为pre
class Solution {
public:
vector<string> split(string& s){
vector<string> res;
int i=0,n=s.size();
while(i<n){
int start=i;
while(i<n&&s[i]!=':') i++;
res.push_back(s.substr(start,i-start));
i++;
}
return res;
}
vector<int> exclusiveTime(int n, vector<string>& logs) {
vector<int> res(n);
int pre=-1;
stack<int> st;
for(string& log:logs){
vector<string> ss=split(log);
int idx=stoi(ss[0]),timestamp=stoi(ss[2]);
if(ss[1]=="start"){
if(!st.empty()) res[st.top()]+=timestamp-pre;
st.push(idx);
pre=timestamp;
}else{
res[st.top()]+=timestamp-pre+1;
pre=timestamp+1;
st.pop();
}
}
return res;
}
};
边栏推荐
- 目标检测中的Bounding Box Regression Loss
- Loadrunner12.0.2安装及中文语言包安装(汉化)
- 文档数据库是怎么定位一个文档的呀?
- 关于那些我们都听过的营销工具—优惠券
- In ASP.NET Core 2.0, solve the configuration problem of large file upload
- Machine learning model too slow?Look at Intel (R) extension to accelerate
- 《STM32MP1 M4裸机CubeIDE开发指南》第二十四章 DAC实验
- C# api 将base64编码 上传至fastdfs转成文件
- 使用C# 调用api接口获取法定节假日(百度api)
- Redis 定长队列的探索和实践
猜你喜欢
随机推荐
萤石、小米对垒智能摄像头
使用类似搭积木的低代码开发方式进行 SAP API 开发
一文读懂配置管理(CM)
上周热点回顾(8.1-8.7)
以技术御风险,护航云原生 | 同创永益 X 博云举办产品联合发布会
Apple developer account application process full version
C# api 将base64编码 上传至fastdfs转成文件
Classificition Loss in target detection
持久化键值数据库的数据是保存在内存中吗?
LeetCode_14_最长公共前缀
LeetCode_1004_最大连续1的个数Ⅲ
七、图结构
鲲鹏开发者创享日2022:鲲鹏全栈创新 与开发者共建数字湖南
vs2019+boost library (boost_1_67_0) installation
关于那些我们都听过的营销工具—优惠券
分布式系统设计策略
部署spark2.2集群(standalone模式)
Jingkai Safety Supervision App technical service support
【力扣】两数相加
ReentrantLock源码分析和使用案例