Problem · Tree

Encrypted File Count and Minimum Time

Learn this problem
HardDatabricks logoDatabricksFULLTIMEPHONE SCREEN

Problem 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 -1 for the root.
  • type[i] is 0 for a directory or 1 for a file.
  • encrypted[i] gives a file's initial encryption state. It is false for 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 + fileTime and encrypts that file.
  • A directory call costs requestTime + N * fileTime and encrypts every currently unencrypted file in that directory's subtree. For this exercise, N is 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 <= 200000
  • parent[0] = -1; for i > 0, 0 <= parent[i] < i.
  • The root is a directory, and every file node has no children.
  • encrypted[i] is false for every directory.
  • 0 <= requestTime, fileTime <= 10^9.
  • The minimum time fits in a signed 64-bit integer.

More Databricks problems

drafts saved locally
public long[] fileEncryptionSummary(int[] parent, int[] type, boolean[] encrypted, long requestTime, long fileTime) {
    // Write your solution here.
}
parent[-1,0,0,1,1]
type[0,0,1,1,1]
encrypted[false,false,false,false,true]
requestTime5
fileTime2
expected[1,2,11]
checking account