From e492c9980c9390f1f81de768b5b29d365c4fecc5 Mon Sep 17 00:00:00 2001 From: suhwan2004 Date: Sat, 14 Dec 2024 11:29:18 +0900 Subject: [PATCH 1/5] Shortest Bridge --- suhwan2004/Shortest Bridge.js | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 suhwan2004/Shortest Bridge.js diff --git a/suhwan2004/Shortest Bridge.js b/suhwan2004/Shortest Bridge.js new file mode 100644 index 0000000..e6d1887 --- /dev/null +++ b/suhwan2004/Shortest Bridge.js @@ -0,0 +1,59 @@ +/* +N === grid.length +Time : O(N^3), Space : O(N^2) +ALGO : DFS, for +DS : Array +Constraints + - 2 <= grid.length <= 100 + - grid[i][j] is either 0 or 1 +*/ + +var shortestBridge = function (grid) { + let arr = []; + let arr2 = []; + for (let i = 0; i < grid.length; i++) { + for (let j = 0; j < grid[i].length; j++) { + if (grid[i][j] == 1) { + if (arr.length == 0) { + grid[i][j] = 2; + dfs(grid, i, j, arr); + } else { + grid[i][j] = 2; + dfs(grid, i, j, arr2); + } + } + } + } + + let val = 0; + let min = Infinity; + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr2.length; j++) { + val = + Math.abs(arr2[j][0] - arr[i][0]) + Math.abs(arr2[j][1] - arr[i][1]) - 1; + if (min > val) min = val; + } + } + return min; +}; + +function dfs(grid, i, j, arr) { + arr.push([i, j]); + if (i > 0 && grid[i - 1][j] == 1) { + grid[i - 1][j] = 2; + dfs(grid, i - 1, j, arr); + } + if (i < grid.length - 1 && grid[i + 1][j] == 1) { + grid[i + 1][j] = 2; + dfs(grid, i + 1, j, arr); + } + if (j > 0 && grid[i][j - 1] == 1) { + grid[i][j - 1] = 2; + dfs(grid, i, j - 1, arr); + } + if (j < grid[i].length - 1 && grid[i][j + 1] == 1) { + grid[i][j + 1] = 2; + dfs(grid, i, j + 1, arr); + } + return; +} From e5162d2ee54fe0dcc1d3e309a0305592ee6ad429 Mon Sep 17 00:00:00 2001 From: suhwan2004 Date: Sat, 14 Dec 2024 12:59:53 +0900 Subject: [PATCH 2/5] =?UTF-8?q?readme=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c6b5802..8a85881 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,14 @@ ## ๐Ÿ“œ ์Šคํ„ฐ๋”” ๊ทœ์น™ 1. **์ฃผ 3ํšŒ ๋ฌธ์ œ ํ’€๊ธฐ** + - ์Šคํ„ฐ๋””์› ์ค‘ ํ•œ๋ช…์ด 3๊ฐœ์˜ ๋ฌธ์ œ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. - ๋‚˜๋จธ์ง€ ์Šคํ„ฐ๋””์›๋“ค์€ ์ง€์ •ํ•œ 3๊ฐœ์˜ ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•ฉ๋‹ˆ๋‹ค. - ๋ฌธ์ œ ํ’€์ด ์ฝ”๋“œ๋ฅผ ๊ฐ์ž์˜ GitHub ๋ธŒ๋žœ์น˜(`๊นƒํ—ˆ๋ธŒ์•„์ด๋””/๋ฌธ์ œ-์ด๋ฆ„`)์— ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. - PR(Pull Request)์„ ํ†ตํ•ด ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 2. **์ฃผ๊ฐ„ ๋ฌธ์ œ ํ’€์ด ๋ฆฌ๋ทฐ** - - **์ผ์‹œ**: ๋งค์ฃผ ํ† ์š”์ผ ์˜ค์ „ 10์‹œ 30๋ถ„ + - **์ผ์‹œ**: ๋งค์ฃผ ํ† ์š”์ผ ์˜ค์ „ 11์‹œ - **์ง„ํ–‰ ๋ฐฉ์‹**: ๋น„๋Œ€๋ฉด (์˜จ๋ผ์ธ) - **๋ฌธ์ œ ํ’€์ด ๋ฐœํ‘œ์ž**: ๋งค์ฃผ ๋žœ๋ค์œผ๋กœ ์„ ์ • From 3bc8d835a6d8f26f508992b0633f3f9d8a0073fb Mon Sep 17 00:00:00 2001 From: suhwan2004 Date: Sun, 15 Dec 2024 23:13:17 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=A0=95=EB=8F=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- suhwan2004/Shortest Bridge.js | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/suhwan2004/Shortest Bridge.js b/suhwan2004/Shortest Bridge.js index e6d1887..5b62c22 100644 --- a/suhwan2004/Shortest Bridge.js +++ b/suhwan2004/Shortest Bridge.js @@ -8,17 +8,19 @@ Constraints - grid[i][j] is either 0 or 1 */ +const LAND = 1 +const VISITED_LAND = 2 var shortestBridge = function (grid) { let arr = []; let arr2 = []; for (let i = 0; i < grid.length; i++) { for (let j = 0; j < grid[i].length; j++) { - if (grid[i][j] == 1) { + if (grid[i][j] == LAND) { if (arr.length == 0) { - grid[i][j] = 2; + grid[i][j] = VISITED_LAND; dfs(grid, i, j, arr); } else { - grid[i][j] = 2; + grid[i][j] = VISITED_LAND; dfs(grid, i, j, arr2); } } @@ -31,7 +33,7 @@ var shortestBridge = function (grid) { for (let j = 0; j < arr2.length; j++) { val = Math.abs(arr2[j][0] - arr[i][0]) + Math.abs(arr2[j][1] - arr[i][1]) - 1; - if (min > val) min = val; + min = Math.min(min, val) } } return min; @@ -39,21 +41,14 @@ var shortestBridge = function (grid) { function dfs(grid, i, j, arr) { arr.push([i, j]); - if (i > 0 && grid[i - 1][j] == 1) { - grid[i - 1][j] = 2; - dfs(grid, i - 1, j, arr); - } - if (i < grid.length - 1 && grid[i + 1][j] == 1) { - grid[i + 1][j] = 2; - dfs(grid, i + 1, j, arr); - } - if (j > 0 && grid[i][j - 1] == 1) { - grid[i][j - 1] = 2; - dfs(grid, i, j - 1, arr); - } - if (j < grid[i].length - 1 && grid[i][j + 1] == 1) { - grid[i][j + 1] = 2; - dfs(grid, i, j + 1, arr); + const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]; + + for(let direction of directions){ + const newI = direction[0] + i; + const newJ = direction[1] + j; + if(newI >= 0 && newI < grid.length && newJ >= 0 && newJ < grid[0].length && grid[newI][newJ] === LAND){ + grid[newI][newJ] = VISITED_LAND; + dfs(grid, newI, newJ, arr) + } } - return; } From a7aa46b6c19e0a6963eb5aa10ac8bfa20d9b55bf Mon Sep 17 00:00:00 2001 From: suhwan2004 Date: Sun, 15 Dec 2024 23:17:44 +0900 Subject: [PATCH 4/5] =?UTF-8?q?pr=20template=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/pull_request_template.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 1c6fcd9..31db248 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,10 +1,13 @@ ## ๋ฌธ์ œ -| Type | Info | -| :----------- | :--- | -| **Platform** | | -| **Level** | | -| **Link** | | +| Type | Info | +| :------------------- | :--- | +| **Time Complexity** | | +| **Space Complexity** | | +| **Algorithm** | | +| **Data Structure** | | +| **Constraints** | | +| **Edge Case** | | ## ํ’€์ด From 7bc6cb9c3b75f52326418e1dc69bc8ae1e62eb80 Mon Sep 17 00:00:00 2001 From: suhwan2004 Date: Mon, 16 Dec 2024 20:02:20 +0900 Subject: [PATCH 5/5] =?UTF-8?q?pr=20template=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/pull_request_template.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 31db248..5116b8f 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -6,8 +6,10 @@ | **Space Complexity** | | | **Algorithm** | | | **Data Structure** | | -| **Constraints** | | -| **Edge Case** | | + +## Constraints + +## Edge Case ## ํ’€์ด