★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-precusnv-ks.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
A 2d grid map of m
rows and n
columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand operation. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.git
Example:github
Given m = 3, n = 3
, positions = [[0,0], [0,1], [1,2], [2,1]]
.
Initially, the 2d grid grid
is filled with water. (Assume 0 represents water and 1 represents land).數組
0 0 0 0 0 0 0 0 0
Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land.微信
1 0 0 0 0 0 Number of islands = 1 0 0 0
Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land.app
1 1 0 0 0 0 Number of islands = 1 0 0 0
Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land.測試
1 1 0 0 0 1 Number of islands = 2 0 0 0
Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land.spa
1 1 0 0 0 1 Number of islands = 3 0 1 0
We return the result as an array: [1, 1, 2, 3]
code
Challenge:orm
Can you do it in time complexity O(k log mn), where k is the length of the positions
?
一個由m行和n列組成的二維網格圖最初是用水填充的。咱們能夠執行一個加法運算,把位置(行,列)的水變成一個陸地。給定一個要操做的位置列表,計算每一個addland操做後的島數。島嶼被水環繞,由水平或垂直鏈接相鄰土地造成。您能夠假設網格的全部四個邊緣都被水包圍。
例子:
給定 m = 3, n = 3
, positions = [[0,0], [0,1], [1,2], [2,1]]
.
最初,二維網格充滿了水。(假設0表明水,1表明土地)。
0 0 0 0 0 0 0 0 0
操做1:addland(0,0)將網格[0][0]處的水變成陸地。
1 0 0 0 0 0 島嶼個數 = 1 0 0 0
操做2:addland(0,1)將網格[0][1]處的水轉化爲陸地。
1 1 0 0 0 0 島嶼個數 = 1 0 0 0
操做3:addland(1,2)將網格[1][2]處的水變成陸地。
1 1 0 0 0 1 島嶼個數 = 2 0 0 0
操做4:addland(2,1)將網格[2][1]處的水變成陸地。
1 1 0 0 0 1 島嶼個數 = 3 0 1 0
咱們以數組的形式返回結果:[1,1,2,3]
挑戰:
你能在時間複雜度o(k log mn)中作到嗎?k是位置的長度?
Solution:
1 class Solution { 2 func numIslands2(_ m:Int,_ n:Int,_ positions:inout [[Int]]) ->[Int] { 3 var res:[Int] = [Int]() 4 var cnt:Int = 0 5 var roots:[Int] = [Int](repeating:-1,count:m * n) 6 var dirs:[[Int]] = [[0, -1],[-1, 0],[0, 1],[1, 0]] 7 for a in positions 8 { 9 var id:Int = n * a[0] + a[1] 10 if roots[id] == -1 11 { 12 roots[id] = id 13 cnt += 1 14 } 15 for dir in dirs 16 { 17 var x:Int = a[0] + dir[0] 18 var y:Int = a[1] + dir[1] 19 var cur_id:Int = n * x + y 20 if x < 0 || x >= m || y < 0 || y >= n || roots[cur_id] == -1 21 { 22 continue 23 } 24 var p:Int = findRoot(&roots, cur_id) 25 var q:Int = findRoot(&roots, id) 26 if p != q 27 { 28 roots[p] = q 29 cnt -= 1 30 } 31 } 32 res.append(cnt) 33 } 34 return res 35 } 36 37 func findRoot(_ roots:inout [Int],_ id:Int) -> Int 38 { 39 return (id == roots[id]) ? id : findRoot(&roots, roots[id]) 40 } 41 }
點擊:Playground測試
1 var sol = Solution() 2 let m:Int = 3 3 let n:Int = 3 4 var positions:[[Int]] = [[0,0], [0,1], [1,2], [2,1]] 5 print(sol.numIslands2(m,n,&positions)) 6 //Print [1, 1, 2, 3]