5355. T 秒后青蛙的位置

给你一棵由 n 个顶点组成的无向树,顶点编号从 1 到 n。青蛙从 顶点 1 开始起跳。规则如下:

在一秒内,青蛙从它所在的当前顶点跳到另一个 未访问 过的顶点(如果它们直接相连)。

青蛙无法跳回已经访问过的顶点。

如果青蛙可以跳到多个不同顶点,那么它跳到其中任意一个顶点上的机率都相同。

如果青蛙不能跳到任何未访问过的顶点上,那么它每次跳跃都会停留在原地。

无向树的边用数组 edges 描述,其中 edges[i] = [fromi, toi] 意味着存在一条直接连通 fromi 和 toi 两个顶点的边。

返回青蛙在 t 秒后位于目标顶点 target 上的概率。

示例 1:

输入:n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 2, target = 4

输出:0.16666666666666666

解释:上图显示了青蛙的跳跃路径。青蛙从顶点 1 起跳,第 1 秒 有 1/3 的概率跳到顶点 2 ,然后第 2 秒 有 1/2 的概率跳到顶点 4,因此青蛙在 2 秒后位于顶点 4 的概率是 1/3 * 1/2 = 1/6 = 0.16666666666666666 。

示例 2:

输入:n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 1, target = 7

输出:0.3333333333333333

解释:上图显示了青蛙的跳跃路径。青蛙从顶点 1 起跳,有 1/3 = 0.3333333333333333 的概率能够 1 秒 后跳到顶点 7 。

示例 3:

输入:n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 20, target = 6

输出:0.16666666666666666

提示:

1 <= n <= 100
edges.length == n-1
edges[i].length == 2
1 <= edges[i][0], edges[i][1] <= n
1 <= t <= 50
1 <= target <= n
与准确值误差在 10^-5 之内的结果将被判定为正确。

PS:

我们把图放进map,然后,便利出每一个尝试,暴力大法。

匹配的条件是,时间到了,要么就是跳进深渊

如果有C++大哥可以查看这里

写的一级棒

class Solution {
public double frogPosition(int n, int[][] edges, int t, int target) {
boolean[] visited = new boolean[n + 1];
Map<Integer, List<Integer>> map = new HashMap<>();
for (int[] e: edges) {
map.putIfAbsent(e[0], new LinkedList<>());
map.get(e[0]).add(e[1]);
map.putIfAbsent(e[1], new LinkedList<>());
map.get(e[1]).add(e[0]);
}
return dfs(map, visited, t, target, 1, 1);
} public double dfs(Map<Integer, List<Integer>> map, boolean[] visited, int t, int target, int cur, double p) {
if (t < 0) {
return 0;
}
List<Integer> next = map.getOrDefault(cur, Collections.emptyList()).stream().filter(i -> !visited[i]).collect(Collectors.toList());
if (t == 0 || next.size() == 0) {
return cur == target ? p : 0;
}
double res = 0;
p /= next.size();
visited[cur] = true;
for (Integer n: next) {
if ((res = dfs(map, visited, t - 1, target, n, p)) > 0) {
return res;
}
}
visited[cur] = false;
return 0;
}
}

Java实现 LeetCode 5355 T 秒后青蛙的位置的更多相关文章

  1. Java实现 LeetCode 145 二叉树的后序遍历

    145. 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成 ...

  2. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. Java for LeetCode 057 Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  4. Java for LeetCode 047 Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  5. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(五)针对hadoop2.9.0启动之后发现slave上正常启动了DataNode,DataManager,但是过了几秒后发现DataNode被关闭

    启动之后发现slave上正常启动了DataNode,DataManager,但是过了几秒后发现DataNode被关闭 以slave1上错误日期为例查看错误信息: /logs/hadoop-spark- ...

  6. LeetCode:二叉树的后序遍历【145】

    LeetCode:二叉树的后序遍历[145] 题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

  7. js5秒后自动关闭本页面及5秒钟后自动跳转指定页面的方法

    5秒钟后自动关闭 <!DOCTYPE HTML> <html> <head> <title>倒计时自动关闭/跳转页面</title> < ...

  8. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  9. 弹出层提示,X秒后关闭

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

随机推荐

  1. C++单例写法

    #define __xx(WaveClassFile::me()) class Xx : public QObject{ Q_OBJECT public: static Xx & me(); ...

  2. STM32 基于 CubeMX配置GPIO点亮LED灯(超级详细+图文并茂)

    我是一个只会点灯的菜鸟: 相关文章 [STM32系列汇总]小白博主的STM32实战快速进阶之路(持续更新) 文章目录 相关文章 1 前言 2 理论分析 2.1 LED 原理 2.2 板载资料 2.3 ...

  3. Mysql常用sql语句(22)- insert 插入数据

    测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 前言 终于讲完基础的查询语句了...这篇讲的就是插入数 ...

  4. 手机网页,div内滚动条,以及div内部滚动条拉到底部之后触发事件

    var gao = document.documentElement.clientHeight; var headHeight = parseInt($('.yhead').css('height') ...

  5. PHP EOF使用说明

    PHP EOF(heredoc) 使用说明 PHP EOF(heredoc)是一种在命令行shell(如sh.csh.ksh.bash.PowerShell和zsh)和程序语言(像Perl.PHP.P ...

  6. Ubuntu16.04 flask + nginx + uWSGI 部署

    前言 又有段时间没写博客了,最近一直在写外包项目,都没啥空余时间.这几天花了不少时间做项目部署,也看了不少教程,这里就记录下整个过程,也方便以后要做类似部署的时候不用再查来查去了. flask + u ...

  7. python re 里面match 和search的区别

    re.match()从开头开始匹配string. re.search()从anywhere 来匹配string. 例子: >>> re.match("c", &q ...

  8. Spark_Transformation和Action算子

    Transformation 和 Action 常用算子 ​ 一.Transformation        1.1 map        1.2 filter        1.3 flatMap  ...

  9. Django之form表单常用字段与插件

    from django.shortcuts import render,HttpResponse from django import forms from app01 import models f ...

  10. 分布式应用程序协调服务 ZooKeeper

    1.简介: ZooKeeper 是一个分布的.开源的协调服务,它主要是用来解决分布式应用中经常遇到的一些数据管理问题.统一命名服务.状态同步服务.集群管理.分布式应用配置项的管理等,简化分布式应用协调 ...