Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int maxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes) {
- vector<bool> explored(status.size(), false);
- vector<bool> visited(status.size(), false);
- int ans = 0;
- queue<int> q;
- // Input the starting boxes in queue.
- for(int box: initialBoxes){
- if(status[box]){
- q.push(box);
- visited[box] = true;
- }
- else
- explored[box] = true;
- }
- // Pop from queue and input next boxes in queue.
- while(!q.empty()){
- int box = q.front(); q.pop();
- ans += candies[box];
- // Input boxes which are explored and their keys are found.
- for(int key: keys[box]){
- status[key] = 1;
- if(explored[key] && !visited[key]){
- q.push(key);
- visited[key] = true;
- }
- }
- // Input or explored next boxes based on keys.
- for(int nextBox: containedBoxes[box]){
- if(status[nextBox] && !visited[nextBox]){
- q.push(nextBox);
- visited[nextBox] = true;
- }
- else
- explored[nextBox] = true;
- }
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement