返回介绍

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

发布于 2024-06-17 01:03:09 字数 7398 浏览 0 评论 0 收藏 0

2115. Find All Possible Recipes from Given Supplies

中文文档

Description

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.

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.

Return _a list of all the recipes that you can create. _You may return the answer in any order.

Note that two recipes may contain each other in their ingredients.

 

Example 1:

Input: recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"]
Output: ["bread"]
Explanation:
We can create "bread" since we have the ingredients "yeast" and "flour".

Example 2:

Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
Output: ["bread","sandwich"]
Explanation:
We can create "bread" since we have the ingredients "yeast" and "flour".
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".

Example 3:

Input: recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
Output: ["bread","sandwich","burger"]
Explanation:
We can create "bread" since we have the ingredients "yeast" and "flour".
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
We can create "burger" since we have the ingredient "meat" and can create the ingredients "bread" and "sandwich".

 

Constraints:

  • 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], and supplies[k] consist only of lowercase English letters.
  • All the values of recipes and supplies combined are unique.
  • Each ingredients[i] does not contain any duplicate values.

Solutions

Solution 1

class Solution:
  def findAllRecipes(
    self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]
  ) -> List[str]:
    g = defaultdict(list)
    indeg = defaultdict(int)
    for a, b in zip(recipes, ingredients):
      for v in b:
        g[v].append(a)
      indeg[a] += len(b)
    q = deque(supplies)
    ans = []
    while q:
      for _ in range(len(q)):
        i = q.popleft()
        for j in g[i]:
          indeg[j] -= 1
          if indeg[j] == 0:
            ans.append(j)
            q.append(j)
    return ans
class Solution {
  public List<String> findAllRecipes(
    String[] recipes, List<List<String>> ingredients, String[] supplies) {
    Map<String, List<String>> g = new HashMap<>();
    Map<String, Integer> indeg = new HashMap<>();
    for (int i = 0; i < recipes.length; ++i) {
      for (String v : ingredients.get(i)) {
        g.computeIfAbsent(v, k -> new ArrayList<>()).add(recipes[i]);
      }
      indeg.put(recipes[i], ingredients.get(i).size());
    }
    Deque<String> q = new ArrayDeque<>();
    for (String s : supplies) {
      q.offer(s);
    }
    List<String> ans = new ArrayList<>();
    while (!q.isEmpty()) {
      for (int n = q.size(); n > 0; --n) {
        String i = q.pollFirst();
        for (String j : g.getOrDefault(i, Collections.emptyList())) {
          indeg.put(j, indeg.get(j) - 1);
          if (indeg.get(j) == 0) {
            ans.add(j);
            q.offer(j);
          }
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
    unordered_map<string, vector<string>> g;
    unordered_map<string, int> indeg;
    for (int i = 0; i < recipes.size(); ++i) {
      for (auto& v : ingredients[i]) {
        g[v].push_back(recipes[i]);
      }
      indeg[recipes[i]] = ingredients[i].size();
    }
    queue<string> q;
    for (auto& s : supplies) {
      q.push(s);
    }
    vector<string> ans;
    while (!q.empty()) {
      for (int n = q.size(); n; --n) {
        auto i = q.front();
        q.pop();
        for (auto j : g[i]) {
          if (--indeg[j] == 0) {
            ans.push_back(j);
            q.push(j);
          }
        }
      }
    }
    return ans;
  }
};
func findAllRecipes(recipes []string, ingredients [][]string, supplies []string) []string {
  g := map[string][]string{}
  indeg := map[string]int{}
  for i, a := range recipes {
    for _, b := range ingredients[i] {
      g[b] = append(g[b], a)
    }
    indeg[a] = len(ingredients[i])
  }
  q := []string{}
  for _, s := range supplies {
    q = append(q, s)
  }
  ans := []string{}
  for len(q) > 0 {
    for n := len(q); n > 0; n-- {
      i := q[0]
      q = q[1:]
      for _, j := range g[i] {
        indeg[j]--
        if indeg[j] == 0 {
          ans = append(ans, j)
          q = append(q, j)
        }
      }
    }
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文