Skip to content

Commit 2c88a80

Browse files
committed
0547-NUMBER-OF-PROVINCES Stats: Time: 0 ms (100%), Space: 23.3 MB (6.63%) - LeetHub
1 parent ef6cee6 commit 2c88a80

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
4+
void dfs(int i, vector<vector<int>>& adj , vector<int> &vis)
5+
{
6+
vis[i] = 1;
7+
for(int it:adj[i])
8+
if(!vis[it])
9+
dfs(it, adj, vis);
10+
}
11+
int findCircleNum(vector<vector<int>>& isConnected) {
12+
int V = isConnected.size();
13+
vector<vector<int>> adj(V, vector<int> (V));
14+
15+
for(int i=0;i<V;i++)
16+
{
17+
for(int j=0;j<V;j++)
18+
{
19+
if(isConnected[i][j] == 1)
20+
{
21+
adj[i].push_back(j);
22+
adj[j].push_back(i);
23+
}
24+
}
25+
}
26+
27+
vector<int> vis(V,0);
28+
int cnt = 0;
29+
for(int i=0;i<V;i++)
30+
{
31+
if(!vis[i])
32+
{
33+
cnt++;
34+
dfs(i, adj, vis);
35+
}
36+
}
37+
38+
return cnt;
39+
}
40+
};

