5947:从给定原材料中找到所有可以做出的菜

This commit is contained in:
huangge1199@hotmail.com 2021-12-26 14:41:53 +08:00
parent 1518038a1e
commit 290e6d3737
2 changed files with 194 additions and 0 deletions

View File

@ -0,0 +1,135 @@
//你有 n 道不同菜的信息给你一个字符串数组 recipes 和一个二维字符串数组 ingredients i 道菜的名字为 recipes[i]
//如果你有它 所有 的原材料 ingredients[i] 那么你可以 做出 这道菜一道菜的原材料可能是 另一道 也就是说 ingredients[i]
//可能包含 recipes 中另一个字符串
//
// 同时给你一个字符串数组 supplies 它包含你初始时拥有的所有原材料每一种原材料你都有无限多
//
// 请你返回你可以做出的所有菜你可以以 任意顺序 返回它们
//
// 注意两道菜在它们的原材料中可能互相包含
//
//
//
// 示例 1
//
// 输入recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = [
//"yeast","flour","corn"]
//输出["bread"]
//解释
//我们可以做出 "bread" 因为我们有原材料 "yeast" "flour"
//
//
// 示例 2
//
// 输入recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread",
//"meat"]], supplies = ["yeast","flour","meat"]
//输出["bread","sandwich"]
//解释
//我们可以做出 "bread" 因为我们有原材料 "yeast" "flour"
//我们可以做出 "sandwich" 因为我们有原材料 "meat" 且可以做出原材料 "bread"
//
//
// 示例 3
//
// 输入recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],
//["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour",
//"meat"]
//输出["bread","sandwich","burger"]
//解释
//我们可以做出 "bread" 因为我们有原材料 "yeast" "flour"
//我们可以做出 "sandwich" 因为我们有原材料 "meat" 且可以做出原材料 "bread"
//我们可以做出 "burger" 因为我们有原材料 "meat" 且可以做出原材料 "bread" "sandwich"
//
//
// 示例 4
//
// 输入recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = [
//"yeast"]
//输出[]
//解释
//我们没法做出任何菜因为我们只有原材料 "yeast"
//
//
//
//
// 提示
//
//
// n == recipes.length == ingredients.length
// 1 <= n <= 100
// 1 <= ingredients[i].length, supplies.length <= 100
// 1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10
// recipes[i], ingredients[i][j] supplies[k] 只包含小写英文字母
// 所有 recipes supplies 中的值互不相同
// ingredients[i] 中的字符串互不相同
//
// 👍 3 👎 0
package leetcode.editor.cn;
import java.util.*;
//5947:从给定原材料中找到所有可以做出的菜
class FindAllPossibleRecipesFromGivenSupplies {
public static void main(String[] args) {
//测试代码
Solution solution = new FindAllPossibleRecipesFromGivenSupplies().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<String> findAllRecipes(String[] recipes, List<List<String>> ingredients, String[] supplies) {
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < recipes.length; i++) {
map.put(recipes[i], i);
}
List<String> sup = Arrays.asList(supplies);
use = new int[recipes.length];
list = new ArrayList<>();
for (int i = 0; i < recipes.length; i++) {
use[i] = dfs(recipes[i], ingredients, sup, map);
}
HashSet<String> set = new HashSet<>(list);
list.clear();
list.addAll(set);
return list;
}
int[] use;
List<String> list;
private int dfs(String strs, List<List<String>> ingredients, List<String> sup, Map<String, Integer> map) {
int i = map.get(strs);
if (use[i] > 0) {
return use[i];
}
boolean bl = true;
for (String str : ingredients.get(i)) {
if ((list != null && list.contains(str)) || sup.contains(str)) {
continue;
}
if (map.containsKey(str) && use[map.get(str)] == 0) {
if (use[map.get(str)] == 3) {
use[map.get(str)] = 2;
return 2;
}
use[i] = 3;
if (dfs(str, ingredients, sup, map) == 2) {
bl = false;
break;
}
} else {
bl = false;
break;
}
}
if (bl) {
list.add(strs);
}
return bl ? 1 : 2;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,59 @@
<p>你有 <code>n</code>&nbsp;道不同菜的信息。给你一个字符串数组&nbsp;<code>recipes</code>&nbsp;和一个二维字符串数组&nbsp;<code>ingredients</code>&nbsp;。第&nbsp;<code>i</code>&nbsp;道菜的名字为&nbsp;<code>recipes[i]</code>&nbsp;,如果你有它&nbsp;<strong>所有</strong>&nbsp;的原材料&nbsp;<code>ingredients[i]</code>&nbsp;,那么你可以&nbsp;<strong>做出</strong>&nbsp;这道菜。一道菜的原材料可能是&nbsp;<strong>另一道</strong>&nbsp;菜,也就是说&nbsp;<code>ingredients[i]</code>&nbsp;可能包含&nbsp;<code>recipes</code>&nbsp;中另一个字符串。</p>
<p>同时给你一个字符串数组&nbsp;<code>supplies</code>&nbsp;,它包含你初始时拥有的所有原材料,每一种原材料你都有无限多。</p>
<p>请你返回你可以做出的所有菜。你可以以 <strong>任意顺序</strong>&nbsp;返回它们。</p>
<p>注意两道菜在它们的原材料中可能互相包含。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"]
<b>输出:</b>["bread"]
<strong>解释:</strong>
我们可以做出 "bread" ,因为我们有原材料 "yeast" 和 "flour" 。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
<b>输出:</b>["bread","sandwich"]
<strong>解释:</strong>
我们可以做出 "bread" ,因为我们有原材料 "yeast" 和 "flour" 。
我们可以做出 "sandwich" ,因为我们有原材料 "meat" 且可以做出原材料 "bread" 。
</pre>
<p><strong>示例 3</strong></p>
<pre><b>输入:</b>recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
<b>输出:</b>["bread","sandwich","burger"]
<strong>解释:</strong>
我们可以做出 "bread" ,因为我们有原材料 "yeast" 和 "flour" 。
我们可以做出 "sandwich" ,因为我们有原材料 "meat" 且可以做出原材料 "bread" 。
我们可以做出 "burger" ,因为我们有原材料 "meat" 且可以做出原材料 "bread" 和 "sandwich" 。
</pre>
<p><strong>示例 4</strong></p>
<pre><b>输入:</b>recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast"]
<b>输出:</b>[]
<strong>解释:</strong>
我们没法做出任何菜,因为我们只有原材料 "yeast" 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == recipes.length == ingredients.length</code></li>
<li><code>1 &lt;= n &lt;= 100</code></li>
<li><code>1 &lt;= ingredients[i].length, supplies.length &lt;= 100</code></li>
<li><code>1 &lt;= recipes[i].length, ingredients[i][j].length, supplies[k].length &lt;= 10</code></li>
<li><code>recipes[i], ingredients[i][j]</code>&nbsp;&nbsp;<code>supplies[k]</code>&nbsp;只包含小写英文字母。</li>
<li>所有&nbsp;<code>recipes</code>&nbsp;<code>supplies</code>&nbsp;中的值互不相同。</li>
<li><code>ingredients[i]</code>&nbsp;中的字符串互不相同。</li>
</ul>
<div><li>👍 3</li><li>👎 0</li></div>