博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Number of Islands II
阅读量:6339 次
发布时间:2019-06-22

本文共 3295 字,大约阅读时间需要 10 分钟。

Problem Description:

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 00 0 00 0 0

Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land.

1 0 00 0 0   Number of islands = 10 0 0

Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land.

1 1 00 0 0   Number of islands = 10 0 0

Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land.

1 1 00 0 1   Number of islands = 20 0 0

Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land.

1 1 00 0 1   Number of islands = 30 1 0

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


This problem requires a classic data structure called UnionFind. Take some efforts to learn it at first, like using this offered by . This note is very nicely written. Take some patinece to read through it and you will get a tool that is also helpful in the future :-) 

The C++ code is as follows, taking as an example.

1 class UnionFind2D { 2 public: 3     UnionFind2D(int m, int n) { 4         for (int i = 0; i < m * n; i++) ids.push_back(-1); 5         for (int i = 0; i < m * n; i++) szs.push_back(1); 6         M = m, N = n, cnt = 0; 7     } 8     int index(int x, int y) { 9         return x * N + y;10     }11     int size(void) {12         return cnt;13     }14     int id(int x, int y) {15         if (x >= 0 && x < M && y >= 0 && y < N)16             return ids[index(x, y)];17         return -1;18     }19     int add(int x, int y) {20         int idx = index(x, y);21         ids[idx] = idx;22         szs[idx] = 1;23         cnt++;24         return idx;25     }26     int root(int i) {27         for (; i != ids[i]; i = ids[i])28             ids[i] = ids[ids[i]];29         return i;30     }31     bool find(int p, int q) {32         return root(p) == root(q);33     }34     void unite(int p, int q) {35         int i = root(p), j = root(q);36         if (szs[i] > szs[j]) swap(i, j);37         ids[i] = j;38         szs[j] += szs[i];39         cnt--;40     }41 private:42     vector
ids, szs;43 int M, N, cnt;44 };45 46 class Solution {47 public:48 vector
numIslands2(int m, int n, vector
>& positions) {49 UnionFind2D islands(m, n);50 vector
> dirs = { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 } };51 vector
ans;52 for (auto& pos : positions) {53 int x = pos.first, y = pos.second;54 int p = islands.add(x, y);55 for (auto& d : dirs) {56 int q = islands.id(x + d.first, y + d.second);57 if (q >= 0 && !islands.find(p, q))58 islands.unite(p, q);59 }60 ans.push_back(islands.size());61 }62 return ans;63 }64 };

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4965051.html

你可能感兴趣的文章
9种用户体验设计的状态是必须知道的(五)
查看>>
解决WIN7下组播问题
查看>>
陈松松:视频营销成交率低,这三个因素没到位
查看>>
vmware nat模式原理探究,实现虚拟机跨网段管理
查看>>
JavaSE 学习参考:集合运算
查看>>
【Signals and Systems】 SYLLABUS
查看>>
RH135-2-command-line-interface
查看>>
mac下开启docker API远程调用
查看>>
tar 命令的详解
查看>>
Cisco路由器安全配置
查看>>
第十次作业
查看>>
给定一个字符串s,返回去掉子串"mi"后的字符串。
查看>>
Nginx 外的另一选择,轻量级开源 Web 服务器 Tengine 发布新版本
查看>>
Wrod中超链接的一些技巧
查看>>
IP_VFR-4-FRAG_TABLE_OVERFLOW【cisco设备报错】碎片***
查看>>
Codeforces Round #256 (Div. 2) D. Multiplication Table 【二分】
查看>>
ARM汇编指令格式
查看>>
HDU-2044-一只小蜜蜂
查看>>
HDU-1394-Minimum Inversion Number
查看>>
[转] createObjectURL方法 实现本地图片预览
查看>>