题目描述
给出一个m行n列的网格地图,每个位置为0或1,0表示海水1表示陆地。一开始地图全为0(没有陆地)。每次在一个位置加入一块陆地,返回此时地图中陆地的总块数(相邻陆地统计时为同一块陆地)。
Example:
操作#1: addLand(0, 0) turns the water at grid[0][0] into a land.
操作#2: addLand(0, 1) turns the water at grid[0][1] into a land.
操作#3: addLand(1, 2) turns the water at grid[1][2] into a land.
操作#4: addLand(2, 1) turns the water at grid[2][1] into a land.
返回答案数组: [1, 1, 2, 3]
你可以做到复杂度O(k log mn)吗?其中k为操作次数。
分析解答
对于一个静态的地图,统计岛屿个数可以使用dfs(类似于寻找一个图中的连通块个数),算法复杂度是O(m*n)。但是对于一个不断更新的地图,如果我们每次重新统计连通块个数,复杂度为O(m*n*k),其中k为总操作个数。考虑到每次只有一个位置发生变化(从

最低0.47元/天 解锁文章
8万+

被折叠的 条评论
为什么被折叠?



