File Encryption Plan
DatabricksPhone screenHard
You are given a file system tree with n nodes numbered 0 to n - 1. Node 0 is the root directory. parent[i] is the parent of node i, with parent[0] = -1 and parent[i] < i for i > 0. kind[i] is 0 for a directory and 1 for a file; files are leaves. encrypted[i] is true when file i is already encrypted. Directories are never marked encrypted.
Two kinds of calls encrypt files: a call on a file encrypts that one file, and a call on a directory encrypts every file in that directory's subtree at once. Build an EncryptionPlan object that answers queries about the cheapest way to get every file encrypted. Each test creates one instance and checks every method's return value.
Part 1: Cheapest time
Implement counts() and min_time(request, per_file).
counts() returns [encrypted, unencrypted]: the number of files already encrypted, then the number of files not encrypted.
min_time(request, per_file) returns the least total time needed to encrypt every file that is not yet encrypted. A call on one file takes request + per_file. A call on a directory takes request + per_file * f, where f is the total number of files in that directory's subtree, counting files that are already encrypted. A directory call encrypts every file in its subtree, so no further calls are needed below it. If no file needs encryption, return 0.
- Example 1
- init
[[-1,0,0,1,1],[0,0,1,1,1],[false,false,false,false,true]]- operations
[["counts",[]],["min_time",[5,2]],["min_time",[1,5]],["min_time",[100,1]]]- Output
[[1,2],11,12,103]
Why: Node 4 is the only encrypted file, so
counts()is[1, 2]. Withrequest = 5,per_file = 2, a call on the root costs5 + 3 * 2 = 11because 3 files lie below it, beating two single-file calls at7each. Withrequest = 1,per_file = 5, the two single-file calls cost6each,12in total, while the root call costs16. Withrequest = 100,per_file = 1, one root call costs103, cheaper than202for two file calls.
Constraints
1 <= len(parent) <= 10^5parent[0] = -10 <= parent[i] < ifori > 0kind[i]is0(directory) or1(file)- node
0is a directory and files have no children 0 <= request <= 10^90 <= per_file <= 10^91 <= len(operations) <= 10