Re: [閒聊] 每日leetcode

作者: sustainer123 (caster)   2024-07-10 09:55:38
※ 引述《DJYOMIYAHINA (通通打死)》之銘言:
: 今天本肥肥也是個稱職的ez守門員
: 準備出門當帕魯
: def minOperations(self, logs: List[str]) -> int:
: base = 0
: for log in logs:
: if log == "../":
: base = max(base-1, 0)
: elif log != "./":
: base += 1
: return base
https://leetcode.com/problems/crawler-log-folder
1598. Crawler Log Folder
有一log紀錄使用者更改資料夾的操作
操作有三:
1 ../:回到父資料夾 假如已在主資料夾 則不變
2 ./:不變
3 x/:前往子資料夾x
請回傳完成log的資料夾操作後 回到主資料夾的最小操作
Example 1:
Input: logs = ["d1/","d2/","../","d21/","./"]
Output: 2
Explanation: Use this change folder operation "../" 2 times and go back to
the main folder.
Example 2:
Input: logs = ["d1/","d2/","./","d3/","../","d31/"]
Output: 3
Example 3:
Input: logs = ["d1/","../","../","../"]
Output: 0
Constraints:
1 <= logs.length <= 103
2 <= logs[i].length <= 10
logs[i] contains lowercase English letters, digits, '.', and '/'.
logs[i] follows the format described in the statement.
Folder names consist of lowercase English letters and digits.
思路:
照題目需求模擬
Python Code:
class Solution:
def minOperations(self, logs: List[str]) -> int:
result = 0
for log in logs:
if log == "../":
if result > 0:
result -= 1
else:
continue
elif log == "./":
continue
else:
result += 1
return result
作者: SecondRun (雨夜琴聲)   2024-07-10 09:57:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com