Problem Statement
You must upload a list of files to some server. For every file, a folder where it must be uploaded is specified. You can't jump from folder to folder; you can only move one level up or to one of the current folder's children.
Uploading one file takes uploadTime milliseconds. Moving from one folder to its child takes 1 millisecond (moving one level up is fast, so we can neglect it). Initially you are located in the root folder. Given a String[] folders, containing the folders to which each file must be uploaded, return the minimal time needed to upload all files. Each element of folders represents the location of a single file, with subfolders delimited by forward slashes. For example, if some element of folders is equal to "/a/b" this means that file should be uploaded to folder b, which is a child of folder a, which is located in the root folder. Note, that all paths are absolute, therefore all elements of folders will start with a slash.
Uploading one file takes uploadTime milliseconds. Moving from one folder to its child takes 1 millisecond (moving one level up is fast, so we can neglect it). Initially you are located in the root folder. Given a String[] folders, containing the folders to which each file must be uploaded, return the minimal time needed to upload all files. Each element of folders represents the location of a single file, with subfolders delimited by forward slashes. For example, if some element of folders is equal to "/a/b" this means that file should be uploaded to folder b, which is a child of folder a, which is located in the root folder. Note, that all paths are absolute, therefore all elements of folders will start with a slash.
Definition
- Class:
- FoldersParser
- Method:
- minimalTime
- Parameters:
- String[], int
- Returns:
- int
- Method signature:
- int minimalTime(String[] folders, int uploadTime)
- (be sure your method is public)
Notes
- Elements of folders may end with a '/'. As in real file systems, "/a/" and "/a" represent the same folder.
Constraints
- uploadTime will be between 1 and 100, inclusive.
- folders will contain between 1 and 50 elements, inclusive.
- Each element of folders contains between 1 and 50 characters, inclusive.
- Each element of folders will contain only lower-case letters and forward slashes ('/').
- Each element of folders will start with a slash and will not contain two consecutive slashes.
Examples
{"/", "/subfolder/subsub", "/subfolder/secondsub"}5
Returns: 18
The fastest way to upload these 3 files is to upload the first file to the root directory, go two levels down to /subfolder/subsub, upload the second file, return one level up and go one level down to secondsub to upload the third file. Overall, you spent 5*3 = 15 seconds for uploading and 3 seconds for 3 moves to subfolders.{"/", "/subfolder/subsub", "/subfolder/secondsub", "/subfolder/subsub/", "/subfolder/subsub"}1
Returns: 8
Note that "/subfolder/subsub/" and "/subfolder/subsub" represent the same folder.{"/", "/", "/", "/a/b/c/d/e", "/e/d/c/b/a", "/", "/", "/", "/", "/"}1
Returns: 20
You should perform 10 moves to subfolders and upload 10 files.