From 1b2e7bfef710c56a102416ad4ae16c05d9014976 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sat, 14 Sep 2024 15:57:02 +0800 Subject: [PATCH] 204th Commit --- README.md | 3 +- .../findCircleNum.test.ts | 21 +++++++++++ .../547. Number of Provinces/findCircleNum.ts | 35 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/page-6/547. Number of Provinces/findCircleNum.test.ts create mode 100644 src/page-6/547. Number of Provinces/findCircleNum.ts diff --git a/README.md b/README.md index 9569b9d..8acfc9c 100644 --- a/README.md +++ b/README.md @@ -204,11 +204,12 @@ Ace Coding Interview with 75 Qs | Graphs - DFS | | | | ------------------------------------------------------------ | --------------- | ------ | | 841. Keys and Rooms | [Solution][841] | Medium | -| 547. Number of Provinces | Solution | Medium | +| 547. Number of Provinces | [Solution][547] | Medium | | 1466. Reorder Routes to Make All Paths Lead to the City Zero | Solution | Medium | | 399. Evaluate Division | Solution | Medium | [841]: ./src/page-8/841.%20Keys%20and%20Rooms/canVisitAllRooms.ts +[547]: ./src/page-6/547.%20Number%20of%20Provinces/findCircleNum.ts | Graphs - BFS | | | | ---------------------------------------- | -------- | ------ | diff --git a/src/page-6/547. Number of Provinces/findCircleNum.test.ts b/src/page-6/547. Number of Provinces/findCircleNum.test.ts new file mode 100644 index 0000000..a6c0b88 --- /dev/null +++ b/src/page-6/547. Number of Provinces/findCircleNum.test.ts @@ -0,0 +1,21 @@ +import { findCircleNum } from './findCircleNum'; + +describe('547. Number of Provinces', () => { + test('findCircleNum', () => { + expect( + findCircleNum([ + [1, 1, 0], + [1, 1, 0], + [0, 0, 1], + ]), + ).toBe(2); + + expect( + findCircleNum([ + [1, 0, 0], + [0, 1, 0], + [0, 0, 1], + ]), + ).toBe(3); + }); +}); diff --git a/src/page-6/547. Number of Provinces/findCircleNum.ts b/src/page-6/547. Number of Provinces/findCircleNum.ts new file mode 100644 index 0000000..c540003 --- /dev/null +++ b/src/page-6/547. Number of Provinces/findCircleNum.ts @@ -0,0 +1,35 @@ +type FindCircleNum = (isConnected: number[][]) => number; + +/** + * Accepted + */ +export const findCircleNum: FindCircleNum = (isConnected) => { + const n = isConnected.length; // Number of cities (or nodes in the graph) + const visited = Array(n).fill(false); // Array to keep track of visited cities + + // DFS function to explore all cities in the current province + const dfs = (i: number) => { + // Traverse all cities + for (let j = 0; j < n; j++) { + // If city 'i' is directly connected to city 'j' and 'j' has not been visited yet + if (isConnected[i][j] === 1 && !visited[j]) { + visited[j] = true; // Mark city 'j' as visited + dfs(j); // Recursively visit all cities connected to city 'j' + } + } + }; + + let provinces = 0; // Variable to count the number of provinces + + // Loop through each city + for (let i = 0; i < n; i++) { + // If the city hasn't been visited yet, it's a new province + if (!visited[i]) { + provinces += 1; // Increment the province count + visited[i] = true; // Mark the current city as visited + dfs(i); // Perform DFS to visit all cities in this province + } + } + + return provinces; // Return the total number of provinces found +};