Skip to content

Commit 67f30f8

Browse files
committed
Mar-21
1 parent 75ed03d commit 67f30f8

30 files changed

+549
-268
lines changed

README.md

Lines changed: 32 additions & 23 deletions
Large diffs are not rendered by default.

leetcode/1094.car-pooling.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Tag: Array, Sorting, Heap (Priority Queue), Simulation, Prefix Sum
2+
// Time: O(NlogN)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// There is a car with capacity empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).
8+
// You are given the integer capacity and an array trips where trips[i] = [numPassengersi, fromi, toi] indicates that the ith trip has numPassengersi passengers and the locations to pick them up and drop them off are fromi and toi respectively. The locations are given as the number of kilometers due east from the car's initial location.
9+
// Return true if it is possible to pick up and drop off all passengers for all the given trips, or false otherwise.
10+
//  
11+
// Example 1:
12+
//
13+
// Input: trips = [[2,1,5],[3,3,7]], capacity = 4
14+
// Output: false
15+
//
16+
// Example 2:
17+
//
18+
// Input: trips = [[2,1,5],[3,3,7]], capacity = 5
19+
// Output: true
20+
//
21+
//  
22+
// Constraints:
23+
//
24+
// 1 <= trips.length <= 1000
25+
// trips[i].length == 3
26+
// 1 <= numPassengersi <= 100
27+
// 0 <= fromi < toi <= 1000
28+
// 1 <= capacity <= 105
29+
//
30+
//
31+
32+
class Solution {
33+
public:
34+
bool carPooling(vector<vector<int>>& trips, int capacity) {
35+
map<int, int> line;
36+
for (auto &t: trips) {
37+
line[t[1]] += t[0];
38+
line[t[2]] -= t[0];
39+
}
40+
41+
int prefix = 0;
42+
for (auto &[loc, num]: line) {
43+
prefix += num;
44+
if (prefix > capacity) {
45+
return false;
46+
}
47+
}
48+
return true;
49+
}
50+
};

