{"id":1763,"date":"2018-02-05T23:44:42","date_gmt":"2018-02-06T07:44:42","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1763"},"modified":"2018-02-06T07:45:33","modified_gmt":"2018-02-06T15:45:33","slug":"leetcode-636-exclusive-time-of-functions","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-636-exclusive-time-of-functions\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 636. Exclusive Time of Functions"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 636. Exclusive Time of Functions - \u5237\u9898\u627e\u5de5\u4f5c EP166\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/Oi68_8xkxI4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e9b\u51fd\u6570\u7684\u8d77\u59cb\/\u7ec8\u6b62\u65f6\u95f4\u7684\u65e5\u5fd7\uff0c\u8ba9\u4f60\u8f93\u51fa\u6bcf\u4e2a\u51fd\u6570\u7684\u603b\u8fd0\u884c\u65f6\u95f4\u3002\u5047\u8bbe\u5355\u6838\u5355\u7ebf\u7a0b\uff0c\u652f\u6301\u9012\u5f52\u51fd\u6570\u3002<\/p>\n<div class=\"question-description\">\n<p>Given the running logs of\u00a0<b>n<\/b>\u00a0functions that are executed in a nonpreemptive single threaded CPU, find the exclusive time of these functions.<\/p>\n<p>Each function has a unique id, start from\u00a0<b>0<\/b>\u00a0to\u00a0<b>n-1<\/b>. A function may be called recursively or by another function.<\/p>\n<p>A log is a string has this format :\u00a0<code>function_id:start_or_end:timestamp<\/code>. For example,\u00a0<code>\"0:start:0\"<\/code>\u00a0means function 0 starts from the very beginning of time 0.\u00a0<code>\"0:end:0\"<\/code>\u00a0means function 0 ends to the very end of time 0.<\/p>\n<p>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&#8217;s exclusive time. You should return the exclusive time of each function sorted by their function id.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input:\r\nn = 2\r\nlogs = \r\n[\"0:start:0\",\r\n \"1:start:2\",\r\n \"1:end:5\",\r\n \"0:end:6\"]\r\nOutput:[3, 4]\r\nExplanation:\r\nFunction 0 starts at time 0, then it executes 2 units of time and reaches the end of time 1. \r\nNow function 0 calls function 1, function 1 starts at time 2, executes 4 units of time and end at time 5.\r\nFunction 0 is running again at time 6, and also end at the time 6, thus executes 1 unit of time. \r\nSo function 0 totally execute 2 + 1 = 3 units of time, and function 1 totally execute 4 units of time.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>Input logs will be sorted by timestamp, NOT log id.<\/li>\n<li>Your output should be sorted by function id, which means the 0th element of your output corresponds to the exclusive time of function 0.<\/li>\n<li>Two functions won&#8217;t start or end at the same time.<\/li>\n<li>Functions could be called recursively, and will always end.<\/li>\n<li>1 &lt;= n &lt;= 100<\/li>\n<\/ol>\n<p><strong>Solution: Simulate using stack<\/strong><\/p>\n<\/div>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 35 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; exclusiveTime(int n, vector&lt;string&gt;&amp; logs) {\r\n    vector&lt;int&gt; ans(n, 0);\r\n    char action[6];\r\n    int fid;\r\n    int curr;\r\n    int prev;    \r\n    stack&lt;int&gt; s;\r\n    for (const string&amp; log : logs) {\r\n      sscanf(log.c_str(), \"%d:%[a-z]:%d\", &amp;fid, action, &amp;curr);\r\n      if (action[0] == 's') {\r\n        if (!s.empty())\r\n          ans[s.top()] += curr - prev;\r\n        s.push(fid);\r\n        prev = curr;\r\n      } else {\r\n        ans[s.top()] += curr - prev + 1;\r\n        s.pop();\r\n        prev = curr + 1;\r\n      }\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e9b\u51fd\u6570\u7684\u8d77\u59cb\/\u7ec8\u6b62\u65f6\u95f4\u7684\u65e5\u5fd7\uff0c\u8ba9\u4f60\u8f93\u51fa\u6bcf\u4e2a\u51fd\u6570\u7684\u603b\u8fd0\u884c\u65f6\u95f4\u3002\u5047\u8bbe\u5355\u6838\u5355\u7ebf\u7a0b\uff0c\u652f\u6301\u9012\u5f52\u51fd\u6570\u3002 Given the running logs of\u00a0n\u00a0functions that are executed in a nonpreemptive single threaded CPU, find the exclusive time of these functions. Each function has&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,47],"tags":[17,180],"class_list":["post-1763","post","type-post","status-publish","format-standard","hentry","category-simulation","category-string","tag-recursion","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1763"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1763\/revisions"}],"predecessor-version":[{"id":1767,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1763\/revisions\/1767"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}