Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into main
  • Loading branch information
paxtonfitzpatrick committed Jul 10, 2024
2 parents d062d06 + 7cbcd9d commit eb09620
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions problems/1598/jeremymanning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# [Problem 1598: Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder/description/?envType=daily-question)

## Initial thoughts (stream-of-consciousness)
- We could solve this with a stack:
- Start with an empty stack (main folder)
- changing the directory pushes the new folder onto the top of the stack
- `'../'` pops the top folder of the stack (if empty, do nothing)
- `'./'` does nothing
- Then return the length of the stack at the end
- 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:
- If the next operation starts with `'..'` and `depth > 0` subtract one from the depth
- If the next operation is (`'./'`) do nothing
- Otherwise add one to the depth

## Refining the problem, round 2 thoughts
- I'm seeing that folders "consist of lowercase letters and digits"-- so they may not always start with `'d'` like in the examples
- Also: testing for `depth > 0` should happen in a nested statement so that `'../'` with `depth == 0` doesn't get counted as a "directory"
- Let's just go with this solution; it seems straightforward

## Attempted solution(s)
```python
class Solution:
def minOperations(self, logs: List[str]) -> int:
depth = 0
for x in logs:
if x == '../':
if depth > 0:
depth -= 1
elif x == './':
continue
else:
depth += 1
return depth
```
- given test cases pass
- verifying folders can start with other characters using `logs = ["e1/","d2/","../","d21/","./","../"]`: pass
- submitting...

- ![Screenshot 2024-07-09 at 9 39 46 PM](https://github.com/ContextLab/leetcode-solutions/assets/9030494/1d640a2b-6490-4c3f-a7f4-849e72885c6e)

Solved!

0 comments on commit eb09620

Please sign in to comment.