LeetCode 1298. Maximum Candies You Can Get from Boxes

原题链接在这里:https://leetcode.com/problems/maximum-candies-you-can-get-from-boxes/description/

题目:

You have n boxes labeled from 0 to n - 1. You are given four arrays: statuscandieskeys, 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

题解:

We can get the candies when the box is reachable & box is open.

To track this, when box is reachable, status += 1000, when box is open status += 1.

Perform BFS for the process.

When reach a box, after status += 1000, if its status is > 1000, that means this box is open. Add it to the que.

When get a key, after status += 1, if its status is 1001, add it to the que. Note here it doesn't check > 1000, since there could be duplciate keys.

Time Complexity: O(n + m). n is the total number of boxes, m is total number of keys.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int maxCandies(int[] status, int[] candies, int[][] keys, int[][] containedBoxes, int[] initialBoxes) {
 3         int res = 0;
 4         LinkedList<Integer> que = new LinkedList<>();
 5         for(int ini : initialBoxes){
 6             status[ini] += 1000;
 7             if(status[ini] > 1000){
 8                 que.add(ini);
 9             }
10         }
11 
12         while(!que.isEmpty()){
13             int cur = que.poll();
14             res += candies[cur];
15             for(int key : keys[cur]){
16                 status[key] += 1;
17                 
18                 if(status[key] == 1001){
19                     que.add(key);
20                 }
21             }
22 
23             for(int box : containedBoxes[cur]){
24                 status[box] += 1000;
25                 if(status[box] > 1000){
26                     que.add(box);
27                 }
28             }
29         }
30 
31         return res;
32     }
33 }

 

posted @ 2024-07-16 13:43  Dylan_Java_NYC  阅读(9)  评论(0编辑  收藏  举报