forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1377.java
54 lines (52 loc) · 1.95 KB
/
_1377.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
public class _1377 {
public static class Solution1 {
/**
* credit: https://leetcode.com/problems/frog-position-after-t-seconds/discuss/532505/Java-Straightforward-BFS-Clean-code-O(N)
*/
public double frogPosition(int n, int[][] edges, int t, int target) {
List<Integer>[] graph = new ArrayList[n];
for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<>();
}
for (int[] edge : edges) {
graph[edge[0] - 1].add(edge[1] - 1);
graph[edge[1] - 1].add(edge[0] - 1);
}
boolean[] visited = new boolean[n];
visited[0] = true;
double[] probabilities = new double[n];
probabilities[0] = 1f;
Queue<Integer> queue = new LinkedList<>();
queue.offer(0);
while (!queue.isEmpty() && t-- > 0) {
for (int i = queue.size(); i > 0; i--) {
int vertex = queue.poll();
int nextVerticesCount = 0;
for (int next : graph[vertex]) {
if (!visited[next]) {
nextVerticesCount++;
}
}
for (int next : graph[vertex]) {
if (!visited[next]) {
visited[next] = true;
queue.offer(next);
probabilities[next] = probabilities[vertex] / nextVerticesCount;
}
}
if (nextVerticesCount > 0) {
probabilities[vertex] = 0;
}
}
}
return probabilities[target - 1];
}
}
}