|
| 1 | +<h2><a href="https://leetcode.com/problems/number-of-provinces/">547. Number of Provinces</a></h2><h3>Medium</h3><hr><div><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> </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><strong>Input:</strong> isConnected = [[1,1,0],[1,1,0],[0,0,1]] |
| 13 | +<strong>Output:</strong> 2 |
| 14 | +</pre> |
| 15 | + |
| 16 | +<p><strong class="example">Example 2:</strong></p> |
| 17 | +<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg" style="width: 222px; height: 142px;"> |
| 18 | +<pre><strong>Input:</strong> isConnected = [[1,0,0],[0,1,0],[0,0,1]] |
| 19 | +<strong>Output:</strong> 3 |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p> </p> |
| 23 | +<p><strong>Constraints:</strong></p> |
| 24 | + |
| 25 | +<ul> |
| 26 | + <li><code>1 <= n <= 200</code></li> |
| 27 | + <li><code>n == isConnected.length</code></li> |
| 28 | + <li><code>n == isConnected[i].length</code></li> |
| 29 | + <li><code>isConnected[i][j]</code> is <code>1</code> or <code>0</code>.</li> |
| 30 | + <li><code>isConnected[i][i] == 1</code></li> |
| 31 | + <li><code>isConnected[i][j] == isConnected[j][i]</code></li> |
| 32 | +</ul> |
| 33 | +</div> |
0 commit comments