Skip to content

Commit 7cbcd9d

Browse files
my solution to 1598
1 parent dbed65a commit 7cbcd9d

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

problems/1598/jeremymanning.md

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,42 @@
11
# [Problem 1598: Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder/description/?envType=daily-question)
22

33
## Initial thoughts (stream-of-consciousness)
4-
4+
- We could solve this with a stack:
5+
- Start with an empty stack (main folder)
6+
- changing the directory pushes the new folder onto the top of the stack
7+
- `'../'` pops the top folder of the stack (if empty, do nothing)
8+
- `'./'` does nothing
9+
- Then return the length of the stack at the end
10+
- We can also see that it doesn't actually matter _which_ folders we visit. So we can use less memory by just keeping track of the depth:
11+
- If the next operation starts with `'..'` and `depth > 0` subtract one from the depth
12+
- If the next operation is (`'./'`) do nothing
13+
- Otherwise add one to the depth
14+
515
## Refining the problem, round 2 thoughts
16+
- I'm seeing that folders "consist of lowercase letters and digits"-- so they may not always start with `'d'` like in the examples
17+
- Also: testing for `depth > 0` should happen in a nested statement so that `'../'` with `depth == 0` doesn't get counted as a "directory"
18+
- Let's just go with this solution; it seems straightforward
619

720
## Attempted solution(s)
821
```python
9-
class Solution: # paste your code here!
10-
...
22+
class Solution:
23+
def minOperations(self, logs: List[str]) -> int:
24+
depth = 0
25+
for x in logs:
26+
if x == '../':
27+
if depth > 0:
28+
depth -= 1
29+
elif x == './':
30+
continue
31+
else:
32+
depth += 1
33+
return depth
1134
```
35+
- given test cases pass
36+
- verifying folders can start with other characters using `logs = ["e1/","d2/","../","d21/","./","../"]`: pass
37+
- submitting...
38+
39+
- ![Screenshot 2024-07-09 at 9 39 46 PM](https://github.com/ContextLab/leetcode-solutions/assets/9030494/1d640a2b-6490-4c3f-a7f4-849e72885c6e)
40+
41+
Solved!
42+

0 commit comments

Comments
 (0)