From e2eb0bb16f83cb68f59e831dd1d0a5af439c5984 Mon Sep 17 00:00:00 2001 From: samps Date: Sat, 1 Jun 2024 16:33:50 -0600 Subject: [PATCH 1/2] Create: 0130-surrounded-regions.dart --- dart/0130-surrounded-regions.dart | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 dart/0130-surrounded-regions.dart diff --git a/dart/0130-surrounded-regions.dart b/dart/0130-surrounded-regions.dart new file mode 100644 index 000000000..f16f439a9 --- /dev/null +++ b/dart/0130-surrounded-regions.dart @@ -0,0 +1,51 @@ +class Solution { + late List> board; + late int rows; + late int columns; + + bool isInBound(int row, int column) { + return 0 <= row && row < rows && 0 <= column && column < columns; + } + + void dfs(int row, int column) { + if (!isInBound(row, column) || board[row][column] != 'O') { + return; + } + + board[row][column] = 'T'; + + dfs(row + 1, column); + dfs(row, column + 1); + dfs(row - 1, column); + dfs(row, column - 1); + } + + void solve(List> board) { + this.board = board; + rows = board.length; + columns = board[0].length; + + // Traverse the first and last columns + for (int column = 0; column < columns; column++) { + dfs(0, column); + dfs(rows - 1, column); + } + + // Traverse the first and last rows + for (int row = 0; row < rows; row++) { + dfs(row, 0); + dfs(row, columns - 1); + } + + // Replace all 'O' with 'X' and 'T' back to 'O' + for (int row = 0; row < rows; row++) { + for (int column = 0; column < columns; column++) { + if (board[row][column] == 'O') { + board[row][column] = 'X'; + } else if (board[row][column] == 'T') { + board[row][column] = 'O'; + } + } + } + } +} \ No newline at end of file From d9147b6af4de2a6d5d3d50de413c2a74f46bfb33 Mon Sep 17 00:00:00 2001 From: samps Date: Sun, 2 Jun 2024 11:49:30 -0600 Subject: [PATCH 2/2] Create: 0127-word-ladder.dart --- dart/0127-word-ladder.dart | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 dart/0127-word-ladder.dart diff --git a/dart/0127-word-ladder.dart b/dart/0127-word-ladder.dart new file mode 100644 index 000000000..5d641cad7 --- /dev/null +++ b/dart/0127-word-ladder.dart @@ -0,0 +1,48 @@ +class Solution { + List findRedundantConnection(List> edges) { + List parent = []; + for (int i = 0; i < edges.length + 1; i++) { + parent.add(i); + } + + List sizes = List.filled(edges.length + 1, 1); + + int find(int n) { + int p = parent[n]; + + while (p != parent[p]) { + parent[p] = parent[parent[p]]; + p = parent[p]; + } + + return p; + } + + bool union(int node1, int node2) { + int parentOf1 = find(node1); + int parentOf2 = find(node2); + + if (parentOf1 == parentOf2) { + return false; + } else if (sizes[parentOf1] > sizes[parentOf2]) { + parent[parentOf2] = parentOf1; + sizes[parentOf1] += sizes[parentOf2]; + } else { + parent[parentOf1] = parentOf2; + sizes[parentOf2] += sizes[parentOf1]; + } + + return true; + } + + for (List edge in edges) { + int node1 = edge[0]; + int node2 = edge[1]; + if (!union(node1, node2)) { + return [node1, node2]; + } + } + + return []; + } +} \ No newline at end of file