📌  相关文章
📜  返回主文件夹所需的最少操作次数

📅  最后修改于: 2021-09-08 12:45:37             🧑  作者: Mango

给定一个字符串数组arr[]表示在文件系统上执行的更改的文件夹操作(Unix 风格)。最初,文件系统在主文件夹中打开。任务是找到以下三种类型的最小操作计数返回到主文件夹:

  • “../”:移动到当前文件夹的父文件夹。 (如果当前文件夹是主文件夹,则它保留在同一文件夹中)。
  • “./”:保留在同一文件夹中。
  • “F/”:移动到名为 F 的子文件夹。

例子:

方法:使用Stack可以解决这个问题。请按照以下步骤解决问题:

  • 初始化一个变量,比如cntOp来存储返回主文件夹所需的最少操作数。
  • 创建一个Stack,说st存放当前文件夹的路径
  • 遍历数组并检查以下条件:
    • 如果arr[i] == “../”则弹出栈顶元素。
    • 如果arr[i] == “F/”,则将arr[i]的值压入栈顶。
  • 最后,打印留在堆栈上的元素计数。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find minimum number of
// operations to go back to main folder
int minOperations(vector& arr,
                               int N)
{
    // Stores path of
    // the current folder
    stack st;
 
    for (int i = 0; i < N; i++) {
       
        // If stack is not empty and
        // the value of arr[i] is "../"
        if (arr[i] == "../" &&
                      !st.empty()) {
             
            // Pop top element of
            // the stack             
            st.pop();
        }
 
        // If the value of arr[i]
        // is like "F/"
        else if (arr[i] != "./") {
             
            // Push arr[i] on top element
            // of the stack              
            st.push(arr[i]);
        }
    }
 
    // Return count of elements left
    // into the stack
    return st.size();
}
 
// Driver Code
int main()
{
    vector arr
        = { "F1/", "F2/", "./",
          "F3/", "../", "F31/" };
    int N = arr.size();
    cout << minOperations(arr, N);
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find minimum number of
// operations to go back to main folder
static int minOperations(String []arr, int N)
{
     
    // Stores path of
    // the current folder
    Stack st = new Stack<>();
 
    for(int i = 0; i < N; i++)
    {
         
        // If stack is not empty and
        // the value of arr[i] is "../"
        if (arr[i] == "../" && !st.empty())
        {
             
            // Pop top element of
            // the stack             
            st.pop();
        }
 
        // If the value of arr[i]
        // is like "F/"
        else if (arr[i] != "./")
        {
             
            // Push arr[i] on top element
            // of the stack              
            st.push(arr[i]);
        }
    }
 
    // Return count of elements left
    // into the stack
    return st.size();
}
 
// Driver Code
public static void main(String args[])
{
    String []arr = { "F1/", "F2/", "./",
                     "F3/", "../", "F31/" };
                      
    int N = arr.length;
     
    System.out.print(minOperations(arr, N));
}
}
 
// This code is contributed by ipg2016107


Python3
# Python3 program to implement
# the above appraoch
 
# Function to find minimum number of
# operations to go back to main folder
def minOperations(arr, N):
     
    # Stores path of
    # the current folder
    st = []
 
    for i in range(N):
         
        # If stack is not empty and
        # the value of arr[i] is "../"
        if (arr[i] == "../" and len(st) != 0):
             
            # Pop top element of
            # the stack
            st.pop(-1)
 
        # If the value of arr[i]
        # is like "F/"
        elif (arr[i] != "./"):
             
            # Push arr[i] on top element
            # of the stack
            st.append(arr[i])
 
    # Return count of elements left
    # into the stack
    return len(st)
 
# Driver code
if __name__ == '__main__':
     
    # Given array
    arr = [ "F1/", "F2/", "./",
            "F3/", "../", "F31/" ]
 
    # Size of the array
    N = len(arr)
 
    # Function Call
    print(minOperations(arr, N))
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to find minimum
// number of operations to
// go back to main folder
static int minOperations(String []arr,
                         int N)
{   
  // Stores path of
  // the current folder
  Stack st =
        new Stack();
 
  for(int i = 0; i < N; i++)
  {
    // If stack is not empty
    // and the value of arr[i]
    // is "../"
    if (arr[i] == "../" &&
        st.Count != 0)
    {
      // Pop top element of
      // the stack             
      st.Pop();
    }
 
    // If the value of arr[i]
    // is like "F/"
    else if (arr[i] != "./")
    {
 
      // Push arr[i] on top
      // element of the stack              
      st.Push(arr[i]);
    }
  }
 
  // Return count of elements
  // left into the stack
  return st.Count;
}
 
// Driver Code
public static void Main(String []args)
{
  String []arr = {"F1/", "F2/", "./",
                  "F3/", "../", "F31/"};
  int N = arr.Length;
  Console.Write(minOperations(arr, N));
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
3

时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live