Skip to content

Commit cf2d2da

Browse files
committed
feat: add solutions to lc problem: No.1298
No.1298.Maximum Candies You Can Get from Boxes
1 parent b87e47a commit cf2d2da

File tree

6 files changed

+445
-2
lines changed

6 files changed

+445
-2
lines changed

solution/1200-1299/1298.Maximum Candies You Can Get from Boxes/README.md

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,173 @@
8383

8484
<!-- 这里可写通用的实现逻辑 -->
8585

86+
**方法一:BFS**
87+
8688
<!-- tabs:start -->
8789

8890
### **Python3**
8991

9092
<!-- 这里可写当前语言的特殊实现逻辑 -->
9193

9294
```python
93-
95+
class Solution:
96+
def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int:
97+
q = deque([i for i in initialBoxes if status[i] == 1])
98+
ans = sum(candies[i] for i in initialBoxes if status[i] == 1)
99+
has = set(initialBoxes)
100+
took = {i for i in initialBoxes if status[i] == 1}
101+
102+
while q:
103+
i = q.popleft()
104+
for k in keys[i]:
105+
status[k] = 1
106+
if k in has and k not in took:
107+
ans += candies[k]
108+
took.add(k)
109+
q.append(k)
110+
for j in containedBoxes[i]:
111+
has.add(j)
112+
if status[j] and j not in took:
113+
ans += candies[j]
114+
took.add(j)
115+
q.append(j)
116+
return ans
94117
```
95118

96119
### **Java**
97120

98121
<!-- 这里可写当前语言的特殊实现逻辑 -->
99122

100123
```java
124+
class Solution {
125+
public int maxCandies(int[] status, int[] candies, int[][] keys, int[][] containedBoxes, int[] initialBoxes) {
126+
int ans = 0;
127+
int n = status.length;
128+
boolean[] has = new boolean[n];
129+
boolean[] took = new boolean[n];
130+
Deque<Integer> q = new ArrayDeque<>();
131+
for (int i : initialBoxes) {
132+
has[i] = true;
133+
if (status[i] == 1) {
134+
ans += candies[i];
135+
took[i] = true;
136+
q.offer(i);
137+
}
138+
}
139+
while (!q.isEmpty()) {
140+
int i = q.poll();
141+
for (int k : keys[i]) {
142+
status[k] = 1;
143+
if (has[k] && !took[k]) {
144+
ans += candies[k];
145+
took[k] = true;
146+
q.offer(k);
147+
}
148+
}
149+
for (int j : containedBoxes[i]) {
150+
has[j] = true;
151+
if (status[j] == 1 && !took[j]) {
152+
ans += candies[j];
153+
took[j] = true;
154+
q.offer(j);
155+
}
156+
}
157+
}
158+
return ans;
159+
}
160+
}
161+
```
162+
163+
### **C++**
164+
165+
```cpp
166+
class Solution {
167+
public:
168+
int maxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes) {
169+
int ans = 0;
170+
int n = status.size();
171+
vector<bool> has(n);
172+
vector<bool> took(n);
173+
queue<int> q;
174+
for (int& i : initialBoxes)
175+
{
176+
has[i] = true;
177+
if (status[i])
178+
{
179+
ans += candies[i];
180+
took[i] = true;
181+
q.push(i);
182+
}
183+
}
184+
while (!q.empty())
185+
{
186+
int i = q.front();
187+
q.pop();
188+
for (int k : keys[i])
189+
{
190+
status[k] = 1;
191+
if (has[k] && !took[k])
192+
{
193+
ans += candies[k];
194+
took[k] = true;
195+
q.push(k);
196+
}
197+
}
198+
for (int j : containedBoxes[i])
199+
{
200+
has[j] = true;
201+
if (status[j] && !took[j])
202+
{
203+
ans += candies[j];
204+
took[j] = true;
205+
q.push(j);
206+
}
207+
}
208+
}
209+
return ans;
210+
}
211+
};
212+
```
101213
214+
### **Go**
215+
216+
```go
217+
func maxCandies(status []int, candies []int, keys [][]int, containedBoxes [][]int, initialBoxes []int) int {
218+
ans := 0
219+
n := len(status)
220+
has := make([]bool, n)
221+
took := make([]bool, n)
222+
var q []int
223+
for _, i := range initialBoxes {
224+
has[i] = true
225+
if status[i] == 1 {
226+
ans += candies[i]
227+
took[i] = true
228+
q = append(q, i)
229+
}
230+
}
231+
for len(q) > 0 {
232+
i := q[0]
233+
q = q[1:]
234+
for _, k := range keys[i] {
235+
status[k] = 1
236+
if has[k] && !took[k] {
237+
ans += candies[k]
238+
took[k] = true
239+
q = append(q, k)
240+
}
241+
}
242+
for _, j := range containedBoxes[i] {
243+
has[j] = true
244+
if status[j] == 1 && !took[j] {
245+
ans += candies[j]
246+
took[j] = true
247+
q = append(q, j)
248+
}
249+
}
250+
}
251+
return ans
252+
}
102253
```
103254

104255
### **...**

solution/1200-1299/1298.Maximum Candies You Can Get from Boxes/README_EN.md

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,169 @@ The total number of candies will be 6.
5959

6060
## Solutions
6161

62+
BFS.
63+
6264
<!-- tabs:start -->
6365

