Re: [閒聊] 每日leetcode

看板Marginalman作者 (神楽めあ的錢包)時間10月前 (2025/01/22 23:21), 編輯推噓1(101)
留言2則, 1人參與, 10月前最新討論串1300/1553 (看更多)
1765. Map of Highest Peak 思路: 先記錄一下水在哪幾格 並且用level紀錄現在的高度 接著就bfs就好 golang code : func highestPeak(isWater [][]int) [][]int { visited := [1001][1001]bool{} queue := []int{} n, m := len(isWater), len(isWater[0]) for i := 0; i < n; i++ { for j := 0; j < m; j++ { if isWater[i][j] == 1 { visited[i][j] = true queue = append(queue, i*m+j) isWater[i][j] = 0 } } } level := 1 chk := func(x, y int) { if x > -1 && y > -1 && x < n && y < m && !visited[x][y] { isWater[x][y] = level visited[x][y] = true queue = append(queue, x*m+y) } } for len(queue) > 0 { cnt := len(queue) for cnt > 0 { cur_node := queue[0] x, y := cur_node/m, cur_node%m queue = queue[1:] cnt-- chk(x+1, y) chk(x-1, y) chk(x, y+1) chk(x, y-1) } level++ } return isWater } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.72.229.235 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1737559301.A.6D6.html

01/22 23:59, 10月前 , 1F
大師
01/22 23:59, 1F

01/22 23:59, 10月前 , 2F
剩我一格一格填了
01/22 23:59, 2F
文章代碼(AID): #1daGq5RM (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1daGq5RM (Marginalman)