Program to find minimum jump needed to return from a folder to home in Python

Suppose we have logs where we have a path to enter into folders. There are different symbols that represent navigation operations ?

  • "../": Move to the parent folder from current one. (If we are at main folder, do not change location).

  • "./": Remain in the current folder.

  • "x/": Move to the child folder named x.

From the logs we need to find the minimum number of operations needed to come back from the last folder where we stop to the main folder.

Problem Understanding

If the input is like logs = ["Dir1/", "Dir2/", "../", "Dir2/", "Dir3/", "./"], then the output will be 3.

Folder Navigation Path Home Dir1 Dir2 Dir1 Dir2 Dir3 3 steps to return home

From the diagram we can see we need to step back three times to reach home from Dir3.

Algorithm

To solve this, we will follow these steps ?

  • stack: Create a new list to simulate folder navigation

  • For each item in logs:

    • If item is "../" and stack has elements, remove last element (go to parent folder)

    • If item is not "./" and not "../", add it to stack (enter new folder)

    • If item is "./", continue (stay in current folder)

  • Return the length of stack (number of steps to return home)

Implementation

def min_jumps_to_home(logs):
    stack = []
    
    for operation in logs:
        if operation == "../" and len(stack) > 0:
            stack.pop()  # Go back to parent folder
        elif operation != "./" and operation != "../":
            stack.append(operation)  # Enter new folder
        # If operation is "./" do nothing (stay in current folder)
    
    return len(stack)

# Test the function
logs = ["Dir1/", "Dir2/", "../", "Dir2/", "Dir3/", "./"]
result = min_jumps_to_home(logs)
print(f"Minimum jumps needed: {result}")
Minimum jumps needed: 3

Step-by-Step Execution

def min_jumps_detailed(logs):
    stack = []
    
    for i, operation in enumerate(logs):
        print(f"Step {i+1}: Processing '{operation}'")
        
        if operation == "../" and len(stack) > 0:
            removed = stack.pop()
            print(f"  Going back from {removed}, stack: {stack}")
        elif operation != "./" and operation != "../":
            stack.append(operation)
            print(f"  Entering folder {operation}, stack: {stack}")
        else:
            print(f"  Staying in current folder, stack: {stack}")
    
    print(f"\nFinal position: {len(stack)} folders deep")
    return len(stack)

logs = ["Dir1/", "Dir2/", "../", "Dir2/", "Dir3/", "./"]
result = min_jumps_detailed(logs)
Step 1: Processing 'Dir1/'
  Entering folder Dir1/, stack: ['Dir1/']
Step 2: Processing 'Dir2/'
  Entering folder Dir2/, stack: ['Dir1/', 'Dir2/']
Step 3: Processing '../'
  Going back from Dir2/, stack: ['Dir1/']
Step 4: Processing 'Dir2/'
  Entering folder Dir2/, stack: ['Dir1/', 'Dir2/']
Step 5: Processing 'Dir3/'
  Entering folder Dir3/, stack: ['Dir1/', 'Dir2/', 'Dir3/']
Step 6: Processing './'
  Staying in current folder, stack: ['Dir1/', 'Dir2/', 'Dir3/']

Final position: 3 folders deep

Key Points

  • We use a stack data structure to track our current folder depth

  • Each folder entry increases the stack size

  • Each "../" operation decreases the stack size (if not already at home)

  • The "./" operation has no effect on navigation

  • Final stack size equals minimum jumps needed to return home

Conclusion

This problem uses a stack-based approach to simulate folder navigation. The key insight is that the final depth in the folder structure equals the minimum number of "../" operations needed to return home.

Updated on: 2026-03-25T20:22:59+05:30

401 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements