exclusive_time_of_functions
1# @leet start 2class Solution: 3 def exclusiveTime(self, n: int, logs: list[str]) -> list[int]: 4 """ 5 This question asks us to figure out the amount of time a specific function 6 took to run given a set of logs that describe how long each function ran 7 for. It comes in the format of "{function_id}:{start | end}:{timestamp}". 8 9 We have to return the amount of time each function exclusively ran on the cpu. 10 To do so, we first parse the log by splitting it on colons, and then, 11 we can simulate the run. 12 13 If the action is start, we want to put our current function onto the stack, 14 and then run the previous function until the current time, since it had 15 exclusive time. We then update our running timestamp to the timestamp 16 given by the function. 17 18 Otherwise, the action is end, so we end the run of the function, by 19 subtracting timestamp - current + 1 and then setting our current to 20 timestamp + 1. We then record the amount of time in the answer array. 21 """ 22 ans, stack, prev_time = [0] * n, [], 0 23 24 for log in logs: 25 fid, action, timestamp = log.split(":") 26 fid, timestamp = int(fid), int(timestamp) 27 28 if action == "start": 29 if stack: 30 ans[stack[-1]] += timestamp - prev_time 31 stack.append(fid) 32 prev_time = timestamp 33 else: 34 ans[stack.pop()] += timestamp - prev_time + 1 35 prev_time = timestamp + 1 36 return ans 37 38 39# @leet end 40 41 42def test(): 43 assert 2 + 2 == 4
3class Solution: 4 def exclusiveTime(self, n: int, logs: list[str]) -> list[int]: 5 """ 6 This question asks us to figure out the amount of time a specific function 7 took to run given a set of logs that describe how long each function ran 8 for. It comes in the format of "{function_id}:{start | end}:{timestamp}". 9 10 We have to return the amount of time each function exclusively ran on the cpu. 11 To do so, we first parse the log by splitting it on colons, and then, 12 we can simulate the run. 13 14 If the action is start, we want to put our current function onto the stack, 15 and then run the previous function until the current time, since it had 16 exclusive time. We then update our running timestamp to the timestamp 17 given by the function. 18 19 Otherwise, the action is end, so we end the run of the function, by 20 subtracting timestamp - current + 1 and then setting our current to 21 timestamp + 1. We then record the amount of time in the answer array. 22 """ 23 ans, stack, prev_time = [0] * n, [], 0 24 25 for log in logs: 26 fid, action, timestamp = log.split(":") 27 fid, timestamp = int(fid), int(timestamp) 28 29 if action == "start": 30 if stack: 31 ans[stack[-1]] += timestamp - prev_time 32 stack.append(fid) 33 prev_time = timestamp 34 else: 35 ans[stack.pop()] += timestamp - prev_time + 1 36 prev_time = timestamp + 1 37 return ans
4 def exclusiveTime(self, n: int, logs: list[str]) -> list[int]: 5 """ 6 This question asks us to figure out the amount of time a specific function 7 took to run given a set of logs that describe how long each function ran 8 for. It comes in the format of "{function_id}:{start | end}:{timestamp}". 9 10 We have to return the amount of time each function exclusively ran on the cpu. 11 To do so, we first parse the log by splitting it on colons, and then, 12 we can simulate the run. 13 14 If the action is start, we want to put our current function onto the stack, 15 and then run the previous function until the current time, since it had 16 exclusive time. We then update our running timestamp to the timestamp 17 given by the function. 18 19 Otherwise, the action is end, so we end the run of the function, by 20 subtracting timestamp - current + 1 and then setting our current to 21 timestamp + 1. We then record the amount of time in the answer array. 22 """ 23 ans, stack, prev_time = [0] * n, [], 0 24 25 for log in logs: 26 fid, action, timestamp = log.split(":") 27 fid, timestamp = int(fid), int(timestamp) 28 29 if action == "start": 30 if stack: 31 ans[stack[-1]] += timestamp - prev_time 32 stack.append(fid) 33 prev_time = timestamp 34 else: 35 ans[stack.pop()] += timestamp - prev_time + 1 36 prev_time = timestamp + 1 37 return ans
This question asks us to figure out the amount of time a specific function took to run given a set of logs that describe how long each function ran for. It comes in the format of "{function_id}:{start | end}:{timestamp}".
We have to return the amount of time each function exclusively ran on the cpu. To do so, we first parse the log by splitting it on colons, and then, we can simulate the run.
If the action is start, we want to put our current function onto the stack, and then run the previous function until the current time, since it had exclusive time. We then update our running timestamp to the timestamp given by the function.
Otherwise, the action is end, so we end the run of the function, by subtracting timestamp - current + 1 and then setting our current to timestamp + 1. We then record the amount of time in the answer array.