Maximum Candies You Can Get from Boxes - Problem

You have n boxes labeled from 0 to n - 1. You are given four arrays:

  • status[i] is 1 if the ith box is open and 0 if closed
  • candies[i] is the number of candies in the ith box
  • keys[i] is a list of labels of boxes you can open after opening the ith box
  • containedBoxes[i] is a list of boxes you find inside the ith box

You are given an integer array initialBoxes that contains the labels of boxes you initially have.

Rules:

  • You can take all candies from any open box
  • You can use keys found in opened boxes to open new boxes
  • You can use boxes found inside opened boxes

Return the maximum number of candies you can get following the rules above.

Input & Output

Example 1 — Basic Case
$ Input: status = [1,0,1,0], candies = [7,5,4,9], keys = [[],[],[1],[]], containedBoxes = [[],[],[],[]], initialBoxes = [0]
Output: 16
💡 Note: Start with box 0 (open) → collect 7 candies. Box 2 is also open → collect 4 candies. Box 0 has no keys, box 2 gives key to box 1 → open box 1 and collect 5 candies. Total: 7+4+5=16
Example 2 — Keys Required
$ Input: status = [1,0,0,0], candies = [1,1,1,1], keys = [[1,2],[3],[],[]], containedBoxes = [[],[],[],[]], initialBoxes = [0]
Output: 4
💡 Note: Box 0 is open → collect 1 candy and get keys [1,2]. Open boxes 1,2 → collect 2 more candies and get key [3]. Open box 3 → collect 1 candy. Total: 1+1+1+1=4
Example 3 — Contained Boxes
$ Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[1,2],[],[]], initialBoxes = [0]
Output: 7
💡 Note: Start with box 0 → collect 2 candies and find boxes [1,2]. Boxes 1,2 are both open → collect 3+2=5 candies. Total: 2+3+2=7

Constraints

  • 1 ≤ status.length ≤ 1000
  • status.length == candies.length == keys.length == containedBoxes.length == n
  • 0 ≤ status[i] ≤ 1
  • 1 ≤ candies[i] ≤ 1000
  • 0 ≤ keys[i].length ≤ 1000
  • 0 ≤ keys[i][j] < n
  • 0 ≤ containedBoxes[i].length ≤ 1000
  • 0 ≤ containedBoxes[i][j] < n
  • All values in keys[i] are unique
  • All values in containedBoxes[i] are unique
  • 1 ≤ initialBoxes.length ≤ 1000
  • 0 ≤ initialBoxes[i] < n

Visualization

Tap to expand
Maximum Candies from Boxes INPUT Boxes (4 total): Box 0 OPEN 7 candies Box 1 CLOSED 5 candies Box 2 OPEN 4 candies Box 3 CLOSED 9 candies Key to 1 status = [1,0,1,0] candies = [7,5,4,9] keys = [[],[],[1],[]] containedBoxes = [[],[],[],[]] initialBoxes = [0] Open Box Closed Box Start with: Box 0 (already open) Total candies = 0 ALGORITHM (BFS) 1 Process Box 0 Open, collect 7 candies No keys, no boxes inside Total: 7 2 Check other boxes Box 2 is open (status=1) But we don't have it 3 Find Box 2 (if possible) Process Box 2: collect 4 Find key to Box 1 Total: 11 4 Use key on Box 1 Open Box 1, collect 5 No more boxes/keys Total: 16 BFS Queue: 0 --> 2 --> 1 FINAL RESULT Boxes Opened: Box 0 7 Box 2 4 Box 1 5 Box NOT Opened: Box 3 No key (9 candies) Candy Calculation: 7 + 4 + 5 = 16 (Box 3 unreachable) OUTPUT 16 Key Insight: Use BFS to process boxes iteratively. Track: (1) boxes we HAVE, (2) keys we HAVE. A box can be opened when we have it AND (it's already open OR we have its key). TutorialsPoint - Maximum Candies You Can Get from Boxes | BFS Approach
Asked in
Google 45 Microsoft 32 Amazon 28 Facebook 22
34.6K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen