File Encryption Plan

Asked at DatabricksPhone screen

Reported by the problem's creator, not verified

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.

Examples

  1. Example 1

    Input: 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]

    Explanation: Node 4 is the only encrypted file, so counts() is [1, 2]. With request = 5, per_file = 2, a call on the root costs 5 + 3 * 2 = 11 because 3 files lie below it, beating two single-file calls at 7 each. With request = 1, per_file = 5, the two single-file calls cost 6 each, 12 in total, while the root call costs 16. With request = 100, per_file = 1, one root call costs 103, cheaper than 202 for two file calls.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.