【LeetCode】636. Exclusive Time of Functions 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/exclusive-time-of-functions/description/
题目描述
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
题目大意
求一个函数调用栈中各个函数各自的执行时间。这是个不能争夺资源的系统,同时只能运行一个函数。
给的数据包括函数的数量,以及各自的调用和结束的时间。
需要注意的是,开始时间是在时间片的开头,结束时间是在时间片的结尾。
解题方法
栈
这个题很好想到思路使用栈,因为我们已经知道了操作系统里面的函数调用确实就是用栈实现的。
因为同时只能运行一个函数,所以就是一个后进先出的栈。给出的调用日志一定会满足我们说的栈的条件的。
下面的解析应该能看明白。
首先要弄明白一点:当遍历到logs中的某个字符串时,无论它是begin还是end,当前位于栈顶的元素都会占用 “当前字符串的timePoint-之前字符串的timePoint”(或+1) 时间。
因为如果当前遍历到的字符串是start,那么栈顶元素就是之前start了还没结束的function,在 当前时间点 和 上一个时间点 之间的这段时间,是被栈顶元素占用的,占用了 “当前字符串的timePoint-之前字符串的timePoint” 时间。
如果当前遍历到的字符串是end,那么栈顶元素就是 当前字符串的function (前面一个字符串刚push进了该function的start) ,那么在 当前时间点 和 上一个时间点 之间的这段时间,也肯定是被栈顶元素占用的,占用 “当前字符串的timePoint-之前字符串的timePoint +1 ” 时间 (比之前多加了一个end时间点)。
举个例子来说明:
functionId: 0 1 2 2 1 0
begin/end: { { { } } }
timeItem: 0 1 2 3 4 5
0 被push进栈后,接下来遍历到 1 start 1,那么 0~1 的时间是被栈顶元素 0 占用的。接下来 1 被push进栈,遍历到 2 start 2,那么 1~2 的时间是被栈顶元素 1 占用的。接下来 2 被push进栈,遍历到 2 end 3,那么 2~3 的时间是被栈顶元素 2 占用的。接下来pop出 2 ,遍历到 1 end 4,那么3~4的时间是栈顶元素 1 占用的。接下来pop出 1 ,遍历到 0 end 5,那么 4~5 的时间是栈顶元素 0 占用的。
所以算法的关键在于:拿到上一个log的 start/stop time 设为prev,再拿到当前 log 的 start/stop time ,计算出两个time之间的时间差。注意prevTime表示的是当前这个操作结束之后的这一秒的开始时间,即如果是start那么就是这秒的开始时间,如果是end那么就是下一秒的开始时间。
摘自:http://blog.csdn.net/huanghanqian/article/details/77160234
class Solution(object):
def exclusiveTime(self, n, logs):
"""
:type n: int
:type logs: List[str]
:rtype: List[int]
"""
res = [0] * n
stack = []
prevTime = 0
for log in logs:
idx, type, time = log.split(':')
if type == 'start':
if stack:
res[stack[-1]] += int(time) - prevTime
stack.append(int(idx))
prevTime = int(time)
else:
res[stack[-1]] += int(time) - prevTime + 1
stack.pop()
prevTime = int(time) + 1
return res
日期
2018 年 3 月 13 日
2019 年 3 月 23 日 —— 时隔一年还是没有bug free
【LeetCode】636. Exclusive Time of Functions 解题报告(Python)的更多相关文章
- [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】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- Leetcode 之 Exclusive Time of Functions
636. Exclusive Time of Functions 1.Problem Given the running logs of n functions that are executed i ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
随机推荐
- Linux学习——Gdb基本调试方法&&多线程调试
1.Gdb的基本调试 示例代码 //e.c #include <stdio.h> void debug(char *str) { printf("debug info :%s\n ...
- 关于蓝牙Mesh您必须知道的七件事
蓝牙技术联盟于7月19日正式宣布,蓝牙(Bluetooth)技术开始全面支持Mesh网状网络.全新的Mesh功能提供设备间多对多传输,并特别提高构建大范围网络覆盖的通信能力,适用于楼宇自动化.无线传感 ...
- 谈一谈 DDD
一.前言 最近 10 年的互联网发展,从电子商务到移动互联,再到"互联网+"与传统行业的互联网转型,是一个非常痛苦的转型过程.在这个过程中,一方面会给我们带来诸多的挑战,另一方面又 ...
- 年底巩固下 CS 知识「GitHub 热点速览 v.21.49」
作者:HelloGitHub-小鱼干 期末到了!是时候来一波 CS 复习资料了,从本科基础知识开始到实用编程技术.本周 GitHub 热点趋势榜给你提供了最全的复习资料:清华的 CS 四年学习资料.W ...
- 分布式事务(3)---强一致性分布式事务Atomikos实战
分布式事务(1)-理论基础 分布式事务(2)---强一致性分布式事务解决方案 分布式事务(4)---最终一致性方案之TCC 前面介绍强一致性分布式解决方案,这里用Atomikos框架写一个实战的dem ...
- Hadoop入门 完全分布式运行模式-集群配置
目录 集群配置 集群部署规划 配置文件说明 配置集群 群起集群 1 配置workers 2 启动集群 总结 3 集群基本测试 上传文件到集群 查看数据真实存储路径 下载 执行wordcount程序 配 ...
- day05 连表查询与子查询
day05 连表查询与子查询 昨日内容回顾 表关系之一对一 换位思考之后得出两边都是不可以 要么是没有关系,要么是一对一 一对一的表关系外键虽然建在哪个都可以,但是建议建在查询频率多的表上 # 外键其 ...
- 【leetcode】834. Sum of Distances in Tree(图算法)
There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(四)-介绍库函数,获取一些SD卡的信息
其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...
- centOS7.4 , gcc4.8.5装cgdb
从github上clone最新releast后进入文件夹 ./configure –prefix=/usr/local CXXFLAGS=-std=c++11 make&make instal ...