Encrypted File Count and Minimum Time
Learn this problemProblem statement
A rooted file-system tree contains directory nodes and file nodes. Node 0 is the root. For each node i:
parent[i]is its parent, or-1for the root.type[i]is0for a directory or1for a file.encrypted[i]gives a file's initial encryption state. It isfalsefor directories.
First, count the initially encrypted and initially unencrypted files.
Then make every file encrypted using these operations:
- A file call on an unencrypted file costs
requestTime + fileTimeand encrypts that file. - A directory call costs
requestTime + N * fileTimeand encrypts every currently unencrypted file in that directory's subtree. For this exercise,Nis the total number of file nodes in the subtree, including files that were already encrypted.
Each initially unencrypted file must be encrypted exactly once. Return a three-element array [encryptedCount, unencryptedCount, minimumTime], where the counts describe the initial tree and minimumTime is the least total operation cost.
Function
fileEncryptionSummary(parent: int[], type: int[], encrypted: boolean[], requestTime: long, fileTime: long) → long[]Examples
Example 1
parent = [-1,0,0,1,1]type = [0,0,1,1,1]encrypted = [false,false,false,false,true]requestTime = 5fileTime = 2return = [1,2,11]Initially, one file is encrypted and two are not. Processing directory 1 in bulk costs 9, while encrypting only its unencrypted file costs 7. At the root, one bulk call costs 5 + 3 * 2 = 11, which is less than combining the best child plans for a total of 14.
Example 2
parent = [-1,0,0]type = [0,1,1]encrypted = [false,true,true]requestTime = 4fileTime = 3return = [2,0,0]Both files are already encrypted, so no operation is needed.
Constraints
1 <= parent.length = type.length = encrypted.length <= 200000parent[0] = -1; fori > 0,0 <= parent[i] < i.- The root is a directory, and every file node has no children.
encrypted[i]isfalsefor every directory.0 <= requestTime, fileTime <= 10^9.- The minimum time fits in a signed 64-bit integer.