Skip to content

Commit 3e0ea65

Browse files
committedApr 12, 2025
Add solution #1377
1 parent 725b7bc commit 3e0ea65

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,263 LeetCode solutions in JavaScript
1+
# 1,264 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1049,6 +1049,7 @@
10491049
1374|[Generate a String With Characters That Have Odd Counts](./solutions/1374-generate-a-string-with-characters-that-have-odd-counts.js)|Easy|
10501050
1375|[Number of Times Binary String Is Prefix-Aligned](./solutions/1375-number-of-times-binary-string-is-prefix-aligned.js)|Medium|
10511051
1376|[Time Needed to Inform All Employees](./solutions/1376-time-needed-to-inform-all-employees.js)|Medium|
1052+
1377|[Frog Position After T Seconds](./solutions/1377-frog-position-after-t-seconds.js)|Hard|
10521053
1380|[Lucky Numbers in a Matrix](./solutions/1380-lucky-numbers-in-a-matrix.js)|Easy|
10531054
1389|[Create Target Array in the Given Order](./solutions/1389-create-target-array-in-the-given-order.js)|Easy|
10541055
1400|[Construct K Palindrome Strings](./solutions/1400-construct-k-palindrome-strings.js)|Medium|
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* 1377. Frog Position After T Seconds
3+
* https://leetcode.com/problems/frog-position-after-t-seconds/
4+
* Difficulty: Hard
5+
*
6+
* Given an undirected tree consisting of n vertices numbered from 1 to n. A frog starts
7+
* jumping from vertex 1. In one second, the frog jumps from its current vertex to another
8+
* unvisited vertex if they are directly connected. The frog can not jump back to a visited
9+
* vertex. In case the frog can jump to several vertices, it jumps randomly to one of them
10+
* with the same probability. Otherwise, when the frog can not jump to any unvisited vertex,
11+
* it jumps forever on the same vertex.
12+
*
13+
* The edges of the undirected tree are given in the array edges, where edges[i] = [ai, bi]
14+
* means that exists an edge connecting the vertices ai and bi.
15+
*
16+
* Return the probability that after t seconds the frog is on the vertex target. Answers
17+
* within 10-5 of the actual answer will be accepted.
18+
*/
19+
20+
/**
21+
* @param {number} n
22+
* @param {number[][]} edges
23+
* @param {number} t
24+
* @param {number} target
25+
* @return {number}
26+
*/
27+
var frogPosition = function(n, edges, t, target) {
28+
const adjacencyList = Array.from({ length: n + 1 }, () => []);
29+
for (const [a, b] of edges) {
30+
adjacencyList[a].push(b);
31+
adjacencyList[b].push(a);
32+
}
33+
34+
return explore(1, 0, 1, new Set());
35+
36+
function explore(vertex, time, probability, visited) {
37+
if (time > t) return 0;
38+
if (vertex === target && time === t) return probability;
39+
if (vertex === target && adjacencyList[vertex].every(v => visited.has(v))) return probability;
40+
41+
const unvisitedNeighbors = adjacencyList[vertex].filter(v => !visited.has(v));
42+
if (unvisitedNeighbors.length === 0) return 0;
43+
44+
const nextProbability = probability / unvisitedNeighbors.length;
45+
visited.add(vertex);
46+
47+
for (const neighbor of unvisitedNeighbors) {
48+
const result = explore(neighbor, time + 1, nextProbability, visited);
49+
if (result > 0) return result;
50+
}
51+
52+
return 0;
53+
}
54+
};

0 commit comments

Comments
 (0)
Please sign in to comment.