Maximum Candies You Can Get from Boxes in C++


Suppose we have n boxes, here each box is given in the format like [status, candies, keys, containedBoxes] there are some constraints −

  • status[i]: an is 1 when box[i] is open and 0 when box[i] is closed.

  • candies[i]: is the number of candies in box[i].

  • keys[i]: is an array contains the indices of the boxes we can open with the key in box[i].

  • containedBoxes[i]: an array contains the indices of the boxes found in box[i].

We will start with some boxes given in initialBoxes array. We can take all the candies in any open box and we can use the keys in it to open new boxes and we also can use the boxes we find in it.

We have to find the maximum number of candies we can get following these rules mentioned above.

So, if the input is like status = [1,0,1,0], candies = [8,6,5,101], and keys = [[], [], [1], []], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0], then the output will be 19. This is because we will be initially given box 0. We will find 8 candies in it and boxes 1 and 2. Box 1 is not opened and we do not have a key for it so we will open box 2. we will find 5 candies and a key to box 1 in box 2. In box 1, you will find 6 candies and box 3 but we will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 8 + 5 + 6 = 19 candy.

To solve this, we will follow these steps −

  • ans := 0

  • Define one queue q

  • Define sets visited, opened, hasKey

  • for initialize i := 0, when i < size of ib, update (increase i by 1), do −

    • x := ib[i]

    • insert x into visited

    • if st[x] is same as 1, then −

      • ans := ans + cnt[x]

      • insert x into opened

      • insert x into q

  • while (not q is empty), do −

    • curr := first element of q

    • delete element from q

    • for initialize i := 0, when i < size of k[curr], update (increase i by 1), do−

      • x := k[curr, i]

      • insert x into hasKey

      • if x is not in opened and x is not in visited, then −

        • ans := ans + cnt[x]

        • insert x into q

        • insert x into opened

    • for initialize i := 0, when i < size of cb[curr], update (increase i by 1), do −

      • x := cb[curr, i]

      • insert x into visited

      • if x is not in opened and x is in hasKey or st[x] is same as 1), then −

        • insert x into opened

        • ans := ans + cnt[x]

        • insert x into q

  • return ans

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
   public:
   int maxCandies(vector<int>& st, vector<int>& cnt,
   vector<vector<int>>& k, vector<vector<int>>& cb, vector<int>& ib) {
      int ans = 0;
      queue<int> q;
      set<int> visited;
      set<int> opened;
      set<int> hasKey;
      for (int i = 0; i < ib.size(); i++) {
         int x = ib[i];
         visited.insert(x);
         if (st[x] == 1) {
            ans += cnt[x];
            opened.insert(x);
            q.push(x);
         }
      }
      while (!q.empty()) {
         int curr = q.front();
         q.pop();
         for (int i = 0; i < k[curr].size(); i++) {
            int x = k[curr][i];
            hasKey.insert(x);
            if (!opened.count(x) && visited.count(x)) {
               ans += cnt[x];
               q.push(x);
               opened.insert(x);
            }
         }
         for (int i = 0; i < cb[curr].size(); i++) {
            int x = cb[curr][i];
            visited.insert(x);
            if (!opened.count(x) && (hasKey.count(x) || st[x] ==
            1)) {
               opened.insert(x);
               ans += cnt[x];
               q.push(x);
            }
         }
      }
      return ans;
   }
};
main(){
   Solution ob;
   vector<int> v = {1,0,1,0}, v1 = {8,6,5,101}, v2 = {0};
   vector<vector<int>> v3 = {{},{},{1},{}}, v4 = {{1,2},{3},{0},{}};
   cout << (ob.maxCandies(v, v1, v3, v4, v2));
}

Input

{1,0,1,0}, {8,6,5,101}, {{},{},{1},{}}, {{1,2},{3},{0},{}}, {0}

Output

19

Updated on: 08-Jun-2020

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements