Number of Islands II | LeetCode

Number of Islands II

My Submissions
Total Accepted: 1666  Total Submissions: 5588  Difficulty: Hard

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.

Example:

Given m = 3, n = 3positions = [[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.

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.

1 1 0
0 0 1   Number of islands = 3
0 1 0

We return the result as an array: [1, 1, 2, 3]

Challenge:

Can you do it in time complexity O(k log mn), where k is the length of the positions?



 思路:
我最开始想的是,保留一个M*N的map,然后每次有新的岛都把相应的位置标上label。 然后如果添加的新岛周围有旧的岛,那么bfs一下,把所有的label都变成最小值。这样每次添加一个岛,都要做一次bfs。显然超时了。
然后我开始想,那么我可以省去bfs这一步,取而代之的是我保留一个unordered_map<int,int>, key是标签,value是它对应的最小的标号。比如当以下情况:
0 2 0
3 * 0
当我们想插入新的岛屿到*的位置时,我们不用把2和3都改成2。而是记录下来<2,2>, <3,2> 来表示,编号3其实就是编号2,编号2就是他自己。然后再把*赋予成2。 需要注意的是每次探索插入点的四周是否有存在岛屿时,我们不能直接看岛屿的编号,而是要根据hashmap中每一个编号对应的最小点,再拿对应的最小点来比较。因为有可能2其实是1,3也其实是1。这种情况下,就没有把2和3连在一起,只是在1的基础上又加了一个岛罢了。

代码如下:
class Solution {
 public:
	 vector<int> numIslands2(int m, int n, vector<pair<int, int>>& positions) {
	     //init
		 vector<int> result;
		 vector<vector<int>> mp(m + 2, vector<int>(n + 2, 0));
		 unordered_map<int, int> used;
		 int numofislands = 0;
		 int label = 0;
		 
		 //traverse positions
		 for (auto &pair : positions) {
			 int x = pair.first + 1, y = pair.second + 1;
			 if (mp[x][y] != 0)
				 continue;
			 set<int> neighbor;
			 //find whether thereis island in neighbor
			 if (mp[x - 1][y] != 0) {
				 int cur = mp[x - 1][y];
				 while (used.find(cur) != used.end() && used[cur] != cur)
					 cur = used[cur];
				 neighbor.insert(cur);
			 }
			 if (mp[x + 1][y] != 0){
				 int cur = mp[x + 1][y];
				 while (used.find(cur) != used.end() && used[cur] != cur)
					 cur = used[cur];
				 neighbor.insert(cur);
			 }
			 if (mp[x][y - 1] != 0){
				 int cur = mp[x][y - 1];
				 while (used.find(cur) != used.end() && used[cur] != cur)
					 cur = used[cur];
				 neighbor.insert(cur);
			 }
			 if (mp[x][y + 1] != 0){
				 int cur = mp[x][y + 1];
				 while (used.find(cur) != used.end() && used[cur] != cur)
					 cur = used[cur];
				 neighbor.insert(cur);
			 }
			 //if not
			 if (neighbor.empty()) {
				 numofislands++;
				 label++;
				 mp[x][y] = label;
			 } else {
			 //if there is 
				 int min = *neighbor.begin();
				 for (auto i : neighbor) {
					 used[i] = min;
				 }
				 mp[x][y] = min;
				 numofislands = numofislands - neighbor.size() + 1;
			 }
			 result.push_back(numofislands);
		 }

		 return result;
	 }
 };


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值