|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Queue; |
| 9 | + |
| 10 | +/** |
| 11 | + * 1377. Frog Position After T Seconds |
| 12 | + * |
| 13 | + * Given an undirected tree consisting of n vertices numbered from 1 to n. |
| 14 | + * A frog starts jumping from the vertex 1. |
| 15 | + * In one second, the frog jumps from its current vertex to another unvisited vertex if they are directly connected. |
| 16 | + * The frog can not jump back to a visited vertex. |
| 17 | + * In case the frog can jump to several vertices it jumps randomly to one of them with the same probability, |
| 18 | + * otherwise, when the frog can not jump to any unvisited vertex it jumps forever on the same vertex. |
| 19 | + * The edges of the undirected tree are given in the array edges, where edges[i] = [fromi, toi] means that exists an edge connecting directly the vertices fromi and toi. |
| 20 | + * Return the probability that after t seconds the frog is on the vertex target. |
| 21 | + * |
| 22 | + * Example 1: |
| 23 | + * Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 2, target = 4 |
| 24 | + * Output: 0.16666666666666666 |
| 25 | + * Explanation: The figure above shows the given graph. |
| 26 | + * The frog starts at vertex 1, jumping with 1/3 probability to the vertex 2 after second 1 and |
| 27 | + * then jumping with 1/2 probability to vertex 4 after second 2. |
| 28 | + * Thus the probability for the frog is on the vertex 4 after 2 seconds is 1/3 * 1/2 = 1/6 = 0.16666666666666666. |
| 29 | + * |
| 30 | + * Example 2: |
| 31 | + * Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 1, target = 7 |
| 32 | + * Output: 0.3333333333333333 |
| 33 | + * Explanation: The figure above shows the given graph. The frog starts at vertex 1, jumping with 1/3 = 0.3333333333333333 probability to the vertex 7 after second 1. |
| 34 | + * |
| 35 | + * Example 3: |
| 36 | + * Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 20, target = 6 |
| 37 | + * Output: 0.16666666666666666 |
| 38 | + * |
| 39 | + * Constraints: |
| 40 | + * 1 <= n <= 100 |
| 41 | + * edges.length == n-1 |
| 42 | + * edges[i].length == 2 |
| 43 | + * 1 <= edges[i][0], edges[i][1] <= n |
| 44 | + * 1 <= t <= 50 |
| 45 | + * 1 <= target <= n |
| 46 | + * Answers within 10^-5 of the actual value will be accepted as correct. |
| 47 | + * */ |
| 48 | +public class _1377 { |
| 49 | + public static class Solution1 { |
| 50 | + public double frogPosition(int n, int[][] edges, int t, int target) { |
| 51 | + List<Integer>[] graph = new ArrayList[n]; |
| 52 | + for (int i = 0; i < n; i++) { |
| 53 | + graph[i] = new ArrayList<>(); |
| 54 | + } |
| 55 | + for (int[] edge : edges) { |
| 56 | + graph[edge[0] - 1].add(edge[1] - 1); |
| 57 | + graph[edge[1] - 1].add(edge[0] - 1); |
| 58 | + } |
| 59 | + boolean[] visited = new boolean[n]; |
| 60 | + visited[0] = true; |
| 61 | + double[] probabilities = new double[n]; |
| 62 | + probabilities[0] = 1f; |
| 63 | + Queue<Integer> queue = new LinkedList<>(); |
| 64 | + queue.offer(0); |
| 65 | + while (!queue.isEmpty() && t-- > 0) { |
| 66 | + for (int i = queue.size(); i > 0; i--) { |
| 67 | + int vertex = queue.poll(); |
| 68 | + int nextVerticesCount = 0; |
| 69 | + for (int v : graph[vertex]) { |
| 70 | + if (!visited[v]) { |
| 71 | + nextVerticesCount++; |
| 72 | + } |
| 73 | + } |
| 74 | + for (int v : graph[vertex]) { |
| 75 | + if (!visited[v]) { |
| 76 | + visited[v] = true; |
| 77 | + queue.offer(v); |
| 78 | + probabilities[v] = probabilities[vertex] / nextVerticesCount; |
| 79 | + } |
| 80 | + } |
| 81 | + if (nextVerticesCount > 0) { |
| 82 | + probabilities[vertex] = 0; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + return probabilities[target - 1]; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments