From 494d4b4534bf48d0b250a7582c859922f9611bb8 Mon Sep 17 00:00:00 2001 From: DengSchoo <1425123490@qq.com> Date: Sat, 3 Aug 2024 22:00:58 +0800 Subject: [PATCH 1/6] feat: add 2nd solution to lc problems with c++ codes: No.2685 --- .../README.md | 60 +++++++++++++++++++ .../README_EN.md | 60 +++++++++++++++++++ .../Solution2.cpp | 24 ++++++++ 3 files changed, 144 insertions(+) create mode 100644 solution/2600-2699/2685.Count the Number of Complete Components/Solution2.cpp diff --git a/solution/2600-2699/2685.Count the Number of Complete Components/README.md b/solution/2600-2699/2685.Count the Number of Complete Components/README.md index 523f109cef196..126f5e4de1e3c 100644 --- a/solution/2600-2699/2685.Count the Number of Complete Components/README.md +++ b/solution/2600-2699/2685.Count the Number of Complete Components/README.md @@ -234,4 +234,64 @@ func countCompleteComponents(n int, edges [][]int) (ans int) { + + +### 方法二:取巧做法 + +> [我的题解](https://leetcode.cn/problems/count-the-number-of-complete-components/solutions/2269282/zui-jian-ji-jie-fa-c-stlzuo-fa-by-dengsc-rw5e/) + +要解决的问题: + +1. 如何保存每一个节点与其它点联通状态 +2. 如何判断多个点是否是一个联通图 + +对于第一点:实际上就是保存了当前到每个点的联通点集合(包括自己),方便后续判等。 +第二点:有了第一点之后,如果是连通图中的点就有: + +1. 此点包含此联通图中所有的点(包括自己) +2. 并且只包含此联通图中的点 + +拿示例一举例: + +- 5包含的联通点有且只有自己,所以是连通图 +- 0包含0、1、2,同理1、2点也是 +- 3和4也是包含自己和彼此 +- 基于以上就有以下代码实现: + + + +#### C++ + +```c++ +class Solution { +public: + int countCompleteComponents(int n, vector>& edges) { + int ans = 0; + vector> m(n + 1, set()); + for (int i = 0; i < n; i++) { + m[i].insert(i); + } + for (auto x : edges) { + m[x[0]].insert(x[1]); + m[x[1]].insert(x[0]); + } + map, int> s; + for (int i = 0; i < n; i++) { + s[m[i]] ++; + } + for (auto &[x,y] : s) { + if (y == x.size()) { + ans ++; + } + } + return ans; + } +}; +// cost:283ms、beats 5%. +``` + + + + + diff --git a/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md b/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md index fb6077f895077..8ba0f5589f1c3 100644 --- a/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md +++ b/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md @@ -222,4 +222,64 @@ func countCompleteComponents(n int, edges [][]int) (ans int) { + + +### 方法二:Simple Method + +> [My Solution Link](https://leetcode.cn/problems/count-the-number-of-complete-components/solutions/2269282/zui-jian-ji-jie-fa-c-stlzuo-fa-by-dengsc-rw5e/) + +Problems needed to solve: + +1. How do we maintain the link state between each node and the others? 如 +2. How can one determine whether multiple points form a connected graph? + +For the first one: we can maintain each node's connection set(including itself). + +For the second one: After solving the first one, we can see: + +- the node itself includes every node in the connected graph(including itself). +- and only connected to the nodes in the connected graph. + +Take example 1 to explain: + +- Node 5's connected node is itself, so it is a connected graph. +- Node 0's connected 0, 1, 2. Same as nodes 1, 2. +- Nodes 3 and 4 also include themselves and each other. + + + +#### C++ + +```c++ +class Solution { +public: + int countCompleteComponents(int n, vector>& edges) { + int ans = 0; + vector> m(n + 1, set()); + for (int i = 0; i < n; i++) { + m[i].insert(i); + } + for (auto x : edges) { + m[x[0]].insert(x[1]); + m[x[1]].insert(x[0]); + } + map, int> s; + for (int i = 0; i < n; i++) { + s[m[i]] ++; + } + for (auto &[x,y] : s) { + if (y == x.size()) { + ans ++; + } + } + return ans; + } +}; +// cost:283ms、beats 5%. +``` + + + + + diff --git a/solution/2600-2699/2685.Count the Number of Complete Components/Solution2.cpp b/solution/2600-2699/2685.Count the Number of Complete Components/Solution2.cpp new file mode 100644 index 0000000000000..1d9762826d9f4 --- /dev/null +++ b/solution/2600-2699/2685.Count the Number of Complete Components/Solution2.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int countCompleteComponents(int n, vector>& edges) { + int ans = 0; + vector> m(n + 1, set()); + for (int i = 0; i < n; i++) { + m[i].insert(i); + } + for (auto x : edges) { + m[x[0]].insert(x[1]); + m[x[1]].insert(x[0]); + } + map, int> s; + for (int i = 0; i < n; i++) { + s[m[i]] ++; + } + for (auto &[x,y] : s) { + if (y == x.size()) { + ans ++; + } + } + return ans; + } +}; From 3b93dd92dccca17f5ad9d011f7c92c6aab14379a Mon Sep 17 00:00:00 2001 From: DengSchoo <1425123490@qq.com> Date: Tue, 6 Aug 2024 21:45:25 +0800 Subject: [PATCH 2/6] feat: add 2nd solution to lc problems with c++ codes: No.2685 --- .../2685.Count the Number of Complete Components/README.md | 2 -- .../2685.Count the Number of Complete Components/README_EN.md | 2 -- 2 files changed, 4 deletions(-) diff --git a/solution/2600-2699/2685.Count the Number of Complete Components/README.md b/solution/2600-2699/2685.Count the Number of Complete Components/README.md index 126f5e4de1e3c..277f3b9d52f07 100644 --- a/solution/2600-2699/2685.Count the Number of Complete Components/README.md +++ b/solution/2600-2699/2685.Count the Number of Complete Components/README.md @@ -238,8 +238,6 @@ func countCompleteComponents(n int, edges [][]int) (ans int) { ### 方法二:取巧做法 -> [我的题解](https://leetcode.cn/problems/count-the-number-of-complete-components/solutions/2269282/zui-jian-ji-jie-fa-c-stlzuo-fa-by-dengsc-rw5e/) - 要解决的问题: 1. 如何保存每一个节点与其它点联通状态 diff --git a/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md b/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md index 8ba0f5589f1c3..50843f2c0300e 100644 --- a/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md +++ b/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md @@ -226,8 +226,6 @@ func countCompleteComponents(n int, edges [][]int) (ans int) { ### 方法二:Simple Method -> [My Solution Link](https://leetcode.cn/problems/count-the-number-of-complete-components/solutions/2269282/zui-jian-ji-jie-fa-c-stlzuo-fa-by-dengsc-rw5e/) - Problems needed to solve: 1. How do we maintain the link state between each node and the others? 如 From a904029097b16598c299dd2c1b236ab98ec3f83a Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 7 Aug 2024 08:26:20 +0800 Subject: [PATCH 3/6] Update README_EN.md --- .../2685.Count the Number of Complete Components/README_EN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md b/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md index 50843f2c0300e..038c1683c0f57 100644 --- a/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md +++ b/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md @@ -224,7 +224,7 @@ func countCompleteComponents(n int, edges [][]int) (ans int) { -### 方法二:Simple Method +### Solution 2: Simple Method Problems needed to solve: From e4726f9380d6df87e91383b4005e65cc445f59fa Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 7 Aug 2024 08:26:38 +0800 Subject: [PATCH 4/6] Update README_EN.md --- .../2685.Count the Number of Complete Components/README_EN.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md b/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md index 038c1683c0f57..f01b9834fd2c6 100644 --- a/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md +++ b/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md @@ -248,7 +248,7 @@ Take example 1 to explain: #### C++ -```c++ +```cpp class Solution { public: int countCompleteComponents(int n, vector>& edges) { @@ -273,7 +273,6 @@ public: return ans; } }; -// cost:283ms、beats 5%. ``` From 0a756cb8e1adf5ce9db98c8cd1a7a9a6e6272e45 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 7 Aug 2024 08:27:26 +0800 Subject: [PATCH 5/6] Update README.md --- .../README.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/solution/2600-2699/2685.Count the Number of Complete Components/README.md b/solution/2600-2699/2685.Count the Number of Complete Components/README.md index 277f3b9d52f07..03951f996f45b 100644 --- a/solution/2600-2699/2685.Count the Number of Complete Components/README.md +++ b/solution/2600-2699/2685.Count the Number of Complete Components/README.md @@ -251,16 +251,16 @@ func countCompleteComponents(n int, edges [][]int) (ans int) { 拿示例一举例: -- 5包含的联通点有且只有自己,所以是连通图 -- 0包含0、1、2,同理1、2点也是 -- 3和4也是包含自己和彼此 -- 基于以上就有以下代码实现: +- 5 包含的联通点有且只有自己,所以是连通图 +- 0 包含 0、1、2,同理 1、2 点也是 +- 3 和 4 也是包含自己和彼此 +- 基于以上就有以下代码实现: #### C++ -```c++ +```cpp class Solution { public: int countCompleteComponents(int n, vector>& edges) { @@ -285,7 +285,6 @@ public: return ans; } }; -// cost:283ms、beats 5%. ``` From ebb21b805599e6a3d1b238d66d8e5ebf3eae9733 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 7 Aug 2024 00:28:12 +0000 Subject: [PATCH 6/6] style: format code and docs with prettier --- .../README_EN.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md b/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md index f01b9834fd2c6..f0292e5585d04 100644 --- a/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md +++ b/solution/2600-2699/2685.Count the Number of Complete Components/README_EN.md @@ -224,7 +224,7 @@ func countCompleteComponents(n int, edges [][]int) (ans int) { -### Solution 2: Simple Method +### Solution 2: Simple Method Problems needed to solve: @@ -235,14 +235,14 @@ For the first one: we can maintain each node's connection set(including itself). For the second one: After solving the first one, we can see: -- the node itself includes every node in the connected graph(including itself). -- and only connected to the nodes in the connected graph. +- the node itself includes every node in the connected graph(including itself). +- and only connected to the nodes in the connected graph. Take example 1 to explain: -- Node 5's connected node is itself, so it is a connected graph. -- Node 0's connected 0, 1, 2. Same as nodes 1, 2. -- Nodes 3 and 4 also include themselves and each other. +- Node 5's connected node is itself, so it is a connected graph. +- Node 0's connected 0, 1, 2. Same as nodes 1, 2. +- Nodes 3 and 4 also include themselves and each other.