Skip to content

Problem 547 #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ In order to practice with similar data structures I'll be placing each problem i
### Graph

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

### Heap

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

### Depth First Search

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

### Divide & Conquer

Expand Down
70 changes: 70 additions & 0 deletions medium/number-of-provinces-547.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Number of Provinces

Page on leetcode: https://leetcode.com/problems/number-of-provinces/

## Problem Statement

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.

A province is a group of directly or indirectly connected cities and no other cities outside of the group.

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.

Return the total number of provinces.

### Constraints

- 1 <= n <= 200
- n == isConnected.length
- n == isConnected[i].length
- isConnected[i][j] is 1 or 0.
- isConnected[i][i] == 1
- isConnected[i][j] == isConnected[j][i]

### Example

```
Input: isConnected = [[1,1,0],[1,1,0],[0,0,1]]
Output: 2
```

## Solution

### Initial Thoughts

- iterate thru arrays
- Have a counter, when you find a break add to counter
- minimum province is 1
- Can I check from i = 0 to i = n/2

### Optimized Solution

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.
You can see a good solution to this problem here: https://www.youtube.com/watch?v=S5UUvCTM0V4

```javascript
const findCircleNum = function (isConnected) {
const visited = new Set();
let numOfProv = 0;

// Iterate thru matrix
for (let i = 0; i < isConnected.length; i++) {
if (!visited.has(i)) {
dfs(i);
numOfProv++;
}
}

return numOfProv;

// Helper function for DFS search
function dfs(i) {
for (let j = 0; j < isConnected.length; j++) {
if (isConnected[i][j] === 1 && !visited.has(j)) {
visited.add(j);
dfs(j);
}
}
}
};
```