6466
### **Python3**
6567

6668
```python
67-
69+
class Solution:
70+
def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int:
71+
q = deque([i for i in initialBoxes if status[i] == 1])
72+
ans = sum(candies[i] for i in initialBoxes if status[i] == 1)
73+
has = set(initialBoxes)
74+
took = {i for i in initialBoxes if status[i] == 1}
75+
76+
while q:
77+
i = q.popleft()
78+
for k in keys[i]:
79+
status[k] = 1
80+
if k in has and k not in took:
81+
ans += candies[k]
82+
took.add(k)
83+
q.append(k)
84+
for j in containedBoxes[i]:
85+
has.add(j)
86+
if status[j] and j not in took:
87+
ans += candies[j]
88+
took.add(j)
89+
q.append(j)
90+
return ans
6891
```
6992

7093
### **Java**
7194

7295
```java
96+
class Solution {
97+
public int maxCandies(int[] status, int[] candies, int[][] keys, int[][] containedBoxes, int[] initialBoxes) {
98+
int ans = 0;
99+
int n = status.length;
100+
boolean[] has = new boolean[n];
101+
boolean[] took = new boolean[n];
102+
Deque<Integer> q = new ArrayDeque<>();
103+
for (int i : initialBoxes) {
104+
has[i] = true;
105+
if (status[i] == 1) {
106+
ans += candies[i];
107+
took[i] = true;
108+
q.offer(i);
109+
}
110+
}
111+
while (!q.isEmpty()) {
112+
int i = q.poll();
113+
for (int k : keys[i]) {
114+
status[k] = 1;
115+
if (has[k] && !took[k]) {
116+
ans += candies[k];
117+
took[k] = true;
118+
q.offer(k);
119+
}
120+
}
121+
for (int j : containedBoxes[i]) {
122+
has[j] = true;
123+
if (status[j] == 1 && !took[j]) {
124+
ans += candies[j];
125+
took[j] = true;
126+
q.offer(j);
127+
}
128+
}
129+
}
130+
return ans;
131+
}
132+
}
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
int maxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes) {
141+
int ans = 0;
142+
int n = status.size();
143+
vector<bool> has(n);
144+
vector<bool> took(n);
145+
queue<int> q;
146+
for (int& i : initialBoxes)
147+
{
148+
has[i] = true;
149+
if (status[i])
150+
{
151+
ans += candies[i];
152+
took[i] = true;
153+
q.push(i);
154+
}
155+
}
156+
while (!q.empty())
157+
{
158+
int i = q.front();
159+
q.pop();
160+
for (int k : keys[i])
161+
{
162+
status[k] = 1;
163+
if (has[k] && !took[k])
164+
{
165+
ans += candies[k];
166+
took[k] = true;
167+
q.push(k);
168+
}
169+
}
170+
for (int j : containedBoxes[i])
171+
{
172+
has[j] = true;
173+
if (status[j] && !took[j])
174+
{
175+
ans += candies[j];
176+
took[j] = true;
177+
q.push(j);
178+
}
179+
}
180+
}
181+
return ans;
182+
}
183+
};
184+
```
73185
186+
### **Go**
187+
188+
```go
189+
func maxCandies(status []int, candies []int, keys [][]int, containedBoxes [][]int, initialBoxes []int) int {
190+
ans := 0
191+
n := len(status)
192+
has := make([]bool, n)
193+
took := make([]bool, n)
194+
var q []int
195+
for _, i := range initialBoxes {
196+
has[i] = true
197+
if status[i] == 1 {
198+
ans += candies[i]
199+
took[i] = true
200+
q = append(q, i)
201+
}
202+
}
203+
for len(q) > 0 {
204+
i := q[0]
205+
q = q[1:]
206+
for _, k := range keys[i] {
207+
status[k] = 1
208+
if has[k] && !took[k] {
209+
ans += candies[k]
210+
took[k] = true
211+
q = append(q, k)
212+
}
213+
}
214+
for _, j := range containedBoxes[i] {
215+
has[j] = true
216+
if status[j] == 1 && !took[j] {
217+
ans += candies[j]
218+
took[j] = true
219+
q = append(q, j)
220+
}
221+
}
222+
}
223+
return ans
224+
}
74225
```
75226

76227
### **...**
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
public:
3+
int maxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes) {
4+
int ans = 0;
5+
int n = status.size();
6+
vector<bool> has(n);
7+
vector<bool> took(n);
8+
queue<int> q;
9+
for (int& i : initialBoxes)
10+
{
11+
has[i] = true;
12+
if (status[i])
13+
{
14+
ans += candies[i];
15+
took[i] = true;
16+
q.push(i);
17+
}
18+
}
19+
while (!q.empty())
20+
{
21+
int i = q.front();
22+
q.pop();
23+
for (int k : keys[i])
24+
{
25+
status[k] = 1;
26+
if (has[k] && !took[k])
27+
{
28+
ans += candies[k];
29+
took[k] = true;
30+
q.push(k);
31+
}
32+
}
33+
for (int j : containedBoxes[i])
34+
{
35+
has[j] = true;
36+
if (status[j] && !took[j])
37+
{
38+
ans += candies[j];
39+
took[j] = true;
40+
q.push(j);
41+
}
42+
}
43+
}
44+
return ans;
45+
}
46+
};

0 commit comments

Comments
 (0)