diff --git a/solution/1500-1599/1598.Crawler Log Folder/README.md b/solution/1500-1599/1598.Crawler Log Folder/README.md index a15b7d479895a..021e2b08152f6 100644 --- a/solution/1500-1599/1598.Crawler Log Folder/README.md +++ b/solution/1500-1599/1598.Crawler Log Folder/README.md @@ -160,15 +160,31 @@ func minOperations(logs []string) int { ```ts function minOperations(logs: string[]): number { - let depth = 0; - for (const log of logs) { - if (log === '../') { - depth = Math.max(0, depth - 1); - } else if (log !== './') { - depth++; + let ans = 0; + for (const x of logs) { + if (x === '../') { + ans && ans--; + } else if (x !== './') { + ans++; } } - return depth; + return ans; +} +``` + +#### JavaScript + +```ts +function minOperations(logs) { + let ans = 0; + for (const x of logs) { + if (x === '../') { + ans && ans--; + } else if (x !== './') { + ans++; + } + } + return ans; } ``` diff --git a/solution/1500-1599/1598.Crawler Log Folder/README_EN.md b/solution/1500-1599/1598.Crawler Log Folder/README_EN.md index f28ffe2898b97..f7f00684bf7ff 100644 --- a/solution/1500-1599/1598.Crawler Log Folder/README_EN.md +++ b/solution/1500-1599/1598.Crawler Log Folder/README_EN.md @@ -157,15 +157,31 @@ func minOperations(logs []string) int { ```ts function minOperations(logs: string[]): number { - let depth = 0; - for (const log of logs) { - if (log === '../') { - depth = Math.max(0, depth - 1); - } else if (log !== './') { - depth++; + let ans = 0; + for (const x of logs) { + if (x === '../') { + ans && ans--; + } else if (x !== './') { + ans++; } } - return depth; + return ans; +} +``` + +#### JavaScript + +```js +function minOperations(logs) { + let ans = 0; + for (const x of logs) { + if (x === '../') { + ans && ans--; + } else if (x !== './') { + ans++; + } + } + return ans; } ``` diff --git a/solution/1500-1599/1598.Crawler Log Folder/Solution.js b/solution/1500-1599/1598.Crawler Log Folder/Solution.js new file mode 100644 index 0000000000000..ea8cad5a66594 --- /dev/null +++ b/solution/1500-1599/1598.Crawler Log Folder/Solution.js @@ -0,0 +1,11 @@ +function minOperations(logs) { + let ans = 0; + for (const x of logs) { + if (x === '../') { + ans && ans--; + } else if (x !== './') { + ans++; + } + } + return ans; +} diff --git a/solution/1500-1599/1598.Crawler Log Folder/Solution.ts b/solution/1500-1599/1598.Crawler Log Folder/Solution.ts index 9a45622f944c2..cb1295231c4fd 100644 --- a/solution/1500-1599/1598.Crawler Log Folder/Solution.ts +++ b/solution/1500-1599/1598.Crawler Log Folder/Solution.ts @@ -1,11 +1,11 @@ function minOperations(logs: string[]): number { - let depth = 0; - for (const log of logs) { - if (log === '../') { - depth = Math.max(0, depth - 1); - } else if (log !== './') { - depth++; + let ans = 0; + for (const x of logs) { + if (x === '../') { + ans && ans--; + } else if (x !== './') { + ans++; } } - return depth; + return ans; }