Skip to content

Commit 41d4dc1

Browse files
committed
feat: add solutions to lc problem: No.2115
No.2115.Find All Possible Recipes from Given Supplies
1 parent 88c93c7 commit 41d4dc1

File tree

6 files changed

+349
-2
lines changed

6 files changed

+349
-2
lines changed

solution/2100-2199/2115.Find All Possible Recipes from Given Supplies/README.md

+122-1
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,143 @@
6969

7070
<!-- 这里可写通用的实现逻辑 -->
7171

72+
**方法一:拓扑排序**
73+
74+
首先,我们可以将每道菜看成一个节点,每个节点的入度表示其所需的原材料数量。我们可以通过拓扑排序的方式,找到所有可以做出的菜。
75+
7276
<!-- tabs:start -->
7377

7478
### **Python3**
7579

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

7882
```python
79-
83+
class Solution:
84+
def findAllRecipes(self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]) -> List[str]:
85+
g = defaultdict(list)
86+
indeg = defaultdict(int)
87+
for a, b in zip(recipes, ingredients):
88+
for v in b:
89+
g[v].append(a)
90+
indeg[a] += len(b)
91+
q = deque(supplies)
92+
ans = []
93+
while q:
94+
for _ in range(len(q)):
95+
i = q.popleft()
96+
for j in g[i]:
97+
indeg[j] -= 1
98+
if indeg[j] == 0:
99+
ans.append(j)
100+
q.append(j)
101+
return ans
80102
```
81103

82104
### **Java**
83105

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

86108
```java
109+
class Solution {
110+
public List<String> findAllRecipes(String[] recipes, List<List<String>> ingredients, String[] supplies) {
111+
Map<String, List<String>> g = new HashMap<>();
112+
Map<String, Integer> indeg = new HashMap<>();
113+
for (int i = 0; i < recipes.length; ++i) {
114+
for (String v : ingredients.get(i)) {
115+
g.computeIfAbsent(v, k -> new ArrayList<>()).add(recipes[i]);
116+
}
117+
indeg.put(recipes[i], ingredients.get(i).size());
118+
}
119+
Deque<String> q = new ArrayDeque<>();
120+
for (String s : supplies) {
121+
q.offer(s);
122+
}
123+
List<String> ans = new ArrayList<>();
124+
while (!q.isEmpty()) {
125+
for (int n = q.size(); n > 0; --n) {
126+
String i = q.pollFirst();
127+
for (String j : g.getOrDefault(i, Collections.emptyList())) {
128+
indeg.put(j, indeg.get(j) - 1);
129+
if (indeg.get(j) == 0) {
130+
ans.add(j);
131+
q.offer(j);
132+
}
133+
}
134+
}
135+
}
136+
return ans;
137+
}
138+
}
139+
```
140+
141+
### **C++**
142+
143+
```cpp
144+
class Solution {
145+
public:
146+
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
147+
unordered_map<string, vector<string>> g;
148+
unordered_map<string, int> indeg;
149+
for (int i = 0; i < recipes.size(); ++i) {
150+
for (auto& v : ingredients[i]) {
151+
g[v].push_back(recipes[i]);
152+
}
153+
indeg[recipes[i]] = ingredients[i].size();
154+
}
155+
queue<string> q;
156+
for (auto& s : supplies) {
157+
q.push(s);
158+
}
159+
vector<string> ans;
160+
while (!q.empty()) {
161+
for (int n = q.size(); n; --n) {
162+
auto i = q.front();
163+
q.pop();
164+
for (auto j : g[i]) {
165+
if (--indeg[j] == 0) {
166+
ans.push_back(j);
167+
q.push(j);
168+
}
169+
}
170+
}
171+
}
172+
return ans;
173+
}
174+
};
175+
```
87176
177+
### **Go**
178+
179+
```go
180+
func findAllRecipes(recipes []string, ingredients [][]string, supplies []string) []string {
181+
g := map[string][]string{}
182+
indeg := map[string]int{}
183+
for i, a := range recipes {
184+
for _, b := range ingredients[i] {
185+
g[b] = append(g[b], a)
186+
}
187+
indeg[a] = len(ingredients[i])
188+
}
189+
q := []string{}
190+
for _, s := range supplies {
191+
q = append(q, s)
192+
}
193+
ans := []string{}
194+
for len(q) > 0 {
195+
for n := len(q); n > 0; n-- {
196+
i := q[0]
197+
q = q[1:]
198+
for _, j := range g[i] {
199+
indeg[j]--
200+
if indeg[j] == 0 {
201+
ans = append(ans, j)
202+
q = append(q, j)
203+
}
204+
}
205+
}
206+
}
207+
return ans
208+
}
88209
```
89210

