File Sizes Under Directory Paths
Problem statement
A filesystem tree is represented by parallel arrays paths and sizes. Entry i is a directory when sizes[i] == -1; otherwise it is a file with that nonnegative size.
For every path in queries, return the total size of all files at or below that path. A file query returns its own size. A directory includes all its descendant files. An empty directory has total size 0. Return -1 for a path that is absent from the tree.
Return results in query order. Path matching is case-sensitive and follows directory boundaries: /ab is not inside /a. The input entries may be in any order, and queries may repeat.
Function
fileSizesByPath(paths: String[], sizes: int[], queries: String[]) → long[]Examples
Example 1
paths = ["/","/home","/home/me","/home/me/foo.txt","/home/me/abs.txt","/home/haha.css"]sizes = [-1,-1,-1,231,443,52]queries = ["/home","/home/me","/home/me/foo.txt","/missing"]return = [726,674,231,-1]/home/me contains files of sizes 231 and 443, totaling 674. Adding haha.css gives /home a total of 726. A file query returns its own size, and the missing path returns -1.
Example 2
paths = ["/","/empty","/zero.txt"]sizes = [-1,-1,0]queries = ["/empty","/","/zero.txt"]return = [0,0,0]The empty directory has total size 0. The only file also has size 0, so the root total is 0.
Example 3
paths = ["/","/a","/ab","/a/f","/ab/g"]sizes = [-1,-1,-1,5,7]queries = ["/a","/ab","/"]return = [5,7,12]/a/f belongs to /a, while /ab/g belongs to /ab. A shared text prefix does not make /ab a child of /a. The root contains both files and totals 12.
Constraints
1 <= paths.length <= 3000paths.length == sizes.length0 <= queries.length <= 30001 <= paths[i].length <= 2001 <= queries[i].length <= 200-1 <= sizes[i] <= 1000000000- All paths are absolute and canonical. The root is
/. Other paths have nonempty slash-separated segments containing only ASCII letters, digits, periods, underscores, and hyphens; a segment cannot be.or.., and there is no trailing slash. - The entries have unique paths, include the root as a directory, and include each non-root entry's parent as a directory. Files have no children. A query may name an absent path.