Crawler Log Folder

🏠 ⬅️ ➡️

The Leetcode file system keeps a log each time some user performs a change folder operation.

The operations are described below:

  • "../ " : Move to the parent folder of the current folder. (If you are already in the main folder, remain in the same folder).
  • "./ " : Remain in the same folder.
  • "x/ " : Move to the child folder named x (This folder is guaranteed to always exist).

You are given a list of strings logs where logs[i] is the operation performed by the user at the ith step.

The file system starts in the main folder, then the operations in logs are performed.

Return the minimum number of operations needed to go back to the main folder after the change folder operations.

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.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: i, n
    character(len=10) :: logs(100)

    call get_args(n, logs)

    do i = 1, n
        write (*, '(A)') trim(logs(i))
    end do

contains

    subroutine get_args(n, logs)
        implicit none
        integer, intent(out) :: n
        character(len=10), intent(out) :: logs(:)
        integer :: i

        ! read input from stdin
        read *, n
        do i = 1, n
            read *, logs(i)
        end do
    end subroutine get_args
end program main
Compiled
Executed
Correct
!include "stdio.h"

program leetcode_1236_solution

implicit none

integer :: i, j, k, n, m, count
character(len=10) :: logs(100000)
character(len=10) :: folder

! read input
read(*,*) n
do i=1,n
    read(*,*) logs(i)
end do

! initialize variables
count = 0
folder = "main"

! process input
do i=1,n
    select case (logs(i))
        case ("../ ")
            ! move to parent folder
            if (folder /= "main") then
                folder = folder(1:len_trim(folder)-1)
            end if
        case ("./ ")
            ! remain in current folder
        case default
            ! move to child folder
            folder = folder // logs(i)(1:len_trim(logs(i)))
    end select
    count = count + 1
end do

! print output
write(*,*) count

end program leetcode_1236_solution
🌐 Data from online sources
def minOperations(logs):
    depth = 0
    for log in logs:
        if log == "../":
            depth = max(0, depth - 1)
        elif log != "./":
            depth += 1
    return depth

The algorithm iterates through each log entry, incrementing the depth variable whenever it encounters a non-relative move ("x/"). If it encounters a move to the parent directory ("../") it decreases the depth variable by 1, but only if the depth is greater than 0 to make sure we don't go beyond the main folder. It ignores relative moves ("./"). Finally, it returns the number of operations needed to go back to the main folder, which is the final value of depth.

🌐 Data from online sources
int minOperations(vector<string>& logs) {
    int depth = 0;
    for (const string& log : logs) {
        if (log == "../") {
            depth = max(0, depth - 1);
        } else if (log != "./") {
            depth += 1;
        }
    }
    return depth;
}

The algorithm iterates through each log entry, incrementing the depth variable whenever it encounters a non-relative move ("x/"). If it encounters a move to the parent directory ("../") it decreases the depth variable by 1, but only if the depth is greater than 0 to make sure we don't go beyond the main folder. It ignores relative moves ("./"). Finally, it returns the number of operations needed to go back to the main folder, which is the final value of depth.