当前位置:网站首页>Stack Title: the longest absolute path of a file
Stack Title: the longest absolute path of a file
2022-07-26 02:24:00 【Great Cherny】
List of articles
subject
Title and provenance
title : The longest absolute path to the file
Source :388. The longest absolute path to the file
difficulty
7 level
Title Description
requirement
Suppose there is a file system to store files and directories . An example of a file system is shown in the following figure :

There will be dir \texttt{dir} dir As the only directory in the root directory . dir \texttt{dir} dir Contains two subdirectories subdir1 \texttt{subdir1} subdir1 and subdir2 \texttt{subdir2} subdir2. subdir1 \texttt{subdir1} subdir1 Include files file1.ext \texttt{file1.ext} file1.ext And subdirectories subsubdir1 \texttt{subsubdir1} subsubdir1; subdir2 \texttt{subdir2} subdir2 Include subdirectories subsubdir2 \texttt{subsubdir2} subsubdir2, This subdirectory contains files file2.ext \texttt{file2.ext} file2.ext.
In text format , As shown below (* Represents a tab ):
dir
* subdir1
* * file1.ext
* * subsubdir1
* subdir2
* * subsubdir2
* * * file2.ext
In case of code representation , The above file system can be written as "dir \ n \ tsubdir1 \ n \ tfile1.ext \ n \ t \ tsubsubdir1 \ n \ tsubdir2 \ n \ t \ tsubsubdir2 \ n \ t \ t \ tfile2.ext" \texttt{"dir}\backslash\texttt{n}\backslash\texttt{tsubdir1}\backslash\texttt{n}\backslash\texttt{tfile1.ext}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{tsubsubdir1}\backslash\texttt{n}\backslash\texttt{tsubdir2}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{tsubsubdir2}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{t}\backslash\texttt{tfile2.ext"} "dir\n\tsubdir1\n\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext". among ‘ \ n’ \texttt{`}\backslash\texttt{n'} ‘\n’ and ‘ \ t’ \texttt{`}\backslash\texttt{t'} ‘\t’ Line breaks and tabs, respectively .
Each file and folder in the file system has a unique Absolute path , That is, you must open it to reach the file / Directory order of directory location , For all paths ‘/’ \texttt{`/'} ‘/’ Connect . In the example above , Point to file2.ext \texttt{file2.ext} file2.ext The absolute path of "dir/subdir2/subsubdir2/file2.ext" \texttt{"dir/subdir2/subsubdir2/file2.ext"} "dir/subdir2/subsubdir2/file2.ext". Each directory name consists of the letters 、 Numbers and / Or spaces , Each file name follows name.extension \texttt{name.extension} name.extension The format of , Where the name and extension consist of the letters 、 Numbers and / Or spaces .
Give a string representing the file system in the above format input \texttt{input} input, Return to the file system The longest absolute path to a file The length of . If there are no files in the system , return 0 \texttt{0} 0.
Example
Example 1:

Input : input = "dir \ n \ tsubdir1 \ n \ tsubdir2 \ n \ t \ tfile.ext" \texttt{input = "dir}\backslash\texttt{n}\backslash\texttt{tsubdir1}\backslash\texttt{n}\backslash\texttt{tsubdir2}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{tfile.ext"} input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
Output : 20 \texttt{20} 20
explain : Only one file , The absolute path is "dir/subdir2/file.ext" \texttt{"dir/subdir2/file.ext"} "dir/subdir2/file.ext", The length of the path 20 \texttt{20} 20.
Example 2:

Input : input = "dir \ n \ tsubdir1 \ n \ t \ tfile1.ext \ n \ t \ tsubsubdir1 \ n \ tsubdir2 \ n \ t \ tsubsubdir2 \ n \ t \ t \ tfile2.ext" \texttt{input = "dir}\backslash\texttt{n}\backslash\texttt{tsubdir1}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{tfile1.ext}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{tsubsubdir1}\backslash\texttt{n}\backslash\texttt{tsubdir2}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{tsubsubdir2}\backslash\texttt{n}\backslash\texttt{t}\backslash\texttt{t}\backslash\texttt{tfile2.ext"} input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
Output : 32 \texttt{32} 32
explain : There are two files :
"dir/subdir1/file1.ext" \texttt{"dir/subdir1/file1.ext"} "dir/subdir1/file1.ext", The length of the path 21 \texttt{21} 21.
"dir/subdir2/subsubdir2/file2.ext" \texttt{"dir/subdir2/subsubdir2/file2.ext"} "dir/subdir2/subsubdir2/file2.ext", The length of the path 32 \texttt{32} 32.
return 32 \texttt{32} 32, Because this is the longest path .
Example 3:
Input : input = "a" \texttt{input = "a"} input = "a"
Output : 0 \texttt{0} 0
explain : No files exist .
Example 4:
Input : input = "file1.txt \ nfile2.txt \ nlongfile.txt" \texttt{input = "file1.txt}\backslash\texttt{nfile2.txt}\backslash\texttt{nlongfile.txt"} input = "file1.txt\nfile2.txt\nlongfile.txt"
Output : 12 \texttt{12} 12
explain : There is... In the root directory 3 \texttt{3} 3 File .
Because the absolute path of anything in the root directory is the name itself , So the answer is "longfile.txt" \texttt{"longfile.txt"} "longfile.txt", The path length is 12 \texttt{12} 12.
Data range
- 1 ≤ input.length ≤ 10 4 \texttt{1} \le \texttt{input.length} \le \texttt{10}^\texttt{4} 1≤input.length≤104
- input \texttt{input} input May contain lowercase or uppercase English letters 、 A newline ‘ \ n’ \texttt{`}\backslash\texttt{n'} ‘\n’、 tabs ‘ \ t’ \texttt{`}\backslash\texttt{t'} ‘\t’、 spot ‘.’ \texttt{`.'} ‘.’、 Space ‘ ’ \texttt{` '} ‘ ’ And number
solution
Ideas and algorithms
Because each file and folder in the file system is separated by a newline , Therefore, the given input is separated into a string array according to the newline character , Each element in the string array is a file and folder , Just process each element in the string array .
For each file or folder , The number of tabs contained at the left end of the string is the level of the file or folder , The first 0 0 0 The layer is the file or folder under the root directory . If a file or folder is not in the root directory , Then there must be a superior folder , The number of levels of the parent folder is the number of levels of the current file or folder minus 1 1 1, And the position of the parent folder in the string array must be before the current file or folder .
For file systems with hierarchical relationships , Stack can be used to maintain the relative path length of each layer , At the same time, calculate the absolute path length of each layer . Because the stack stores the relative path length of each layer , Therefore, the location of each element in the stack reflects the level of the file or folder represented by this element , When there is i i i Element time , The element at the top of the stack represents the i − 1 i - 1 i−1 The relative path length of the file or folder of the layer .
Traverse each element in the string array in turn , Maintain the elements in the stack and the absolute path length during traversal , Initially, the stack is empty , The absolute path length is 0 0 0. The specific methods are as follows :
For the current element in the string array , Calculate the number of tabs contained at the left end of the string , Get the level of the file or folder and the relative path length of the file or folder ;
If the number of elements in the stack is greater than the current level , The top of the stack element is removed from the stack , Until the number of elements in the stack is equal to the current level , At the same time, the stack elements are subtracted from the absolute path length ;
Stack the current relative path length , At the same time, add the current relative path length to the absolute path length ;
If the current element is a file , That is, the string contains a dot , Then the longest absolute path length of the file is updated according to the current absolute path length .
When calculating the relative path length of a file or folder , One detail to note is , Except for the files and folders under the root directory , The relative path of each of the remaining files and folders contains a ‘/’ \text{`/'} ‘/’, Therefore, when calculating the relative path length of files and folders , The relative path length of the file or folder under the root directory is the length of the string , The relative path length of a file or folder not under the root directory is the length of the string minus the number of tabs plus 1 1 1( Add 1 1 1 Because it contains a ‘/’ \text{`/'} ‘/’).
After the traversal , You can get the longest absolute path length of the file .
Code
class Solution {
public int lengthLongestPath(String input) {
String[] array = input.split("\n");
int maxPathLength = 0;
int pathLength = 0;
Deque<Integer> stack = new ArrayDeque<Integer>();
int arrayLength = array.length;
for (int i = 0; i < arrayLength; i++) {
String str = array[i];
int level = 0;
while (str.charAt(level) == '\t') {
level++;
}
int length = str.length() - level + (level == 0 ? 0 : 1);
while (stack.size() > level) {
pathLength -= stack.pop();
}
stack.push(length);
pathLength += length;
if (str.indexOf('.') >= 0) {
maxPathLength = Math.max(maxPathLength, pathLength);
}
}
return maxPathLength;
}
}
Complexity analysis
Time complexity : O ( n ) O(n) O(n), among n n n Is string input \textit{input} input The length of . You need to traverse the string array once , The operation time of each character in the original input is O ( 1 ) O(1) O(1).
Spatial complexity : O ( n ) O(n) O(n), among n n n Is string input \textit{input} input The length of . Space complexity mainly depends on string array and stack , The space complexity is O ( n ) O(n) O(n).
边栏推荐
- 本地仓库导致的报错
- Pytorch的API总览
- Manifold learning
- Effectively solve the problem of garbled code when idea runs the web project (with detailed steps)
- Is the securities account presented by qiniu true? How to open it safely and reliably?
- I came to the library applet check-in process analysis
- 1. Mx6ul core module serial Ethernet test (VII)
- i. Mx6ull snvs power domain GPIO status hold verification
- 商业智能BI全解析,探寻BI本质与发展趋势
- Information System Project Manager - Chapter 10 communication management and stakeholder management examination questions over the years
猜你喜欢

TCP three handshakes and four waves

i. Mx6ull snvs power domain GPIO status hold verification

Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"

c# 单元测试

Wechat applet - get user location (longitude and latitude + city)

Prometheus + process exporter + grafana monitor the resource usage of the process

租户问题。
![[xxl-job] xxl-job learning](/img/2c/d3872983e4228a3ef52a9d1bef836e.png)
[xxl-job] xxl-job learning

Kaggle registration method to solve the problem of man-machine verification

1. Mx6ul core module uses serial can and buzzer test (XI)
随机推荐
ERROR: could not extract tar starting at offset 000000000000020980+9231072+2
Niuke net question brushing training (I)
National standard gb28181 protocol video platform easygbs message pop-up mode optimization
18.删除链表的倒数第n个节点
Is the securities account presented by qiniu true? How to open it safely and reliably?
Turn on the LED
1. Mx6ul core module uses serial can and buzzer test (XI)
[2019] [paper notes] tunable THz broadband absorption based on metamaterials——
Li Kou daily question - day 39 -67. Binary sum
i. Mx6ull snvs power domain GPIO status hold verification
图解B+树的插入过程
Recorded target detection NMS (non maximum suppression)
Product thinking drives the construction of R & D management tools
020-024 polymorphism review
I.MX6UL核心模块使用连载-WIFI测试 (八)
如何加速矩阵乘法
numpy.sort
数仓:银行业数仓的分层架构实践
IDEA运行web项目出现乱码问题有效解决(附详细步骤)
scipy.sparse.csr_matrix