Skip to content

Commit 080a37e

Browse files
add 2115
1 parent 3621a47 commit 080a37e

File tree

3 files changed

+99
-1
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+99
-1
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
| 2467 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2469.java) || Easy ||
5151
| 2455 | [Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2455.java) || Easy ||
5252
| 2441 | [Largest Positive Integer That Exists With Its Negative](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2441.java) || Easy ||
53-
| 2437 | [Number of Valid Clock Times](https://leetcode.com/problems/number-of-valid-clock-times/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2437.java) | | Easy ||
53+
| 2437 | [Number of Valid Clock Times](https://leetcode.com/problems/number-of-valid-clock-times/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2437.java) | | Easy ||
5454
| 2433 | [Find The Original Array of Prefix Xor](https://leetcode.com/problems/find-the-original-array-of-prefix-xor/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2433.java) | [:tv:](https://youtu.be/idcT-p_DDrI) | Medium ||
5555
| 2432 | [The Employee That Worked on the Longest Task](https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2432.java) || Easy ||
5656
| 2427 | [Number of Common Factors](https://leetcode.com/problems/number-of-common-factors/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2427.java) || Easy ||
@@ -153,6 +153,7 @@
153153
| 2120 | [Execution of All Suffix Instructions Staying in a Grid](https://leetcode.com/problems/execution-of-all-suffix-instructions-staying-in-a-grid/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2120.java) || Medium ||
154154
| 2119 | [A Number After a Double Reversal](https://leetcode.com/problems/a-number-after-a-double-reversal/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2119.java) || Easy ||
155155
| 2116 | [Check if a Parentheses String Can Be Valid](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2116.java) || Medium ||
156+
| 2115 | [Find All Possible Recipes from Given Supplies](https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2115.java) || Medium |Topological Sort, HashTable
156157
| 2114 | [Maximum Number of Words Found in Sentences](https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2114.java) || Easy ||
157158
| 2110 | [Number of Smooth Descent Periods of a Stock](https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2110.java) || Medium ||
158159
| 2109 | [Adding Spaces to a String](https://leetcode.com/problems/adding-spaces-to-a-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2109.java) || Medium ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Queue;
11+
import java.util.Set;
12+
13+
public class _2115 {
14+
public static class Solution1 {
15+
/**
16+
* My completely original solution, topological sort template comes in pretty handy.
17+
*/
18+
public List<String> findAllRecipes(String[] recipes, List<List<String>> ingredients, String[] supplies) {
19+
Set<String> allRecipes = new HashSet<>();
20+
Collections.addAll(allRecipes, recipes);
21+
22+
Set<String> allSupplies = new HashSet<>();
23+
Collections.addAll(allSupplies, supplies);
24+
25+
Map<String, Integer> indegree = new HashMap<>();
26+
Map<String, List<String>> adjList = new HashMap<>();
27+
Map<String, List<String>> ingredientMap = new HashMap<>();
28+
for (int i = 0; i < ingredients.size(); i++) {
29+
int dependencyCount = 0;
30+
for (String ingredient : ingredients.get(i)) {
31+
if (allRecipes.contains(ingredient)) {
32+
dependencyCount++;
33+
List<String> list = adjList.getOrDefault(ingredient, new ArrayList<>());
34+
list.add(recipes[i]);
35+
adjList.put(ingredient, list);
36+
}
37+
}
38+
indegree.put(recipes[i], dependencyCount);
39+
ingredientMap.put(recipes[i], ingredients.get(i));
40+
}
41+
Queue<String> q = new LinkedList<>();
42+
for (Map.Entry<String, Integer> entry : indegree.entrySet()) {
43+
if (entry.getValue() == 0 && allSupplies.containsAll(ingredientMap.get(entry.getKey()))) {
44+
q.offer(entry.getKey());
45+
}
46+
}
47+
List<String> result = new ArrayList<>();
48+
while (!q.isEmpty()) {
49+
String curr = q.poll();
50+
result.add(curr);
51+
for (String neighbor : adjList.getOrDefault(curr, new ArrayList<>())) {
52+
indegree.put(neighbor, indegree.get(neighbor) - 1);
53+
if (indegree.get(neighbor) == 0) {
54+
q.offer(neighbor);
55+
}
56+
}
57+
}
58+
return result;
59+
}
60+
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2115;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
public class _2115Test {
12+
private static _2115.Solution1 solution1;
13+
14+
@BeforeEach
15+
public void setup() {
16+
solution1 = new _2115.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(Arrays.asList("bread"),
22+
solution1.findAllRecipes(new String[]{"bread"},
23+
Arrays.asList(Arrays.asList("yeast", "flour")),
24+
new String[]{"yeast", "flour", "corn"}));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
assertEquals(Arrays.asList("bread", "sandwich"),
30+
solution1.findAllRecipes(new String[]{"bread", "sandwich"},
31+
Arrays.asList(Arrays.asList("yeast", "flour"), Arrays.asList("bread", "meat")),
32+
new String[]{"yeast", "flour", "corn"}));
33+
}
34+
35+
}

0 commit comments

Comments
 (0)