Skip to content

Commit 045b0a1

Browse files
author
Danieldu
committed
update code
1 parent 9026b77 commit 045b0a1

7 files changed

+102
-150
lines changed
File renamed without changes.

StudyPlan/Graph/127.word-ladder.cpp

Lines changed: 0 additions & 150 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* @lc app=leetcode id=2115 lang=cpp
3+
*
4+
* [2115] Find All Possible Recipes from Given Supplies
5+
*/
6+
7+
// @lc code=start
8+
#include "bits/stdc++.h"
9+
using namespace std;
10+
class Solution {
11+
public:
12+
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
13+
14+
unordered_set<string> available(supplies.begin(), supplies.end());
15+
unordered_map<string, vector<int>> dependents;
16+
vector<int> missingCount(recipes.size(), 0);
17+
18+
for (int i = 0; i < recipes.size(); i++) {
19+
for (const auto& ing : ingredients[i]) {
20+
if (available.find(ing) == available.end()) {
21+
missingCount[i]++;
22+
dependents[ing].push_back(i);
23+
}
24+
}
25+
}
26+
27+
queue<int> q;
28+
vector<string> result;
29+
for (int i = 0; i < recipes.size(); i++) {
30+
if (missingCount[i] == 0) {
31+
q.push(i);
32+
result.push_back(recipes[i]);
33+
available.insert(recipes[i]);
34+
}
35+
}
36+
37+
while (!q.empty()) {
38+
int curr = q.front();
39+
q.pop();
40+
string currRecipe = recipes[curr];
41+
42+
if (dependents.count(currRecipe)) {
43+
for (int recId : dependents[currRecipe]) {
44+
missingCount[recId]--;
45+
46+
if (missingCount[recId] == 0) {
47+
q.push(recId);
48+
result.push_back(recipes[recId]);
49+
available.insert(recipes[recId]);
50+
}
51+
}
52+
}
53+
}
54+
return result;
55+
}
56+
};
57+
58+
// @lc code=end
59+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @lc app=leetcode id=889 lang=cpp
3+
*
4+
* [889] Construct Binary Tree from Preorder and Postorder Traversal
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
20+
struct TreeNode {
21+
int val;
22+
TreeNode *left;
23+
TreeNode *right;
24+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
25+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
26+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
27+
};
28+
#include "bits/stdc++.h"
29+
using namespace std;
30+
31+
class Solution {
32+
public:
33+
TreeNode* constructFromPrePost(vector<int>& preorder, vector<int>& postorder) {
34+
TreeNode* root;
35+
stack<int> stk;
36+
for(int i=0;i<preorder.size();i++){
37+
root = new TreeNode(preorder[i]);
38+
}
39+
return root;
40+
}
41+
};
42+
// @lc code=end
43+

0 commit comments

Comments
 (0)