1298. 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
i
t
h
i^{th}
ith box is closed, - candies[i] is the number of candies in the
i
t
h
i^{th}
ith box, - keys[i] is a list of the labels of the boxes you can open after opening the
i
t
h
i^{th}
ith box. - containedBoxes[i] is a list of the boxes you found inside the
i
t
h
i^{th}
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
From: LeetCode Link: 1298. Maximum Candies You Can Get from Boxes
Solution:
Ideas:
Use BFS to process every box that you both possess and can open, while collecting new keys and contained boxes.
Code:
#include <stdlib.h>
#include <stdbool.h>
int maxCandies(int* status, int statusSize,
int* candies, int candiesSize,
int** keys, int keysSize, int* keysColSize,
int** containedBoxes, int containedBoxesSize,
int* containedBoxesColSize,
int* initialBoxes, int initialBoxesSize) {
int n = statusSize;
bool* haveBox = calloc(n, sizeof(bool));
bool* processed = calloc(n, sizeof(bool));
int* queue = malloc(n * sizeof(int));
int front = 0;
int rear = 0;
int total = 0;
/* Record the boxes initially owned. */
for (int i = 0; i < initialBoxesSize; i++) {
int box = initialBoxes[i];
haveBox[box] = true;
}
/* Add initially available open boxes. */
for (int i = 0; i < initialBoxesSize; i++) {
int box = initialBoxes[i];
if (status[box] == 1 && !processed[box]) {
processed[box] = true;
queue[rear++] = box;
}
}
while (front < rear) {
int box = queue[front++];
total += candies[box];
/* Collect keys. */
for (int i = 0; i < keysColSize[box]; i++) {
int next = keys[box][i];
status[next] = 1;
if (haveBox[next] && !processed[next]) {
processed[next] = true;
queue[rear++] = next;
}
}
/* Collect contained boxes. */
for (int i = 0; i < containedBoxesColSize[box]; i++) {
int next = containedBoxes[box][i];
haveBox[next] = true;
if (status[next] == 1 && !processed[next]) {
processed[next] = true;
queue[rear++] = next;
}
}
}
free(haveBox);
free(processed);
free(queue);
return total;
}
网硕互联帮助中心

评论前必须登录!
注册