Skip to content

Commit 7a7e795

Browse files
add 2050
1 parent 8b9c165 commit 7a7e795

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|2050|[Parallel Courses III](https://leetcode.com/problems/parallel-courses-iii/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2050.java) ||Hard||
1112
|2048|[Next Greater Numerically Balanced Number](https://leetcode.com/problems/next-greater-numerically-balanced-number/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2048.java) ||Medium||
1213
|2047|[Number of Valid Words in a Sentence](https://leetcode.com/problems/number-of-valid-words-in-a-sentence/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2047.java) ||Easy||
1314
|2044|[Count Number of Maximum Bitwise-OR Subsets](https://leetcode.com/problems/count-number-of-maximum-bitwise-or-subsets/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2044.java) ||Medium||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.LinkedList;
6+
import java.util.Map;
7+
import java.util.Queue;
8+
import java.util.Set;
9+
10+
public class _2050 {
11+
public static class Solution1 {
12+
13+
/**
14+
* My original solution, but results in TLE on LeetCode at 39/40 test cases...
15+
*/
16+
public int minimumTime(int n, int[][] relations, int[] time) {
17+
Map<Integer, Set<Integer>> prereqMap = new HashMap<>();
18+
for (int[] rel : relations) {
19+
int pre = rel[0];
20+
int then = rel[1];
21+
Set<Integer> list = prereqMap.getOrDefault(then, new HashSet<>());
22+
list.add(pre);
23+
prereqMap.put(then, list);
24+
}
25+
Queue<Integer> queue = new LinkedList<>();
26+
Map<Integer, Integer> minMap = new HashMap<>();
27+
for (int i = 0; i < n; i++) {
28+
if (!prereqMap.containsKey(i + 1)) {
29+
minMap.put(i + 1, time[i]);
30+
} else {
31+
queue.add(i + 1);
32+
}
33+
}
34+
int minTime = 0;
35+
while (!queue.isEmpty()) {
36+
Integer curr = queue.poll();
37+
int thisMinTime = Integer.MIN_VALUE;
38+
Set<Integer> reqs = prereqMap.get(curr);
39+
boolean valid = true;
40+
for (int r : reqs) {
41+
if (!minMap.containsKey(r)) {
42+
queue.offer(curr);
43+
valid = false;
44+
break;
45+
} else {
46+
thisMinTime = Math.max(minMap.get(r), thisMinTime);
47+
}
48+
}
49+
if (valid) {
50+
minMap.put(curr, thisMinTime + time[curr - 1]);
51+
}
52+
}
53+
for (int key : minMap.keySet()) {
54+
minTime = Math.max(minTime, minMap.get(key));
55+
}
56+
return minTime;
57+
}
58+
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._2050;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _2050Test {
11+
private static _2050.Solution1 solution1;
12+
private static int[][] relation;
13+
private static int[] time;
14+
private static int n;
15+
private static int expected;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _2050.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
n = 3;
25+
relation = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,3],[2,3]");
26+
time = new int[]{3, 2, 5};
27+
expected = 8;
28+
assertEquals(expected, solution1.minimumTime(n, relation, time));
29+
}
30+
31+
@Test
32+
public void test2() {
33+
n = 5;
34+
relation = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,5],[2,5],[3,5],[3,4],[4,5]");
35+
time = new int[]{1, 2, 3, 4, 5};
36+
expected = 12;
37+
assertEquals(expected, solution1.minimumTime(n, relation, time));
38+
}
39+
40+
41+
}

0 commit comments

Comments
 (0)