1298. Maximum Candies You Can Get from Boxes LeetCode Solution
In this guide, you will get 1298. Maximum Candies You Can Get from Boxes LeetCode Solution with the best time and space complexity. The solution to Maximum Candies You Can Get from Boxes problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Candies You Can Get from Boxes solution in C++
- Maximum Candies You Can Get from Boxes solution in Java
- Maximum Candies You Can Get from Boxes solution in Python
- Additional Resources

Problem Statement of Maximum Candies You Can Get from Boxes
You have n boxes labeled from 0 to n – 1. You are given four arrays: status, candies, keys, and containedBoxes where:
status[i] is 1 if the ith box is open and 0 if the ith box is closed,
candies[i] is the number of candies in the ith box,
keys[i] is a list of the labels of the boxes you can open after opening the ith box.
containedBoxes[i] is a list of the boxes you found inside the ith box.
You are given an integer array initialBoxes that contains the labels of the boxes you initially have. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it.
Return the maximum number of candies you can get following the rules above.
Example 1:
Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0]
Output: 16
Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2.
Box 1 is closed and you do not have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2.
In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed.
Total number of candies collected = 7 + 4 + 5 = 16 candy.
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
Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys.
The total number of candies will be 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
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(n)
1298. Maximum Candies You Can Get from Boxes LeetCode Solution in C++
class Solution {
public:
int maxCandies(vector<int>& status, vector<int>& candies,
vector<vector<int>>& keys, vector<vector<int>>& containedBoxes,
vector<int>& initialBoxes) {
int ans = 0;
queue<int> q;
vector<bool> reachedClosedBoxes(status.size());
auto pushBoxesIfPossible = [&status, &q,
&reachedClosedBoxes](const vector<int>& boxes) {
for (const int box : boxes)
if (status[box])
q.push(box);
else
reachedClosedBoxes[box] = true;
};
pushBoxesIfPossible(initialBoxes);
while (!q.empty()) {
const int currBox = q.front();
q.pop();
// Add the candies.
ans += candies[currBox];
// Push `reachedClosedBoxes` by `key` obtained in this turn and change
// their statuses.
for (const int key : keys[currBox]) {
if (!status[key] && reachedClosedBoxes[key])
q.push(key);
status[key] = 1; // boxes[key] is now open.
}
// Push the boxes contained in `currBox`.
pushBoxesIfPossible(containedBoxes[currBox]);
}
return ans;
}
};
/* code provided by PROGIEZ */
1298. Maximum Candies You Can Get from Boxes LeetCode Solution in Java
class Solution {
public int maxCandies(int[] status, int[] candies, int[][] keys, int[][] containedBoxes,
int[] initialBoxes) {
int ans = 0;
Queue<Integer> q = new ArrayDeque<>();
boolean[] reachedClosedBoxes = new boolean[status.length];
pushBoxesIfPossible(initialBoxes, status, q, reachedClosedBoxes);
while (!q.isEmpty()) {
final int currBox = q.poll();
// Add the candies.
ans += candies[currBox];
// Push `reachedClosedBoxes` by `key` obtained in this turn and change
// their statuses.
for (final int key : keys[currBox]) {
if (status[key] == 0 && reachedClosedBoxes[key])
q.offer(key);
status[key] = 1; // boxes[key] is now open.
}
// Push the boxes contained in `currBox`.
pushBoxesIfPossible(containedBoxes[currBox], status, q, reachedClosedBoxes);
}
return ans;
}
private void pushBoxesIfPossible(int[] boxes, int[] status, Queue<Integer> q,
boolean[] reachedClosedBoxes) {
for (final int box : boxes)
if (status[box] == 1)
q.offer(box);
else
reachedClosedBoxes[box] = true;
}
}
// code provided by PROGIEZ
1298. Maximum Candies You Can Get from Boxes LeetCode Solution in Python
class Solution:
def maxCandies(
self,
status: list[int],
candies: list[int],
keys: list[list[int]],
containedBoxes: list[list[int]],
initialBoxes: list[int],
) -> int:
ans = 0
q = collections.deque()
reachedClosedBoxes = [0] * len(status)
def pushBoxesIfPossible(boxes: list[int]) -> None:
for box in boxes:
if status[box]:
q.append(box)
else:
reachedClosedBoxes[box] = True
pushBoxesIfPossible(initialBoxes)
while q:
currBox = q.popleft()
# Add the candies.
ans += candies[currBox]
# Push `reachedClosedBoxes` by `key` obtained in this turn and change
# their statuses.
for key in keys[currBox]:
if not status[key] and reachedClosedBoxes[key]:
q.append(key)
status[key] = 1 # boxes[key] is now open
# Push the boxes contained in `currBox`.
pushBoxesIfPossible(containedBoxes[currBox])
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.