leetcode1377_go_T秒后青蛙的位置

内容分享5小时前发布
0 0 0

题目

给你一棵由 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 之内的结果将被判定为正确。

解题思路分析

1、深度优先搜索;时间复杂度O(n),空间复杂度O(n)

var arr [][]int
var res []float64

func frogPosition(n int, edges [][]int, t int, target int) float64 {
   arr = make([][]int, n+1)
   res = make([]float64, n+1)
   res[1] = 1
   for i := 0; i < len(edges); i++ {
      a, b := edges[i][0], edges[i][1]
      arr[a] = append(arr[a], b)
      arr[b] = append(arr[b], a)
   }
   visited := make([]bool, n+1) // 已经访问过的
   visited[1] = true
   dfs(1, t, visited)
   return res[target]
}

func dfs(start int, t int, visited []bool) {
   if t <= 0 {
      return
   }
   count := 0
   for i := 0; i < len(arr[start]); i++ {
      next := arr[start][i]
      if visited[next] == false {
         count++
      }
   }
   if count == 0 {
      return
   }
   per := res[start] / float64(count) // 每一跳的概率
   for i := 0; i < len(arr[start]); i++ {
      next := arr[start][i]
      if visited[next] == false {
         visited[next] = true
         res[start] = res[start] - per // start-per
         res[next] = res[next] + per   // next+per
         dfs(next, t-1, visited)
         visited[next] = false
      }
   }
}

2、广度优先搜索;时间复杂度O(n),空间复杂度O(n)

leetcode1377_go_T秒后青蛙的位置

func frogPosition(n int, edges [][]int, t int, target int) float64 {
   arr := make([][]int, n+1)
   res := make([]float64, n+1)
   res[1] = 1
   for i := 0; i < len(edges); i++ {
      a, b := edges[i][0], edges[i][1]
      arr[a] = append(arr[a], b)
      arr[b] = append(arr[b], a)
   }
   visited := make([]bool, n+1) // 已经访问过的
   queue := make([]int, 0)
   queue = append(queue, 1)
   count := 0
   for len(queue) > 0 {
      length := len(queue)
      if count == t {
         break
      }
      for i := 0; i < length; i++ {
         start := queue[i]
         visited[start] = true
         count := 0
         for j := 0; j < len(arr[start]); j++ {
            next := arr[start][j]
            if visited[next] == false {
               count++
            }
         }
         if count == 0 {
            continue
         }
         per := res[start] / float64(count) // 每一跳的概率
         for j := 0; j < len(arr[start]); j++ {
            next := arr[start][j]
            if visited[next] == false {
               res[start] = res[start] - per // start-per
               res[next] = res[next] + per   // next+per
               queue = append(queue, next)
            }
         }
      }
      queue = queue[length:]
      count++
   }
   return res[target]
}

总结

Hard题目,构建邻接表后,使用深度优先搜索或者广度优先搜索即可

© 版权声明

相关文章

暂无评论

none
暂无评论...