Candies Reachable Through Possessed Boxes and Keys

Quick Overview

Candies Reachable Through Possessed Boxes and Keys. There are n labeled boxes. status[i] says whether box i is initially open; candies[i] is its candy count.

Candies Reachable Through Possessed Boxes and Keys

Company: Airbnb

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem There are n labeled boxes. status[i] says whether box i is initially open; candies[i] is its candy count. After opening a possessed box, collect its candies, keys[i] for unlocking named boxes, and containedBoxes[i] for acquiring more boxes. Starting only with initialBoxes, return the greatest total candies obtainable. A key does not itself give possession of its box. ```cpp class Solution { public: int maxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes); }; ``` ### Examples **Example 1:** ```text Input: status = [1,0,0], candies = [2,5,9], keys = [[1],[],[]], containedBoxes = [[1,2],[],[]], initialBoxes = [0] Output: 7 Explanation: Box0 supplies box1 and its key, so collect2+5. Box2 is possessed but stays locked. ``` **Example 2:** ```text Input: status = [1,0], candies = [4,6], keys = [[1],[]], containedBoxes = [[],[]], initialBoxes = [0] Output: 4 Explanation: A key to box1 is found, but box1 itself is never acquired. ``` ### Constraints - status,candies,keys,containedBoxes have a common length n, with `1 <= n <= 1000`. - Status entries are 0 or 1; each candy count is an integer in `[1,1000]`. - Each key list and each contained-box list has length 0 to n, contains labels in `[0,n-1]`, and has no repeated label within that list. - Each box is contained in at most one box. - initialBoxes has length 0 to n and all its labels lie in `[0,n-1]`; no additional uniqueness promise is added. ### Contract considerations Preserve all five original array arguments. Keys unlock boxes; they do not create those boxes or grant their contents remotely. ```hint Read the boundary condition Does finding a key also acquire its box, and can discovering the same box again add its candies again? ``` ### Boundary reminders Preserve all five original array arguments. Keys unlock boxes; they do not create those boxes or grant their contents remotely.

Overview: Candies Reachable Through Possessed Boxes and Keys. There are n labeled boxes. status[i] says whether box i is initially open; candies[i] is its candy count.

Airbnb logo
Airbnb
Source window: 0–6 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

There are n labeled boxes. status[i] says whether box i is initially open; candies[i] is its candy count. After opening a possessed box, collect its candies, keys[i] for unlocking named boxes, and containedBoxes[i] for acquiring more boxes. Starting only with initialBoxes, return the greatest total candies obtainable. A key does not itself give possession of its box.

class Solution {
public:
    int maxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes);
};

Examples

Example 1:

Input: status = [1,0,0], candies = [2,5,9], keys = [[1],[],[]], containedBoxes = [[1,2],[],[]], initialBoxes = [0]
Output: 7
Explanation: Box0 supplies box1 and its key, so collect2+5. Box2 is possessed but stays locked.

Example 2:

Input: status = [1,0], candies = [4,6], keys = [[1],[]], containedBoxes = [[],[]], initialBoxes = [0]
Output: 4
Explanation: A key to box1 is found, but box1 itself is never acquired.

Constraints

  • status,candies,keys,containedBoxes have a common length n, with 1 <= n <= 1000 .
  • Status entries are 0 or 1; each candy count is an integer in [1,1000] .
  • Each key list and each contained-box list has length 0 to n, contains labels in [0,n-1] , and has no repeated label within that list.
  • Each box is contained in at most one box.
  • initialBoxes has length 0 to n and all its labels lie in [0,n-1] ; no additional uniqueness promise is added.

Contract considerations

Preserve all five original array arguments. Keys unlock boxes; they do not create those boxes or grant their contents remotely.

Boundary reminders

Preserve all five original array arguments. Keys unlock boxes; they do not create those boxes or grant their contents remotely.