[LeetCode] 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:

  1. Input logs will be sorted by timestamp, NOT log id.
  2. Your output should be sorted by function id, which means the 0th element of your output corresponds to the exclusive time of function 0.
  3. Two functions won‘t start or end at the same time.
  4. Functions could be called recursively, and will always end.
  5. 1 <= n <= 100

s

时间: 2024-08-02 23:11:35

[LeetCode] Exclusive Time of Functions 函数的独家时间的相关文章

Leetcode 636.函数的独占时间

函数的独占时间 给出一个非抢占单线程CPU的 n 个函数运行日志,找到函数的独占时间. 每个函数都有一个唯一的 Id,从 0 到 n-1,函数可能会递归调用或者被其他函数调用. 日志是具有以下格式的字符串:function_id:start_or_end:timestamp.例如:"0:start:0" 表示函数 0 从 0 时刻开始运行."0:end:0" 表示函数 0 在 0 时刻结束. 函数的独占时间定义是在该方法中花费的时间,调用其他函数花费的时间不算该函数

各个函数消耗的时间profiling和内存泄漏valgrind

来源:http://06110120wxc.blog.163.com/blog/static/37788161201333112445844/ ARM(hisi)上面的profiling和valgrind的安装 profiling的使用 GNU gprof能够打印出程序运行中各个函数消耗的时间,可以帮助程序员找出众多函数中耗时最多的函数.产生程序运行时候的函数调用关系,包括调用次数,可以帮助程序员分析程序的运行流程.有了函数的调用关系,这会让开发人员大大提高工作效率 gprof的基本用法: 1.

MySQL字符串函数、日期时间函数

MySQL字符串函数.日期时间函数 一.常见字符串函数: 1.CHAR_LENGTH  获取长度(字符为单位) 2.FORMAT  格式化 3.INSERT  替换的方式插入 4.INSTR  获取位置 5.LEFT/RIGHT  取左.取右 6.LENGTH   获取长度(字节为单位) 7.LTRIM/RTRIM/TRIM 去空格(左/右/自定义) 8.STRCMP  字符串比较 9.CONCAT  字符串拼接 10.SUBSTRING  字符串截取 1.CHAR_LENGTH:获取长度(字符

ORACLE函数之日期时间运算函数

1            ADD_MONTHS 格式:ADD_MONTHS(D,N) 说明:返回日期时间D加N月后对应的日期时间.N为正时则表示D之后:N为负时则表示为D之前:N为小数则会自动先删除小数部分,而用整数部分 举例: SQL>SELECT ADD_MONTHS(SYSDATE,7) A,ADD_MONTHS(SYSDATE,-7) B,ADD_MONTHS(SYSDATE,7.9)C FROM DUAL; A                             B        

无参装饰器为被装饰函数添加统计时间的功能

#需求 定义无参装饰器为被装饰函数添加统计时间的功能 1 import time #导入时间模块 2 3 def timer(func): 4 def wapper(): 5 start = time.time() 6 func() 7 stop = time.time() 8 index_spend_time = stop - start 9 print(index_spend_time) 10 return wapper 11 @timer 12 def index(): 13 time.s

ORACLE函数之日期时间转换函数

 1.          TO_CHAR 语法:TO_CHAR(X [,format]) 说明:将X按format格式转换成字符串.X是一个日期或者数字,format是一个规定了X采用何种格式转换的格式字符串 举例: SQL>SELECT TO_CHAR(sysdate,'YYYY-MM-DD hh24:mi:ss') A FROM DUAL; A ------------------- 2014-06-1815:58:43 2.            TO_DATE 语法:TO_DATE(

C# .Net计算函数执行的时间

C#计算函数执行的时间 protected void StopwatchTest() { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); // 开始监视代码 //_________________要执行的函数______________________ //Code…… stopwatch.Stop(); // 停止监视 TimeSpan timeSpa

给file_get_contents函数设置超时时间

$opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>60, ) ); $context = stream_context_create($opts); $html =file_get_contents('http://www.example.com', false, $context); 这样,file_get_contents获取数据时,超出60秒将会自动退出. 还可以利用file_get_conten

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 functio