Skip to content

Practice Maximum Candies You Can Get from Boxes

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1298
LeetCode Problem

Maximum Candies You Can Get from Boxes

Problem Statement

Boxes are labeled 0 through n - 1. status[i] is 1 if box i is open and 0 if closed; candies[i] is its candy count; keys[i] lists boxes its keys can unlock; and containedBoxes[i] lists boxes inside it. You initially possess the boxes in initialBoxes. Whenever you can open a box you possess, collect its candies, keys, and contained boxes. A key alone does not give you the box it unlocks. Return the maximum candies you can collect.

Source: Maximum Candies You Can Get from Boxes on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0]
Output
16

# Example 2

Input
status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0]
Output
6

Constraints

n == status.length == candies.length == keys.length == containedBoxes.length
1 <= n <= 1000
status[i] is either 0 or 1.
1 <= candies[i] <= 1000
0 <= keys[i].length <= n
0 <= keys[i][j] < n
All values of keys[i] are unique.
0 <= containedBoxes[i].length <= n
0 <= containedBoxes[i][j] < n
All values of containedBoxes[i] are unique.
Each box is contained in one box at most.
0 <= initialBoxes.length <= n
0 <= initialBoxes[i] < n

Related LeetCode Problems

Practice more problems similar to Maximum Candies You Can Get from Boxes.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.