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.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
At line 22 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7aa5c98dd960 in ??? #1 0x7aa5c98de4d9 in ??? #2 0x7aa5c9b3217b in ??? #3 0x7aa5c9b2b684 in ??? #4 0x7aa5c9b2c2aa in ??? #5 0x56dd5d2432e9 in get_args.0 #6 0x56dd5d243469 in MAIN__ #7 0x56dd5d2435ae in main
!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
At line 12 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x79b4be4bf960 in ??? #1 0x79b4be4c04d9 in ??? #2 0x79b4be71417b in ??? #3 0x79b4be70d684 in ??? #4 0x79b4be70e2aa in ??? #5 0x593f4cbe229e in MAIN__ #6 0x593f4cbe2636 in main
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
.
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
.