Skip to content

Commit 29010b0

Browse files
author
haotf
committed
feat: dijkstra
1 parent cfcb131 commit 29010b0

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

Diff for: 743.网络延迟时间.java

+62-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
import java.util.Arrays;
2+
import java.util.HashMap;
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.PriorityQueue;
7+
18
/*
29
* @lc app=leetcode.cn id=743 lang=java
310
*
@@ -6,9 +13,63 @@
613

714
// @lc code=start
815
class Solution {
16+
public static List<int[]>[] graph;
17+
918
public int networkDelayTime(int[][] times, int n, int k) {
19+
buildGraph(times, n);
20+
int[] distToTarget = new int[n + 1];
21+
Arrays.fill(distToTarget, Integer.MAX_VALUE);
22+
distToTarget[k] = 0;
23+
24+
PriorityQueue<State> pq = new PriorityQueue<State>((a, b) -> a.distFromStart - b.distFromStart);
25+
pq.add(new State(k, 0));
26+
27+
while (!pq.isEmpty()) {
28+
State state = pq.poll();
29+
if (state.distFromStart > distToTarget[state.id])
30+
continue;
31+
List<int[]> targets = graph[state.id];
32+
for (int[] target : targets) {
33+
int t = target[0];
34+
int w = target[1];
35+
int distFromStart = w + state.distFromStart;
36+
if (distToTarget[t] > distFromStart) {
37+
distToTarget[t] = distFromStart;
38+
pq.offer(new State(t, distFromStart));
39+
}
40+
}
41+
}
42+
int max = 0;
43+
for (int i = 1; i < distToTarget.length; i++) {
44+
max = Math.max(max, distToTarget[i]);
45+
}
46+
47+
return max == Integer.MAX_VALUE ? -1 : max;
48+
49+
}
1050

51+
private static void buildGraph(int[][] times, int n) {
52+
List<int[]>[] list = new LinkedList[n + 1];
53+
for (int i = 0; i < list.length; i++) {
54+
list[i] = new LinkedList<>();
55+
}
56+
for (int[] g : times) {
57+
int from = g[0];
58+
int to = g[1];
59+
int weight = g[2];
60+
list[from].add(new int[] { to, weight });
61+
}
62+
graph = list;
1163
}
1264
}
13-
// @lc code=end
1465

66+
class State {
67+
public int id;
68+
public int distFromStart;
69+
70+
public State(int id, int distFromStart) {
71+
this.id = id;
72+
this.distFromStart = distFromStart;
73+
}
74+
}
75+
// @lc code=end

Diff for: 817.链表组件.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.HashSet;
4+
import java.util.List;
5+
import java.util.Set;
6+
import java.util.stream.Collectors;
7+
8+
/*
9+
* @lc app=leetcode.cn id=817 lang=java
10+
*
11+
* [817] 链表组件
12+
*/
13+
14+
// @lc code=start
15+
/**
16+
* Definition for singly-linked list.
17+
* public class ListNode {
18+
* int val;
19+
* ListNode next;
20+
* ListNode() {}
21+
* ListNode(int val) { this.val = val; }
22+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
23+
* }
24+
*/
25+
class Solution {
26+
public int numComponents(ListNode head, int[] nums) {
27+
Set<Integer> set = new HashSet<>(Arrays.stream(nums).boxed().collect(Collectors.toList()));
28+
int res = 0;
29+
ListNode p = head;
30+
31+
boolean start = false;
32+
while (p != null) {
33+
if (set.contains(p.val)) {
34+
if (start == false) {
35+
start = true;
36+
res++;
37+
}
38+
} else {
39+
start = false;
40+
}
41+
p = p.next;
42+
}
43+
44+
return res;
45+
}
46+
}
47+
// @lc code=end

0 commit comments

Comments
 (0)