leetcode/1094.car-pooling.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Tag: Array, Sorting, Heap (Priority Queue), Simulation, Prefix Sum
2+
# Time: O(NlogN)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# There is a car with capacity empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).
8+
# You are given the integer capacity and an array trips where trips[i] = [numPassengersi, fromi, toi] indicates that the ith trip has numPassengersi passengers and the locations to pick them up and drop them off are fromi and toi respectively. The locations are given as the number of kilometers due east from the car's initial location.
9+
# Return true if it is possible to pick up and drop off all passengers for all the given trips, or false otherwise.
10+
#  
11+
# Example 1:
12+
#
13+
# Input: trips = [[2,1,5],[3,3,7]], capacity = 4
14+
# Output: false
15+
#
16+
# Example 2:
17+
#
18+
# Input: trips = [[2,1,5],[3,3,7]], capacity = 5
19+
# Output: true
20+
#
21+
#  
22+
# Constraints:
23+
#
24+
# 1 <= trips.length <= 1000
25+
# trips[i].length == 3
26+
# 1 <= numPassengersi <= 100
27+
# 0 <= fromi < toi <= 1000
28+
# 1 <= capacity <= 105
29+
#
30+
#
31+
32+
from collections import defaultdict
33+
class Solution:
34+
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
35+
line = defaultdict(int)
36+
for p, f, t in trips:
37+
line[f] += p
38+
line[t] -= p
39+
40+
prefix = 0
41+
for loc in sorted(line.keys()):
42+
prefix += line[loc]
43+
if prefix > capacity:
44+
return False
45+
46+
return True
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Tag: Array, Hash Table, String, Graph, Topological Sort
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/TWu8pnf7LWo
7+
8+
// 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]. A recipe can also be an ingredient for other recipes, i.e., ingredients[i] may contain a string that is in recipes.
9+
// 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.
10+
// Return a list of all the recipes that you can create. You may return the answer in any order.
11+
// Note that two recipes may contain each other in their ingredients.
12+
//  
13+
// Example 1:
14+
//
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".
19+
//
20+
// Example 2:
21+
//
22+
// Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
23+
// Output: ["bread","sandwich"]
24+
// Explanation:
25+
// We can create "bread" since we have the ingredients "yeast" and "flour".
26+
// We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
27+
//
28+
// Example 3:
29+
//
30+
// Input: recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
31+
// Output: ["bread","sandwich","burger"]
32+
// Explanation:
33+
// We can create "bread" since we have the ingredients "yeast" and "flour".
34+
// We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
35+
// We can create "burger" since we have the ingredient "meat" and can create the ingredients "bread" and "sandwich".
36+
//
37+
//  
38+
// Constraints:
39+
//
40+
// n == recipes.length == ingredients.length
41+
// 1 <= n <= 100
42+
// 1 <= ingredients[i].length, supplies.length <= 100
43+
// 1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10
44+
// recipes[i], ingredients[i][j], and supplies[k] consist only of lowercase English letters.
45+
// All the values of recipes and supplies combined are unique.
46+
// Each ingredients[i] does not contain any duplicate values.
47+
//
48+
//
49+
50+
class Solution {
51+
public:
52+
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
53+
unordered_map<string, vector<string>> graph;
54+
unordered_map<string, int> indegree;
55+
int n = recipes.size();
56+
for (int i = 0; i < n; i++) {
57+
for (auto &ingredient: ingredients[i]) {
58+
graph[ingredient].push_back(recipes[i]);
59+
indegree[recipes[i]] += 1;
60+
}
61+
}
62+
vector<string> res;
63+
queue<string> q;
64+
for (auto &supply: supplies) {
65+
q.push(supply);
66+
}
67+
68+
while (!q.empty()) {
69+
string cur = q.front();
70+
q.pop();
71+
for (auto &neighbor: graph[cur]) {
72+
indegree[neighbor] -= 1;
73+
if (indegree[neighbor] == 0) {
74+
res.push_back(neighbor);
75+
q.push(neighbor);
76+
}
77+
}
78+
79+
}
80+
81+
return res;
82+
}
83+
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Tag: Array, Hash Table, String, Graph, Topological Sort
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/TWu8pnf7LWo
7+
8+
# 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]. A recipe can also be an ingredient for other recipes, i.e., ingredients[i] may contain a string that is in recipes.
9+
# 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.
10+
# Return a list of all the recipes that you can create. You may return the answer in any order.
11+
# Note that two recipes may contain each other in their ingredients.
12+
#  
13+
# Example 1:
14+
#
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".
19+
#
20+
# Example 2:
21+
#
22+
# Input: recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
23+
# Output: ["bread","sandwich"]
24+
# Explanation:
25+
# We can create "bread" since we have the ingredients "yeast" and "flour".
26+
# We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
27+
#
28+
# Example 3:
29+
#
30+
# Input: recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
31+
# Output: ["bread","sandwich","burger"]
32+
# Explanation:
33+
# We can create "bread" since we have the ingredients "yeast" and "flour".
34+
# We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
35+
# We can create "burger" since we have the ingredient "meat" and can create the ingredients "bread" and "sandwich".
36+
#
37+
#  
38+
# Constraints:
39+
#
40+
# n == recipes.length == ingredients.length
41+
# 1 <= n <= 100
42+
# 1 <= ingredients[i].length, supplies.length <= 100
43+
# 1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10
44+
# recipes[i], ingredients[i][j], and supplies[k] consist only of lowercase English letters.
45+
# All the values of recipes and supplies combined are unique.
46+
# Each ingredients[i] does not contain any duplicate values.
47+
#
48+
#
49+
50+
from collections import defaultdict, deque
51+
class Solution:
52+
def findAllRecipes(self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]) -> List[str]:
53+
n = len(recipes)
54+
graph = defaultdict(list)
55+
indegree = defaultdict(int)
56+
for i in range(n):
57+
for ingredient in ingredients[i]:
58+
graph[ingredient].append(recipes[i])
59+
indegree[recipes[i]] += 1
60+
61+
res = []
62+
q = deque(supplies)
63+
while len(q) > 0:
64+
cur = q.popleft()
65+
for neighbor in graph[cur]:
66+
indegree[neighbor] -= 1
67+
if indegree[neighbor] == 0:
68+
res.append(neighbor)
69+
q.append(neighbor)
70+
71+
return res

leetcode/3108.minimum-cost-walk-in-weighted-graph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Tag: Array, Bit Manipulation, Union Find, Graph
2-
// Time: O(N+M)
2+
// Time: O(N + M)
33
// Space: O(N)
44
// Ref: -
55
// Note: -

leetcode/3108.minimum-cost-walk-in-weighted-graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Tag: Array, Bit Manipulation, Union Find, Graph
22
# Time: O(N + M)
3-
# Space: O(1)
3+
# Space: O(N)
44
# Ref: -
55
# Note: -
66
# Video: https://youtu.be/8EobS6zkaZw

leetcode/370.range-addition.cpp

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)