0547-number-of-provinces/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<h2><a href="https://leetcode.com/problems/number-of-provinces">547. Number of Provinces</a></h2><h3>Medium</h3><hr><p>There are <code>n</code> cities. Some of them are connected, while some are not. If city <code>a</code> is connected directly with city <code>b</code>, and city <code>b</code> is connected directly with city <code>c</code>, then city <code>a</code> is connected indirectly with city <code>c</code>.</p>
2+
3+
<p>A <strong>province</strong> is a group of directly or indirectly connected cities and no other cities outside of the group.</p>
4+
5+
<p>You are given an <code>n x n</code> matrix <code>isConnected</code> where <code>isConnected[i][j] = 1</code> if the <code>i<sup>th</sup></code> city and the <code>j<sup>th</sup></code> city are directly connected, and <code>isConnected[i][j] = 0</code> otherwise.</p>
6+
7+
<p>Return <em>the total number of <strong>provinces</strong></em>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg" style="width: 222px; height: 142px;" />
12+
<pre>
13+
<strong>Input:</strong> isConnected = [[1,1,0],[1,1,0],[0,0,1]]
14+
<strong>Output:</strong> 2
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg" style="width: 222px; height: 142px;" />
19+
<pre>
20+
<strong>Input:</strong> isConnected = [[1,0,0],[0,1,0],[0,0,1]]
21+
<strong>Output:</strong> 3
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>1 &lt;= n &lt;= 200</code></li>
29+
<li><code>n == isConnected.length</code></li>
30+
<li><code>n == isConnected[i].length</code></li>
31+
<li><code>isConnected[i][j]</code> is <code>1</code> or <code>0</code>.</li>
32+
<li><code>isConnected[i][i] == 1</code></li>
33+
<li><code>isConnected[i][j] == isConnected[j][i]</code></li>
34+
</ul>

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ this is readme. here i will make
8484
| [0199-binary-tree-right-side-view](https://github.com/TheVinaySagar/Leetcode/tree/master/0199-binary-tree-right-side-view) |
8585
| [0236-lowest-common-ancestor-of-a-binary-tree](https://github.com/TheVinaySagar/Leetcode/tree/master/0236-lowest-common-ancestor-of-a-binary-tree) |
8686
| [0297-serialize-and-deserialize-binary-tree](https://github.com/TheVinaySagar/Leetcode/tree/master/0297-serialize-and-deserialize-binary-tree) |
87+
| [0547-number-of-provinces](https://github.com/TheVinaySagar/Leetcode/tree/master/0547-number-of-provinces) |
8788
| [0662-maximum-width-of-binary-tree](https://github.com/TheVinaySagar/Leetcode/tree/master/0662-maximum-width-of-binary-tree) |
8889
| [0684-redundant-connection](https://github.com/TheVinaySagar/Leetcode/tree/master/0684-redundant-connection) |
8990
| [0854-making-a-large-island](https://github.com/TheVinaySagar/Leetcode/tree/master/0854-making-a-large-island) |
@@ -100,6 +101,7 @@ this is readme. here i will make
100101
| [0199-binary-tree-right-side-view](https://github.com/TheVinaySagar/Leetcode/tree/master/0199-binary-tree-right-side-view) |
101102
| [0297-serialize-and-deserialize-binary-tree](https://github.com/TheVinaySagar/Leetcode/tree/master/0297-serialize-and-deserialize-binary-tree) |
102103
| [0407-trapping-rain-water-ii](https://github.com/TheVinaySagar/Leetcode/tree/master/0407-trapping-rain-water-ii) |
104+
| [0547-number-of-provinces](https://github.com/TheVinaySagar/Leetcode/tree/master/0547-number-of-provinces) |
103105
| [0662-maximum-width-of-binary-tree](https://github.com/TheVinaySagar/Leetcode/tree/master/0662-maximum-width-of-binary-tree) |
104106
| [0684-redundant-connection](https://github.com/TheVinaySagar/Leetcode/tree/master/0684-redundant-connection) |
105107
| [0854-making-a-large-island](https://github.com/TheVinaySagar/Leetcode/tree/master/0854-making-a-large-island) |
@@ -190,6 +192,7 @@ this is readme. here i will make
190192
## Graph
191193
| |
192194
| ------- |
195+
| [0547-number-of-provinces](https://github.com/TheVinaySagar/Leetcode/tree/master/0547-number-of-provinces) |
193196
| [0684-redundant-connection](https://github.com/TheVinaySagar/Leetcode/tree/master/0684-redundant-connection) |
194197
| [1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid](https://github.com/TheVinaySagar/Leetcode/tree/master/1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid) |
195198
| [1558-course-schedule-iv](https://github.com/TheVinaySagar/Leetcode/tree/master/1558-course-schedule-iv) |
@@ -219,6 +222,7 @@ this is readme. here i will make
219222
## Union Find
220223
| |
221224
| ------- |
225+
| [0547-number-of-provinces](https://github.com/TheVinaySagar/Leetcode/tree/master/0547-number-of-provinces) |
222226
| [0684-redundant-connection](https://github.com/TheVinaySagar/Leetcode/tree/master/0684-redundant-connection) |
223227
| [0854-making-a-large-island](https://github.com/TheVinaySagar/Leetcode/tree/master/0854-making-a-large-island) |
224228
| [1396-count-servers-that-communicate](https://github.com/TheVinaySagar/Leetcode/tree/master/1396-count-servers-that-communicate) |

stats.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"leetcode":{"shas":{"README.md":{"":"1e4fca8f62a61e6c56789b55310b9370d214fd33"},"2465-shifting-letters-ii":{"sha":"d185b9a482db6f49393149aabed8a625bb6c4d4b","difficulty":"medium"},"1895-minimum-number-of-operations-to-move-all-balls-to-each-box":{"sha":"8cf49a54282d1cc0785f31978f229b45fe17ed37","difficulty":"medium"},"1524-string-matching-in-an-array":{"sha":"1e81100d7eb5724cd55903ee2d9d62124949a6a4","difficulty":"easy"},"1029-vertical-order-traversal-of-a-binary-tree":{"sha":"233be19d0cea32f5988fad6d651954cba5c9ed45","difficulty":"hard"},"0199-binary-tree-right-side-view":{"sha":"3217ef07997f85fa8d961bc4b52603ea40379e9b","difficulty":"medium"},"3309-count-prefix-and-suffix-pairs-i":{"sha":"6f1dcc761a6930bc80b7079393535bee8875e8ed","difficulty":"easy"},"0101-symmetric-tree":{"sha":"71468a287e3dc86a1a7d1e30b92c1d32f4b8fb82","difficulty":"easy"},"0121-best-time-to-buy-and-sell-stock":{"sha":"28885475707cf6030f96ff8c6b4f34f70cae2bc8","difficulty":"easy"},"2292-counting-words-with-a-given-prefix":{"sha":"e212e615d1226f7aa1fe914321a6de10cad22ec2","difficulty":"easy"},"0236-lowest-common-ancestor-of-a-binary-tree":{"sha":"0c991325621d187084183637a371159a21446339","difficulty":"medium"},"0662-maximum-width-of-binary-tree":{"sha":"110c4ddfaa298c100c10bc2fe492f28c05cc2ee7","difficulty":"medium"},"0952-word-subsets":{"sha":"a29373661158716bb8b0ccf00e3158ba370fc293","difficulty":"medium"},"0893-all-nodes-distance-k-in-binary-tree":{"sha":"258c574ced99a128a788c3eaa0cd159c20ab3e93","difficulty":"medium"},"0222-count-complete-tree-nodes":{"sha":"6c1ff1fde6115833071a176f1ad405866db12436","difficulty":"easy"},"1502-construct-k-palindrome-strings":{"sha":"f54a64510560bfd162fa61d6793cae9cc16b372d","difficulty":"medium"},"2221-check-if-a-parentheses-string-can-be-valid":{"sha":"86ca2c40bfac3acc2e4c98f7f7620cef96e24717","difficulty":"medium"},"3455-minimum-length-of-string-after-operations":{"sha":"3d63cb0e94ee6ee5d8bde444db227e80b397a703","difficulty":"medium"},"2766-find-the-prefix-common-array-of-two-arrays":{"sha":"1f536c0b4f2a0bddd73ebf3c8a02372411662889","difficulty":"medium"},"2509-minimize-xor":{"sha":"859be97d5d5eb05646d28e37d1f57f9bee76bc38","difficulty":"medium"},"0106-construct-binary-tree-from-inorder-and-postorder-traversal":{"sha":"b251746ba3488013aaa6db4afea2f6175f0f1a56","difficulty":"medium"},"0297-serialize-and-deserialize-binary-tree":{"sha":"f31632db19b9ca20446158b4b73b5260eff612a0","difficulty":"hard"},"2792-neighboring-bitwise-xor":{"sha":"0c7f84b7ab7903d479efae54727a290528e52e3a","difficulty":"medium"},"1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid":{"sha":"7871ed4476bc954d2e44ce5efb8940b5040345ab","difficulty":"hard"},"0407-trapping-rain-water-ii":{"sha":"435cb721c56f2db44c6b235bc5304182db10371b","difficulty":"hard"},"2685-first-completely-painted-row-or-column":{"sha":"003bfba52059b42334a974f8926c961dde330c83","difficulty":"medium"},"2145-grid-game":{"sha":"10ceaba68f9e680b9c110702bce7f10038991b28","difficulty":"medium"},"1876-map-of-highest-peak":{"sha":"4ae04ec1ad12335b5ad550fd6249cd57c8610633","difficulty":"medium"},"1396-count-servers-that-communicate":{"sha":"92c1e705b7495e8ee7e1fd6027d0b9ba2aa8122f","difficulty":"medium"},"0783-search-in-a-binary-search-tree":{"sha":"d2ae96c0c9681043ea392f7b81b58ec115bdd113","difficulty":"easy"},"0784-insert-into-a-binary-search-tree":{"sha":"","difficulty":"medium"},"1558-course-schedule-iv":{"sha":"badda8021673e3e997225736414be21623410e12","difficulty":"medium"},"3439-find-minimum-diameter-after-merging-two-trees":{"sha":"eaf85fa397a1b5b21272a1c7a4dfc2a19cf30304","difficulty":"hard"},"2764-maximum-number-of-fish-in-a-grid":{"sha":"d00f05e94cc685b98a97cbb577435e2cc15e2092","difficulty":"medium"},"0684-redundant-connection":{"sha":"880ddaa2e6c6d7fd457efdfd3e5a1c962ab9b667","difficulty":"medium"},"2583-divide-nodes-into-the-maximum-number-of-groups":{"sha":"24c405e0bdc8200a64b90001e9dc5f4fb5ee5f44","difficulty":"hard"},"1036-rotting-oranges":{"sha":"0d30a8ed29926c3799eef6d17c065d0b7e94b902","difficulty":"medium"},"0854-making-a-large-island":{"sha":"67c0ad3a35f375cb21edb0fb78cacf5a278ec244","difficulty":"hard"},"3429-special-array-i":{"sha":"","difficulty":"easy"}},"solved":38,"easy":8,"medium":23,"hard":7}}
1+
{"leetcode":{"shas":{"README.md":{"":"c003e4f1f2b3a1d55cb088c833d7104fdf20df39"},"2465-shifting-letters-ii":{"sha":"d185b9a482db6f49393149aabed8a625bb6c4d4b","difficulty":"medium"},"1895-minimum-number-of-operations-to-move-all-balls-to-each-box":{"sha":"8cf49a54282d1cc0785f31978f229b45fe17ed37","difficulty":"medium"},"1524-string-matching-in-an-array":{"sha":"1e81100d7eb5724cd55903ee2d9d62124949a6a4","difficulty":"easy"},"1029-vertical-order-traversal-of-a-binary-tree":{"sha":"233be19d0cea32f5988fad6d651954cba5c9ed45","difficulty":"hard"},"0199-binary-tree-right-side-view":{"sha":"3217ef07997f85fa8d961bc4b52603ea40379e9b","difficulty":"medium"},"3309-count-prefix-and-suffix-pairs-i":{"sha":"6f1dcc761a6930bc80b7079393535bee8875e8ed","difficulty":"easy"},"0101-symmetric-tree":{"sha":"71468a287e3dc86a1a7d1e30b92c1d32f4b8fb82","difficulty":"easy"},"0121-best-time-to-buy-and-sell-stock":{"sha":"28885475707cf6030f96ff8c6b4f34f70cae2bc8","difficulty":"easy"},"2292-counting-words-with-a-given-prefix":{"sha":"e212e615d1226f7aa1fe914321a6de10cad22ec2","difficulty":"easy"},"0236-lowest-common-ancestor-of-a-binary-tree":{"sha":"0c991325621d187084183637a371159a21446339","difficulty":"medium"},"0662-maximum-width-of-binary-tree":{"sha":"110c4ddfaa298c100c10bc2fe492f28c05cc2ee7","difficulty":"medium"},"0952-word-subsets":{"sha":"a29373661158716bb8b0ccf00e3158ba370fc293","difficulty":"medium"},"0893-all-nodes-distance-k-in-binary-tree":{"sha":"258c574ced99a128a788c3eaa0cd159c20ab3e93","difficulty":"medium"},"0222-count-complete-tree-nodes":{"sha":"6c1ff1fde6115833071a176f1ad405866db12436","difficulty":"easy"},"1502-construct-k-palindrome-strings":{"sha":"f54a64510560bfd162fa61d6793cae9cc16b372d","difficulty":"medium"},"2221-check-if-a-parentheses-string-can-be-valid":{"sha":"86ca2c40bfac3acc2e4c98f7f7620cef96e24717","difficulty":"medium"},"3455-minimum-length-of-string-after-operations":{"sha":"3d63cb0e94ee6ee5d8bde444db227e80b397a703","difficulty":"medium"},"2766-find-the-prefix-common-array-of-two-arrays":{"sha":"1f536c0b4f2a0bddd73ebf3c8a02372411662889","difficulty":"medium"},"2509-minimize-xor":{"sha":"859be97d5d5eb05646d28e37d1f57f9bee76bc38","difficulty":"medium"},"0106-construct-binary-tree-from-inorder-and-postorder-traversal":{"sha":"b251746ba3488013aaa6db4afea2f6175f0f1a56","difficulty":"medium"},"0297-serialize-and-deserialize-binary-tree":{"sha":"f31632db19b9ca20446158b4b73b5260eff612a0","difficulty":"hard"},"2792-neighboring-bitwise-xor":{"sha":"0c7f84b7ab7903d479efae54727a290528e52e3a","difficulty":"medium"},"1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid":{"sha":"7871ed4476bc954d2e44ce5efb8940b5040345ab","difficulty":"hard"},"0407-trapping-rain-water-ii":{"sha":"435cb721c56f2db44c6b235bc5304182db10371b","difficulty":"hard"},"2685-first-completely-painted-row-or-column":{"sha":"003bfba52059b42334a974f8926c961dde330c83","difficulty":"medium"},"2145-grid-game":{"sha":"10ceaba68f9e680b9c110702bce7f10038991b28","difficulty":"medium"},"1876-map-of-highest-peak":{"sha":"4ae04ec1ad12335b5ad550fd6249cd57c8610633","difficulty":"medium"},"1396-count-servers-that-communicate":{"sha":"92c1e705b7495e8ee7e1fd6027d0b9ba2aa8122f","difficulty":"medium"},"0783-search-in-a-binary-search-tree":{"sha":"d2ae96c0c9681043ea392f7b81b58ec115bdd113","difficulty":"easy"},"0784-insert-into-a-binary-search-tree":{"sha":"","difficulty":"medium"},"1558-course-schedule-iv":{"sha":"badda8021673e3e997225736414be21623410e12","difficulty":"medium"},"3439-find-minimum-diameter-after-merging-two-trees":{"sha":"eaf85fa397a1b5b21272a1c7a4dfc2a19cf30304","difficulty":"hard"},"2764-maximum-number-of-fish-in-a-grid":{"sha":"d00f05e94cc685b98a97cbb577435e2cc15e2092","difficulty":"medium"},"0684-redundant-connection":{"sha":"880ddaa2e6c6d7fd457efdfd3e5a1c962ab9b667","difficulty":"medium"},"2583-divide-nodes-into-the-maximum-number-of-groups":{"sha":"24c405e0bdc8200a64b90001e9dc5f4fb5ee5f44","difficulty":"hard"},"1036-rotting-oranges":{"sha":"0d30a8ed29926c3799eef6d17c065d0b7e94b902","difficulty":"medium"},"0854-making-a-large-island":{"sha":"67c0ad3a35f375cb21edb0fb78cacf5a278ec244","difficulty":"hard"},"3429-special-array-i":{"sha":"ef6cee63d0af1b6f4b4171c2297be6337909168b","difficulty":"easy"},"0547-number-of-provinces":{"sha":"","difficulty":"medium"}},"solved":39,"easy":8,"medium":24,"hard":7}}

0 commit comments

Comments
 (0)