Skip to content

Commit 06b99ba

Browse files
committed
Add solution and test-cases for problem 2115
1 parent d569e0c commit 06b99ba

File tree

3 files changed

+103
-26
lines changed

3 files changed

+103
-26
lines changed

leetcode/2101-2200/2115.Find-All-Possible-Recipes-from-Given-Supplies/README.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [2115.Find All Possible Recipes from Given Supplies][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You have information about `n` different recipes. You are given a string array `recipes` and a 2D string array `ingredients`. The `ith` recipe has the name `recipes[i]`, and you can **create** it if you have **all** the needed ingredients from `ingredients[i]`. Ingredients to a recipe may need to be created from other recipes, i.e., `ingredients[i]` may contain a string that is in `recipes`.
5+
6+
You are also given a string array `supplies` containing all the ingredients that you initially have, and you have an infinite supply of all of them.
7+
8+
Return a list of all the recipes that you can create. You may return the answer in **any order**.
9+
10+
Note that two recipes may contain each other in their ingredients.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"]
16+
Output: ["bread"]
17+
Explanation:
18+
We can create "bread" since we have the ingredients "yeast" and "flour".
1319
```
1420

15-
## 题意
16-
> ...
17-
18-
## 题解
21+
**Example 2:**
1922

20-
### 思路1
21-
> ...
22-
Find All Possible Recipes from Given Supplies
23-
```go
2423
```
24+
Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
25+
Output: ["bread","sandwich"]
26+
Explanation:
27+
We can create "bread" since we have the ingredients "yeast" and "flour".
28+
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
29+
```
30+
31+
**Example 3:**
2532

33+
```
34+
Input: recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
35+
Output: ["bread","sandwich","burger"]
36+
Explanation:
37+
We can create "bread" since we have the ingredients "yeast" and "flour".
38+
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
39+
We can create "burger" since we have the ingredient "meat" and can create the ingredients "bread" and "sandwich".
40+
```
2641

2742
## 结语
2843

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(recipes []string, ingredients [][]string, supplies []string) []string {
4+
sm := make(map[string]struct{})
5+
rm := make(map[string]struct{})
6+
for _, s := range supplies {
7+
sm[s] = struct{}{}
8+
}
9+
for _, r := range recipes {
10+
rm[r] = struct{}{}
11+
}
12+
13+
in := make(map[string]int)
14+
unlock := make(map[string][]string)
15+
for idx, r := range recipes {
16+
in[r] = 0
17+
for _, i := range ingredients[idx] {
18+
if _, ok := sm[i]; ok {
19+
continue
20+
}
21+
if i == r {
22+
in[r] = -1
23+
break
24+
}
25+
if _, ok := rm[i]; ok {
26+
in[r]++
27+
if _, ok := unlock[i]; !ok {
28+
unlock[i] = []string{}
29+
}
30+
unlock[i] = append(unlock[i], r)
31+
continue
32+
}
33+
in[r] = -1 // 无法组成,用了一些不存在的东西
34+
break
35+
}
36+
}
37+
res := []string{}
38+
queue := []string{}
39+
for r, c := range in {
40+
if c == 0 {
41+
queue = append(queue, r)
42+
res = append(res, r)
43+
}
44+
}
45+
for len(queue) > 0 {
46+
nq := make([]string, 0)
47+
for _, cur := range queue {
48+
for _, r := range unlock[cur] {
49+
in[r]--
50+
if in[r] == 0 {
51+
nq = append(nq, r)
52+
res = append(res, r)
53+
}
54+
}
55+
}
56+
if len(nq) == 0 {
57+
break
58+
}
59+
queue = nq
60+
}
61+
62+
return res
563
}

leetcode/2101-2200/2115.Find-All-Possible-Recipes-from-Given-Supplies/Solution_test.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,42 @@ package Solution
22

33
import (
44
"reflect"
5+
"sort"
56
"strconv"
67
"testing"
78
)
89

910
func TestSolution(t *testing.T) {
1011
// 测试用例
1112
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
13+
name string
14+
recipes []string
15+
ingredients [][]string
16+
supplies []string
17+
expect []string
1518
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
19+
{"TestCase1", []string{"bread"}, [][]string{{"yeast", "flour"}}, []string{"yeast", "flour", "conr"}, []string{"bread"}},
20+
{"TestCase2", []string{"bread", "sandwich"}, [][]string{{"yeast", "flour"}, {"bread", "meat"}}, []string{"yeast", "flour", "meat"}, []string{"bread", "sandwich"}},
21+
{"TestCase3", []string{"bread", "sandwich", "burger"}, [][]string{{"yeast", "flour"}, {"bread", "meat"}, {"sandwich", "meat", "bread"}}, []string{"yeast", "flour", "meat"}, []string{"bread", "burger", "sandwich"}},
1922
}
2023

2124
// 开始测试
2225
for i, c := range cases {
2326
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
27+
got := Solution(c.recipes, c.ingredients, c.supplies)
28+
sort.Strings(got)
2529
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
30+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
31+
c.expect, got, c.recipes, c.ingredients, c.supplies)
2832
}
2933
})
3034
}
3135
}
3236

33-
// 压力测试
37+
// 压力测试
3438
func BenchmarkSolution(b *testing.B) {
3539
}
3640

37-
// 使用案列
41+
// 使用案列
3842
func ExampleSolution() {
3943
}

0 commit comments

Comments
 (0)