1298. Maximum Candies You Can Get from Boxes Hard
1/**
2 * [1298] Maximum Candies You Can Get from Boxes
3 *
4 * You have n boxes labeled from 0 to n - 1. You are given four arrays: status, candies, keys, and containedBoxes where:
5 *
6 * status[i] is 1 if the i^th box is open and 0 if the i^th box is closed,
7 * candies[i] is the number of candies in the i^th box,
8 * keys[i] is a list of the labels of the boxes you can open after opening the i^th box.
9 * containedBoxes[i] is a list of the boxes you found inside the i^th box.
10 *
11 * 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.
12 * Return the maximum number of candies you can get following the rules above.
13 *
14 * Example 1:
15 *
16 * Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0]
17 * Output: 16
18 * Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2.
19 * 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.
20 * 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.
21 * Total number of candies collected = 7 + 4 + 5 = 16 candy.
22 *
23 * Example 2:
24 *
25 * 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]
26 * Output: 6
27 * Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys.
28 * The total number of candies will be 6.
29 *
30 *
31 * Constraints:
32 *
33 * n == status.length == candies.length == keys.length == containedBoxes.length
34 * 1 <= n <= 1000
35 * status[i] is either 0 or 1.
36 * 1 <= candies[i] <= 1000
37 * 0 <= keys[i].length <= n
38 * 0 <= keys[i][j] < n
39 * All values of keys[i] are unique.
40 * 0 <= containedBoxes[i].length <= n
41 * 0 <= containedBoxes[i][j] < n
42 * All values of containedBoxes[i] are unique.
43 * Each box is contained in one box at most.
44 * 0 <= initialBoxes.length <= n
45 * 0 <= initialBoxes[i] < n
46 *
47 */
48pub struct Solution {}
49
50// problem: https://leetcode.com/problems/maximum-candies-you-can-get-from-boxes/
51// discuss: https://leetcode.com/problems/maximum-candies-you-can-get-from-boxes/discuss/?currentPage=1&orderBy=most_votes&query=
52
53// submission codes start here
54
55impl Solution {
56 pub fn max_candies(status: Vec<i32>, candies: Vec<i32>, keys: Vec<Vec<i32>>, contained_boxes: Vec<Vec<i32>>, initial_boxes: Vec<i32>) -> i32 {
57 0
58 }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn test_1298() {
69 }
70}
71
Back
© 2025 bowen.ge All Rights Reserved.