Skip to content

Commit a05312e

Browse files
Problem 547 (#30)
* add solution and resources * update readme
1 parent 8bd4b64 commit a05312e

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ In order to practice with similar data structures I'll be placing each problem i
158158
### Graph
159159

160160
- [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)
161+
- [Number of Provinces #547](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-provinces-547.md)
161162

162163
### Heap
163164

@@ -193,6 +194,7 @@ Within the problems above there are several patterns that often occur. I plan to
193194
- [01 Matrix #542](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/01-matrix-542.md)
194195
- [Binary Tree Level Order Traversal #102](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/binary-tree-level-102.md)
195196
- [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)
197+
- [Number of Provinces #547](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-provinces-547.md)
196198

197199
### Depth First Search
198200

@@ -203,6 +205,7 @@ Within the problems above there are several patterns that often occur. I plan to
203205
- [Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)
204206
- [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
205207
- [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)
208+
- [Number of Provinces #547](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-provinces-547.md)
206209

207210
### Divide & Conquer
208211

medium/number-of-provinces-547.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Number of Provinces
2+
3+
Page on leetcode: https://leetcode.com/problems/number-of-provinces/
4+
5+
## Problem Statement
6+
7+
There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c.
8+
9+
A province is a group of directly or indirectly connected cities and no other cities outside of the group.
10+
11+
You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the ith city and the jth city are directly connected, and isConnected[i][j] = 0 otherwise.
12+
13+
Return the total number of provinces.
14+
15+
### Constraints
16+
17+
- 1 <= n <= 200
18+
- n == isConnected.length
19+
- n == isConnected[i].length
20+
- isConnected[i][j] is 1 or 0.
21+
- isConnected[i][i] == 1
22+
- isConnected[i][j] == isConnected[j][i]
23+
24+
### Example
25+
26+
```
27+
Input: isConnected = [[1,1,0],[1,1,0],[0,0,1]]
28+
Output: 2
29+
```
30+
31+
## Solution
32+
33+
### Initial Thoughts
34+
35+
- iterate thru arrays
36+
- Have a counter, when you find a break add to counter
37+
- minimum province is 1
38+
- Can I check from i = 0 to i = n/2
39+
40+
### Optimized Solution
41+
42+
Time and space complexity for this solution would be O(n). This is a recursive DFS solution however it can also be solved with BFS and using a Union Find.
43+
You can see a good solution to this problem here: https://www.youtube.com/watch?v=S5UUvCTM0V4
44+
45+
```javascript
46+
const findCircleNum = function (isConnected) {
47+
const visited = new Set();
48+
let numOfProv = 0;
49+
50+
// Iterate thru matrix
51+
for (let i = 0; i < isConnected.length; i++) {
52+
if (!visited.has(i)) {
53+
dfs(i);
54+
numOfProv++;
55+
}
56+
}
57+
58+
return numOfProv;
59+
60+
// Helper function for DFS search
61+
function dfs(i) {
62+
for (let j = 0; j < isConnected.length; j++) {
63+
if (isConnected[i][j] === 1 && !visited.has(j)) {
64+
visited.add(j);
65+
dfs(j);
66+
}
67+
}
68+
}
69+
};
70+
```

0 commit comments

Comments
 (0)