넘치게 채우기

[LeetCode] 1598. Crawler Log Folder 본문

PS/LeetCode

[LeetCode] 1598. Crawler Log Folder

riveroverflow 2024. 7. 10. 10:19
728x90
반응형

https://leetcode.com/problems/crawler-log-folder/description/

leetcode - Crawler Log Folder

문제 유형 : 구현

문제 난이도 : Easy

 

문제

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.

 

Leetcode 파일시스템은 폴더 연산을 저장한다.

  • "../": 부모 디렉토리로 이동(메인폴더이면, 유지)
  • "./": 현재 폴더에 남기
  • "x/": x란 이름의 자식폴더로 이동

 

이동의 순서열 logs가 주어집니다.

파일시스템은 메인부터시작합니다.

파일시스템은 메인폴더부터 해서 logs대로 움직였습니다.

main으로 돌아가기위한 최소 폴더연산의 횟수를 구하시오.

 

풀이

폴더 연산 규칙 그대로 적용하면 된다.

main으로부터의 거리 depth를 정해서 "../"를 만나면 depth--, "./"를 만나면 유지, 이외에는 depth++를 한다.

그 뒤 depth를 반환한다.

 

 

코드

C++

class Solution {
public:
    int minOperations(vector<string>& logs) {
        int depth = 0;
        for(const auto &operation : logs) {
            if(operation == "./") {
                continue;
            } else if(operation == "../") {
                if(depth == 0) continue;
                depth--;
            } else {
                depth++;
            }
        }

        return depth;
    }
};
 
728x90
반응형