当前位置:网站首页>Leetcode-2115: find all the dishes that can be made from the given raw materials
Leetcode-2115: find all the dishes that can be made from the given raw materials
2022-07-03 00:47:00 【Chrysanthemum headed bat】
leetcode-2115: Find all the dishes you can make from a given raw material
subject
Do you have n
Information about different dishes . Here's an array of strings recipes
And a two-dimensional string array ingredients
. The first i
The name of this dish is recipes[i]
, If you have it all Raw materials ingredients[i]
, Then you can To make a This dish . The raw material of a dish may be Another way food , in other words ingredients[i]
May contain recipes
Another string in .
And give you an array of strings supplies
, It contains all the raw materials you had at the beginning , You have an unlimited number of raw materials .
Please return to all the dishes you can make . You can In any order Return to them .
Note that the two dishes may contain each other in their raw materials .
Example 1:
Input :recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"]
Output :["bread"]
explain :
We can make "bread" , Because we have raw materials "yeast" and "flour" .
Example 2:
Input :recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
Output :["bread","sandwich"]
explain :
We can make "bread" , Because we have raw materials "yeast" and "flour" .
We can make "sandwich" , Because we have raw materials "meat" And can make raw materials "bread" .
Example 3:
Input :recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
Output :["bread","sandwich","burger"]
explain :
We can make "bread" , Because we have raw materials "yeast" and "flour" .
We can make "sandwich" , Because we have raw materials "meat" And can make raw materials "bread" .
We can make "burger" , Because we have raw materials "meat" And can make raw materials "bread" and "sandwich" .
Example 4:
Input :recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast"]
Output :[]
explain :
We can't make any dishes , Because we only have raw materials "yeast" .
Problem solving
Method 1 : Violent simulation
Made from existing raw materials , Put the newly cooked dishes , Also added to raw materials , Repeat until no new dishes are added , Just return the result
class Solution {
public:
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
vector<string> res;
unordered_set<string> set;
for(string& s:supplies){
// Record existing raw materials
set.insert(s);
}
int preLen;// The current length of all raw materials
while(true){
int preLen=set.size();
for(int i=0;i<recipes.size();i++){
if(set.count(recipes[i])) continue;
vector<string> tmp=ingredients[i];
bool ok=true;// If true It means you can cook this dish
for(int j=0;j<tmp.size();j++){
if(!set.count(tmp[j])){
ok=false;
break;
}
}
if(ok){
// If this dish can be made
set.insert(recipes[i]);// This dish is added as raw material
res.push_back(recipes[i]);// Save results
}
}
if(set.size()<=preLen) break;// If the raw materials are no longer increased , It means that the rest of the dishes can't be made
}
return res;
}
};
Method 2 : A topological sort
Starting from raw materials , When the corresponding degree of the dish is 0 When , It indicates that the raw materials are complete , Add the dishes to the raw materials .
Get a penetration of 0 The point of , It's a dish that can be made .
class Solution {
public:
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
unordered_map<string,vector<string>> graph;
unordered_map<string,int> indeg;
int n=recipes.size();
for(int i=0;i<n;i++){
for(int j=0;j<ingredients[i].size();j++){
graph[ingredients[i][j]].push_back(recipes[i]);
indeg[recipes[i]]++;
}
}
queue<string> q;
for(string& s:supplies){
q.push(s);
}
vector<string> res;
while(!q.empty()){
string x=q.front();
q.pop();
for(string& y:graph[x]){
if(--indeg[y]==0){
q.push(y);
res.push_back(y);
}
}
}
return res;
}
};
边栏推荐
- Two common methods and steps of character device registration
- Why is the website slow to open?
- 2022中国3D视觉企业(引导定位、分拣场景)厂商名单
- Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
- leetcode-2115:从给定原材料中找到所有可以做出的菜
- 为什么网站打开速度慢?
- Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
- 利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
- 字符设备注册常用的两种方法和步骤
- Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
猜你喜欢
【AutoSAR 十二 模式管理】
【雅思阅读】王希伟阅读P1(阅读判断题)
How SQLSEVER removes data with duplicate IDS
指针初阶(基础)
【AutoSAR 一 概述】
Pageoffice - bug modification journey
文件操作IO-Part2
Vulkan performance and refinement
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
随机推荐
[pulsar document] concepts and architecture
Vulkan practice first bullet
Kubernetes simple introduction to writing YML
指针初阶(基础)
leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
The most painful programming problem in 2021, adventure of code 2021 Day24
简单聊聊运维监控的其他用途
【雅思阅读】王希伟阅读P1(阅读判断题)
Illustrated network: what is virtual router redundancy protocol VRRP?
leetcode-849:到最近的人的最大距离
Vulkan performance and refinement
Use Jenkins II job
Shell 实现文件基本操作(sed-编辑、awk-匹配)
测试右移:线上质量监控 ELK 实战
How to systematically learn machine learning
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
图解网络:什么是虚拟路由器冗余协议 VRRP?
线程的启动与优先级
Markdown tutorial
leetcode-224:基本计算器