Count Candies from Unlockable Boxes
AirbnbSoftware Engineer · Technical ScreenLast updated
Determine the maximum number of candies you can collect given an initial set of boxes, where each box may be locked or unlocked, contain candies, contain keys to other boxes, and contain other boxes.
Problem Statement
You are given n boxes, numbered from 0 to n - 1. Each box i contains a certain number of candies, keys to other boxes, and other boxes inside it.
You are given the following arrays:
status: An array of integers wherestatus[i] = 1if boxiis already unlocked (open), andstatus[i] = 0if boxiis locked.candies: An array of integers wherecandies[i]is the number of candies in boxi.keys: A 2D array wherekeys[i]is a list of box IDs that can be unlocked using the keys found in boxi.contained_boxes: A 2D array wherecontained_boxes[i]is a list of box IDs contained inside boxi.initial_boxes: An array of box IDs that you initially have in your possession.
A box can only be opened if you currently have it in your possession and it is unlocked (either because it was initially unlocked or you obtained its key). Once you open a box:
- You collect all its candies.
- You obtain all the keys contained inside it (which unlock corresponding locked boxes).
- You obtain all the boxes contained inside it into your possession.
Return the total number of candies you can collect.
Function Signature
def max_candies(
status: list[int],
candies: list[int],
keys: list[list[int]],
contained_boxes: list[list[int]],
initial_boxes: list[int]
) -> int:
pass
Examples
Example 1
status = [1, 0, 1, 0]
candies = [7, 5, 4, 100]
keys = [[], [], [1], []]
contained_boxes = [[1, 2], [3], [], []]
initial_boxes = [0]
# Output: 16
Explanation:
- Initially, you have box
0, which is unlocked (status[0] == 1). You open it and get7candies, no keys, and boxes1and2. - Box
2is unlocked (status[2] == 1), so you open it and get4candies, a key to box1, and no new boxes. - Box
1is now possessed and unlocked via the key from box2. You open box1, getting5candies, no keys, and box3. - Box
3is possessed, but it is locked (status[3] == 0) and you have no key for it, so you cannot open it. - Total candies collected =
7 + 4 + 5 = 16.
Example 2
status = [1, 1, 1]
candies = [100, 1, 100]
keys = [[], [0, 2], []]
contained_boxes = [[], [], []]
initial_boxes = [1]
# Output: 1
Explanation:
- You start with box
1, which is unlocked. You get1candy and keys to boxes0and2. - However, you never obtain boxes
0or2in your possession, so you cannot open them. - Total candies collected =
1.
Constraints
n == status.length == candies.length == keys.length == contained_boxes.length1 <= n <= 1000status[i]is either0or1.0 <= candies[i] <= 10000 <= keys[i].length <= n0 <= contained_boxes[i].length <= n- All values in
keys[i]andcontained_boxes[i]are unique and in the range[0, n - 1]. 0 <= initial_boxes.length <= n- Values in
initial_boxesare unique and in the range[0, n - 1].
Hints
Hint 1
To open a box, two conditions must be met: you must possess the box, and the box must be unlocked.
Hint 2
Track which boxes you possess and which boxes are unlocked using boolean arrays or sets.
Hint 3
Use a queue to process boxes that are ready to be opened; whenever a key or a new box becomes available, check if both conditions are now satisfied for that box.
Follow-up questions
- How would you modify your solution if using a key consumed it (i.e., each key can only open one copy of a box if duplicate keys existed)?
- What if opening a box had a cost (candies required to open), and you wanted to maximize net candies collected?
- How would you detect if there are unreachable cycles of mutually dependent locked boxes that could have yielded more candies if one were opened?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.