90211
### **TypeScript**

solution/2100-2199/2115.Find All Possible Recipes from Given Supplies/README_EN.md

+118-1
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,130 @@ We can create &quot;burger&quot; since we have the ingredient &quot;meat&quot; a
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def findAllRecipes(self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]) -> List[str]:
68+
g = defaultdict(list)
69+
indeg = defaultdict(int)
70+
for a, b in zip(recipes, ingredients):
71+
for v in b:
72+
g[v].append(a)
73+
indeg[a] += len(b)
74+
q = deque(supplies)
75+
ans = []
76+
while q:
77+
for _ in range(len(q)):
78+
i = q.popleft()
79+
for j in g[i]:
80+
indeg[j] -= 1
81+
if indeg[j] == 0:
82+
ans.append(j)
83+
q.append(j)
84+
return ans
6785
```
6886

6987
### **Java**
7088

7189
```java
90+
class Solution {
91+
public List<String> findAllRecipes(String[] recipes, List<List<String>> ingredients, String[] supplies) {
92+
Map<String, List<String>> g = new HashMap<>();
93+
Map<String, Integer> indeg = new HashMap<>();
94+
for (int i = 0; i < recipes.length; ++i) {
95+
for (String v : ingredients.get(i)) {
96+
g.computeIfAbsent(v, k -> new ArrayList<>()).add(recipes[i]);
97+
}
98+
indeg.put(recipes[i], ingredients.get(i).size());
99+
}
100+
Deque<String> q = new ArrayDeque<>();
101+
for (String s : supplies) {
102+
q.offer(s);
103+
}
104+
List<String> ans = new ArrayList<>();
105+
while (!q.isEmpty()) {
106+
for (int n = q.size(); n > 0; --n) {
107+
String i = q.pollFirst();
108+
for (String j : g.getOrDefault(i, Collections.emptyList())) {
109+
indeg.put(j, indeg.get(j) - 1);
110+
if (indeg.get(j) == 0) {
111+
ans.add(j);
112+
q.offer(j);
113+
}
114+
}
115+
}
116+
}
117+
return ans;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
128+
unordered_map<string, vector<string>> g;
129+
unordered_map<string, int> indeg;
130+
for (int i = 0; i < recipes.size(); ++i) {
131+
for (auto& v : ingredients[i]) {
132+
g[v].push_back(recipes[i]);
133+
}
134+
indeg[recipes[i]] = ingredients[i].size();
135+
}
136+
queue<string> q;
137+
for (auto& s : supplies) {
138+
q.push(s);
139+
}
140+
vector<string> ans;
141+
while (!q.empty()) {
142+
for (int n = q.size(); n; --n) {
143+
auto i = q.front();
144+
q.pop();
145+
for (auto j : g[i]) {
146+
if (--indeg[j] == 0) {
147+
ans.push_back(j);
148+
q.push(j);
149+
}
150+
}
151+
}
152+
}
153+
return ans;
154+
}
155+
};
156+
```
72157
158+
### **Go**
159+
160+
```go
161+
func findAllRecipes(recipes []string, ingredients [][]string, supplies []string) []string {
162+
g := map[string][]string{}
163+
indeg := map[string]int{}
164+
for i, a := range recipes {
165+
for _, b := range ingredients[i] {
166+
g[b] = append(g[b], a)
167+
}
168+
indeg[a] = len(ingredients[i])
169+
}
170+
q := []string{}
171+
for _, s := range supplies {
172+
q = append(q, s)
173+
}
174+
ans := []string{}
175+
for len(q) > 0 {
176+
for n := len(q); n > 0; n-- {
177+
i := q[0]
178+
q = q[1:]
179+
for _, j := range g[i] {
180+
indeg[j]--
181+
if indeg[j] == 0 {
182+
ans = append(ans, j)
183+
q = append(q, j)
184+
}
185+
}
186+
}
187+
}
188+
return ans
189+
}
73190
```
74191

75192
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
4+
unordered_map<string, vector<string>> g;
5+
unordered_map<string, int> indeg;
6+
for (int i = 0; i < recipes.size(); ++i) {
7+
for (auto& v : ingredients[i]) {
8+
g[v].push_back(recipes[i]);
9+
}
10+
indeg[recipes[i]] = ingredients[i].size();
11+
}
12+
queue<string> q;
13+
for (auto& s : supplies) {
14+
q.push(s);
15+
}
16+
vector<string> ans;
17+
while (!q.empty()) {
18+
for (int n = q.size(); n; --n) {
19+
auto i = q.front();
20+
q.pop();
21+
for (auto j : g[i]) {
22+
if (--indeg[j] == 0) {
23+
ans.push_back(j);
24+
q.push(j);
25+
}
26+
}
27+
}
28+
}
29+
return ans;
30+
}
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func findAllRecipes(recipes []string, ingredients [][]string, supplies []string) []string {
2+
g := map[string][]string{}
3+
indeg := map[string]int{}
4+
for i, a := range recipes {
5+
for _, b := range ingredients[i] {
6+
g[b] = append(g[b], a)
7+
}
8+
indeg[a] = len(ingredients[i])
9+
}
10+
q := []string{}
11+
for _, s := range supplies {
12+
q = append(q, s)
13+
}
14+
ans := []string{}
15+
for len(q) > 0 {
16+
for n := len(q); n > 0; n-- {
17+
i := q[0]
18+
q = q[1:]
19+
for _, j := range g[i] {
20+
indeg[j]--
21+
if indeg[j] == 0 {
22+
ans = append(ans, j)
23+
q = append(q, j)
24+
}
25+
}
26+
}
27+
}
28+
return ans
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public List<String> findAllRecipes(String[] recipes, List<List<String>> ingredients, String[] supplies) {
3+
Map<String, List<String>> g = new HashMap<>();
4+
Map<String, Integer> indeg = new HashMap<>();
5+
for (int i = 0; i < recipes.length; ++i) {
6+
for (String v : ingredients.get(i)) {
7+
g.computeIfAbsent(v, k -> new ArrayList<>()).add(recipes[i]);
8+
}
9+
indeg.put(recipes[i], ingredients.get(i).size());
10+
}
11+
Deque<String> q = new ArrayDeque<>();
12+
for (String s : supplies) {
13+
q.offer(s);
14+
}
15+
List<String> ans = new ArrayList<>();
16+
while (!q.isEmpty()) {
17+
for (int n = q.size(); n > 0; --n) {
18+
String i = q.pollFirst();
19+
for (String j : g.getOrDefault(i, Collections.emptyList())) {
20+
indeg.put(j, indeg.get(j) - 1);
21+
if (indeg.get(j) == 0) {
22+
ans.add(j);
23+
q.offer(j);
24+
}
25+
}
26+
}
27+
}
28+
return ans;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def findAllRecipes(self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]) -> List[str]:
3+
g = defaultdict(list)
4+
indeg = defaultdict(int)
5+
for a, b in zip(recipes, ingredients):
6+
for v in b:
7+
g[v].append(a)
8+
indeg[a] += len(b)
9+
q = deque(supplies)
10+
ans = []
11+
while q:
12+
for _ in range(len(q)):
13+
i = q.popleft()
14+
for j in g[i]:
15+
indeg[j] -= 1
16+
if indeg[j] == 0:
17+
ans.append(j)
18+
q.append(j)
19+
return ans

0 commit comments

Comments
 (0)