koenjazh-CNfresde
짬뽕얼큰하게의 맨땅에 헤딩 :: Crawler Log Folder

Crawler Log Folder

알고리즘 2025. 2. 2. 20:57

summary

  • The problem is to find the minimum number of operations needed to go back to the main folder after a series of change folder operations.

approach

  • The solution uses a stack data structure to keep track of the current folder path
  • When a folder operation is encountered, the corresponding action is performed on the stack
  • If the operation is '../', the top element is removed from the stack
  • If the operation is './', the top element is left unchanged
  • If the operation is 'x/', the top element is replaced with 'x'
  • The minimum number of operations needed to go back to the main folder is the number of times '../' is performed minus the number of times './' is performed.

complexity

  • O(n), where n is the number of operations in the logs array

explain

  • The solution iterates over the logs array and performs the corresponding action on the stack for each operation
  • The stack is used to keep track of the current folder path
  • When the stack is empty and an '../' operation is encountered, the solution returns 0
  • Otherwise, the solution returns the difference between the number of '../' operations and the number of './' operations.

Solution Code:


class Solution {
public:
    int minOperations(vector& logs) {
        int cnt = 0;
        for(int i = 0; i < logs.size(); i++){
            if(!logs[i].compare("../")){
                cnt = cnt - 1 < 0 ? 0 : cnt - 1;
            } else if (!logs[i].compare("./")){

            } else {
                cnt++;
            }
        }
        return cnt;
    }
};

'알고리즘' 카테고리의 다른 글

Maximum Score From Removing Substrings  (0) 2025.02.02
Reverse Substrings Between Each Pair of Parentheses  (0) 2025.02.02
Average Waiting Time  (0) 2025.02.02
Find the Winner of the Circular Game  (0) 2025.02.02
Water Bottles  (0) 2025.02.02
블로그 이미지

짬뽕얼큰하게

,