Skip to content

Commit 0e6c1ca

Browse files
add 3112
1 parent d22ee01 commit 0e6c1ca

File tree

3 files changed

+84
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+84
-0
lines changed

Diff for: paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
| 3131 | [Find the Integer Added to Array I](https://leetcode.com/problems/find-the-integer-added-to-array-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3131.java) | | Easy |
3737
| 3127 | [Make a Square with the Same Color](https://leetcode.com/problems/make-a-square-with-the-same-color/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3127.java) | | Easy |
3838
| 3120 | [Count the Number of Special Characters I](https://leetcode.com/problems/count-the-number-of-special-characters-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3120.java) | | Easy |
39+
| 3112 | [Minimum Time to Visit Disappearing Nodes](https://leetcode.com/problems/minimum-time-to-visit-disappearing-nodes/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3112.java) | | Medium | Graph, Shortest Path
3940
| 3110 | [Score of a String](https://leetcode.com/problems/score-of-a-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3110.java) | | Easy |
4041
| 3095 | [Shortest Subarray With OR at Least K I](https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3095.java) | | Easy |
4142
| 3090 | [Maximum Length Substring With Two Occurrences](https://leetcode.com/problems/maximum-length-substring-with-two-occurrences/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3090.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.PriorityQueue;
7+
8+
public class _3112 {
9+
public static class Solution1 {
10+
/**
11+
* My completely original solution: Dijkstra's algorithm!
12+
*/
13+
public int[] minimumTime(int n, int[][] edges, int[] disappear) {
14+
List<int[]>[] graph = new ArrayList[n];
15+
for (int i = 0; i < n; i++) {
16+
graph[i] = new ArrayList<>();
17+
}
18+
for (int[] edge : edges) {
19+
graph[edge[0]].add(new int[]{edge[1], edge[2]});
20+
graph[edge[1]].add(new int[]{edge[0], edge[2]});
21+
}
22+
int[] ans = new int[n];
23+
int[] shortestTimes = new int[disappear.length];
24+
Arrays.fill(shortestTimes, Integer.MAX_VALUE);
25+
shortestTimes[0] = 0;
26+
dijkstra(graph, disappear, shortestTimes);
27+
for (int target = 1; target < n; target++) {
28+
if (shortestTimes[target] == Integer.MAX_VALUE || shortestTimes[target] >= disappear[target]) {
29+
ans[target] = -1;
30+
} else {
31+
ans[target] = shortestTimes[target];
32+
}
33+
}
34+
return ans;
35+
}
36+
37+
private void dijkstra(List<int[]>[] graph, int[] disappear, int[] shortestTimes) {
38+
PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> a[1] - b[1]);
39+
q.offer(new int[]{0, 0});
40+
while (!q.isEmpty()) {
41+
int[] curr = q.poll();
42+
int currNode = curr[0];
43+
int currCost = curr[1];
44+
if (currCost > shortestTimes[currNode]) {
45+
continue;
46+
}
47+
for (int[] neighbor : graph[currNode]) {
48+
int neighborNode = neighbor[0];
49+
int neighborCost = neighbor[1];
50+
if (neighborCost + currCost < shortestTimes[neighborNode] && neighborCost + currCost < disappear[neighborNode]) {
51+
shortestTimes[neighborNode] = neighborCost + currCost;
52+
q.offer(new int[]{neighborNode, shortestTimes[neighborNode]});
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions.fourththousand._3112;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
9+
10+
public class _3112Test {
11+
private static _3112.Solution1 solution1;
12+
13+
@BeforeEach
14+
public void setup() {
15+
solution1 = new _3112.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertArrayEquals(new int[]{0, -1, 4}, solution1
21+
.minimumTime(3, CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray
22+
("[0,1,2],[1,2,1],[0,2,4]"),
23+
new int[]{1, 1, 5}));
24+
}
25+
}

0 commit comments

Comments
 (0)