1298: Maximum-Candies-You-Can-Get-from-Boxes
Hard
The hardest part of this problem was actually understanding what they wanted you to solve. Implementation wise, I would rate this as a low-medium.
It is basically just a breadth-first search
implementation, where you only add to the queue
if an
un-opened box becomes openable. This only occurs
when:
Then, we simply just find the totalCandy
by
summing the candy present in all these opened boxes, and return
it.
Code:
class Solution {
public:
int maxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes) {
<int> boxStatus (status.size(), 0);
vector<int> openableBoxes;
queue
for (int& i : initialBoxes) {
[i] = 1;
boxStatusif (status[i] == 1) {
.push(i);
openableBoxes}
}
int totalCandy = 0;
while(!openableBoxes.empty()) {
int box = openableBoxes.front();
.pop();
openableBoxes
+= candies[box];
totalCandy for (int& i : keys[box]) {
++status[i];
if (status[i] == 1 && boxStatus[i] >= 1) {
.push(i);
openableBoxes}
}
for (int& i : containedBoxes[box]) {
++boxStatus[i];
if (boxStatus[i] == 1 && status[i] >= 1) {
.push(i);
openableBoxes}
}
}
return totalCandy;
}
};