{"id":7419,"date":"2020-09-27T00:26:14","date_gmt":"2020-09-27T07:26:14","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7419"},"modified":"2020-09-27T00:28:05","modified_gmt":"2020-09-27T07:28:05","slug":"leetcode-1598-crawler-log-folder","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1598-crawler-log-folder\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1598. Crawler Log Folder"},"content":{"rendered":"\n<p>The Leetcode file system keeps a log each time some user performs a&nbsp;<em>change folder<\/em>&nbsp;operation.<\/p>\n\n\n\n<p>The operations are described below:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>\"..\/\"<\/code>&nbsp;: Move to the parent folder of the current folder. (If you are already in the main folder,&nbsp;<strong>remain in the same folder<\/strong>).<\/li><li><code>\".\/\"<\/code>&nbsp;: Remain in the same folder.<\/li><li><code>\"x\/\"<\/code>&nbsp;: Move to the child folder named&nbsp;<code>x<\/code>&nbsp;(This folder is&nbsp;<strong>guaranteed to always exist<\/strong>).<\/li><\/ul>\n\n\n\n<p>You are given a list of strings&nbsp;<code>logs<\/code>&nbsp;where&nbsp;<code>logs[i]<\/code>&nbsp;is the operation performed by the user at the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;step.<\/p>\n\n\n\n<p>The file system starts in the main folder, then the operations in&nbsp;<code>logs<\/code>&nbsp;are performed.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum number of operations needed to go back to the main folder after the change folder operations.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/09\/sample_11_1957.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> logs = [\"d1\/\",\"d2\/\",\"..\/\",\"d21\/\",\".\/\"]\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong>Use this change folder operation \"..\/\" 2 times and go back to the main folder.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/09\/sample_22_1957.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> logs = [\"d1\/\",\"d2\/\",\".\/\",\"d3\/\",\"..\/\",\"d31\/\"]\n<strong>Output:<\/strong> 3\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> logs = [\"d1\/\",\"..\/\",\"..\/\",\"..\/\"]\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= logs.length &lt;= 10<sup>3<\/sup><\/code><\/li><li><code>2 &lt;= logs[i].length &lt;= 10<\/code><\/li><li><code>logs[i]<\/code>&nbsp;contains lowercase English letters, digits,&nbsp;<code>'.'<\/code>, and&nbsp;<code>'\/'<\/code>.<\/li><li><code>logs[i]<\/code>&nbsp;follows the format described in the statement.<\/li><li>Folder names consist of lowercase English letters and digits.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>We only need to track the depth of current folder, and name and path can be ignored.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minOperations(vector<string>& logs) {\n    int ans = 0;\n    for (const string& log : logs) {\n      if (log == \"..\/\")\n        ans = max(ans - 1, 0);\n      else if (log != \".\/\")\n        ++ans;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Leetcode file system keeps a log each time some user performs a&nbsp;change folder&nbsp;operation. The operations are described below: &#8220;..\/&#8221;&nbsp;: Move to the parent folder&#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],"tags":[222,179],"class_list":["post-7419","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7419","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=7419"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7419\/revisions"}],"predecessor-version":[{"id":7421,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7419\/revisions\/7421"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}