1298. Maximum Candies You Can Get from Boxes #1762
-
Topics: You have
You are given an integer array Return the maximum number of candies you can get following the rules above. Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine the maximum number of candies we can collect by opening boxes according to specific rules. The boxes can be opened if they are initially open or if we have the corresponding key. When a box is opened, we collect its candies, obtain any keys inside it, and gain access to any boxes contained within it. The challenge is to efficiently traverse all possible boxes we can open starting from the initial set of boxes, considering dependencies between keys and boxes. Approach
Let's implement this solution in PHP: 1298. Maximum Candies You Can Get from Boxes <?php
/**
* @param Integer[] $status
* @param Integer[] $candies
* @param Integer[][] $keys
* @param Integer[][] $containedBoxes
* @param Integer[] $initialBoxes
* @return Integer
*/
function maxCandies($status, $candies, $keys, $containedBoxes, $initialBoxes) {
$n = count($status);
$hasBox = array_fill(0, $n, false);
$hasKey = array_fill(0, $n, false);
$visited = array_fill(0, $n, false);
$queue = new SplQueue();
foreach ($initialBoxes as $box) {
$hasBox[$box] = true;
if ($status[$box] == 1 || $hasKey[$box]) {
$visited[$box] = true;
$queue->enqueue($box);
}
}
$total = 0;
while (!$queue->isEmpty()) {
$box = $queue->dequeue();
$total += $candies[$box];
foreach ($keys[$box] as $key) {
if (!$hasKey[$key]) {
$hasKey[$key] = true;
if ($hasBox[$key] && !$visited[$key]) {
$visited[$key] = true;
$queue->enqueue($key);
}
}
}
foreach ($containedBoxes[$box] as $child) {
if (!$hasBox[$child]) {
$hasBox[$child] = true;
if (($status[$child] == 1 || $hasKey[$child]) && !$visited[$child]) {
$visited[$child] = true;
$queue->enqueue($child);
}
}
}
}
return $total;
}
// Example 1
$status = [1,0,1,0];
$candies = [7,5,4,100];
$keys = [[],[],[1],[]];
$containedBoxes = [[1,2],[3],[],[]];
$initialBoxes = [0];
echo maxCandies($status, $candies, $keys, $containedBoxes, $initialBoxes) . "\n"; // Output: 16
// Example 2
$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];
echo maxCandies($status, $candies, $keys, $containedBoxes, $initialBoxes) . "\n"; // Output: 6
?> Explanation:
This approach efficiently processes all accessible boxes by leveraging BFS to handle dependencies between keys and boxes dynamically, ensuring maximum candy collection. |
Beta Was this translation helpful? Give feedback.
We need to determine the maximum number of candies we can collect by opening boxes according to specific rules. The boxes can be opened if they are initially open or if we have the corresponding key. When a box is opened, we collect its candies, obtain any keys inside it, and gain access to any boxes contained within it. The challenge is to efficiently traverse all possible boxes we can open starting from the initial set of boxes, considering dependencies between keys and boxes.
Approach
Initialization: We maintain three arrays to track:
hasBox
: Indicates which boxes we currently possess.hasKey
: Indicates which keys we have obtained.visited
: Marks boxes that have been processed (open…