
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
- Related Articles
- Maximum number of candies that can be bought in C
- Program to find maximum number of boxes we can fit inside another boxes in python
- Maximum Points You Can Obtain from Cards in C++
- Max Difference You Can Get From Changing an Integer in C++
- How can you get XRP?
- Program to find maximum coins we can get from disappearing coins matrix in Python
- Find the minimum and maximum amount to buy all N candies in Python
- How you can get a true/false from a Python regular expressions?
- Beautiful hand crafted articles like boxes and toys are made of paper pulp in our country. Can you explain how paper pulp which is made from paper can be used to make hard boxes and other articles?
- Distribute Candies in C++
- C++ program to check whether we can distribute bags so that two friends will get same amount of candies
- Get MySQL maximum value from 3 different columns?
- Get maximum of Nth column from tuple list in Python
- Python Pandas - Get the maximum value from Ordered CategoricalIndex
- Get maximum date from a list of varchar dates in MySQL
