[ICE0208] WEEK 07 Solutions - #2798
Conversation
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
number-of-islands/ICE0208.java
import java.util.ArrayDeque;
import java.util.Deque;
class Solution {
private static final int[][] DIRECTIONS = {
{0, 1},
{0, -1},
{1, 0},
{-1, 0}
};
private static final char WATER = '0';
private static final char LAND = '1';
private record Position(int row, int column) {
}
/**
* ๊ฒฉ์๋ฅผ ์ํํ๋ฉฐ ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ์ก์ง๋ฅผ ๋ฐ๊ฒฌํ ๋๋ง๋ค
* ์ฐ๊ฒฐ๋ ํ๋์ ์ฌ์ ๋ฐ๋ณตํ DFS๋ก ๋ชจ๋ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌํ๋ค.
*
* ์๊ฐ ๋ณต์ก๋: O(m * n)
* ๊ณต๊ฐ ๋ณต์ก๋: O(m * n)
*/
public int numIslands(char[][] grid) {
int islandCount = 0;
for (int row = 0; row < grid.length; row++) {
for (int column = 0; column < grid[row].length; column++) {
if (grid[row][column] != LAND) {
continue;
}
// ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ์ก์ง๋ ์๋ก์ด ์ฌ์ ์์์ ์ด๋ค.
islandCount++;
markIslandAsVisited(grid, row, column);
}
}
return islandCount;
}
/**
* ์์ ์์น์ ์ํ์ข์ฐ๋ก ์ฐ๊ฒฐ๋ ๋ชจ๋ ์ก์ง๋ฅผ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌํ๋ค.
* ์ฌ๊ท ํธ์ถ๋ก ์ธํ ์คํ ์ค๋ฒํ๋ก๋ฅผ ํผํ๊ธฐ ์ํด ๋ณ๋์ ์คํ์ ์ฌ์ฉํ๋ค.
*/
private static void markIslandAsVisited(
char[][] grid,
int startRow,
int startColumn
) {
Deque<Position> stack = new ArrayDeque<>();
stack.push(new Position(startRow, startColumn));
// ์คํ์ ๋ฃ๋ ์์ ์ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌํ์ฌ ๊ฐ์ ์์น๊ฐ ์ค๋ณต์ผ๋ก ๋ค์ด๊ฐ๋ ๊ฒ์ ๋ฐฉ์งํ๋ค.
grid[startRow][startColumn] = WATER;
while (!stack.isEmpty()) {
Position current = stack.pop();
for (int[] direction : DIRECTIONS) {
int nextRow = current.row() + direction[0];
int nextColumn = current.column() + direction[1];
if (!isInBounds(grid, nextRow, nextColumn)) {
continue;
}
if (grid[nextRow][nextColumn] != LAND) {
continue;
}
grid[nextRow][nextColumn] = WATER;
stack.push(new Position(nextRow, nextColumn));
}
}
}
private static boolean isInBounds(
char[][] grid,
int row,
int column
) {
return row >= 0
&& row < grid.length
&& column >= 0
&& column < grid[0].length;
}
}- ํจํด: Depth-First Search, Stack/Explicit Stack (as part of DFS)
- ์ค๋ช : ๊ทธ๋ฆฌ๋๋ฅผ ์ํํ๋ฉฐ ์ฌ(์ฐ๊ฒฐ๋ ์ก์ง)์ DFS๋ก ๋ฐฉ๋ฌธ ์ฒ๋ฆฌํ๋ค. ์ฌ๊ท ๋์ ์คํ์ ์ฌ์ฉํด DFS๋ฅผ ๊ตฌํํ๊ณ , ์ธ์ ํ ์ก์ง๋ฅผ ํ์ํ๋ฉฐ ๋ฐฉ๋ฌธ ํ์์ ๋จ๊ธด๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ์ ์ ๋ถ์ | ์ค์ ๋ถ์ | ๊ฒฐ๊ณผ | |
|---|---|---|---|
| Time | O(m * n) | O(n * m) | โ |
| Space | O(m * n) | O(n * m) | โ |
ํผ๋๋ฐฑ: ๋ ๋ฐฉํฅ์ผ๋ก์ ์ธ์ ๋ง ํ์ฅํ๋ DFS๋ก ์ ์ฒด ๊ฒฉ์๋ฅผ ํ ๋ฒ์ฉ ๋ฐฉ๋ฌธํ๊ณ , ๋ฐฉ๋ฌธ ์ ์ก์ง๋ฅผ WATER๋ก ํ์ํด ์ค๋ณต ๋ฐฉ๋ฌธ์ ๋ง๋๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ ICE0208 ๋์ ํ์ต ํํฉ์ด๋ฒ ์ฃผ ์ ์ถ ๋ฌธ์
๋์ ํ์ต ์์ฝ
๋ฌธ์ ํ์ด ํํฉ
๐ค ์ด ๋๊ธ์ GitHub App์ ํตํด ์๋์ผ๋ก ์์ฑ๋์์ต๋๋ค. ๐ข API ์ฌ์ฉ๋ (gpt-5-nano)
|
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
unique-paths/ICE0208.java
import java.util.Arrays;
class Solution {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
// ์ฒซ ๋ฒ์งธ ํ๊ณผ ์ด์ ํ ๋ฐฉํฅ์ผ๋ก๋ง ์ด๋ํด ๋๋ฌํ ์ ์์ผ๋ฏ๋ก 1๋ก ์ด๊ธฐํํฉ๋๋ค.
initializeBaseCases(dp);
for (int row = 1; row < m; ++row) {
for (int col = 1; col < n; ++col) {
dp[row][col] = dp[row - 1][col] + dp[row][col - 1];
}
}
return dp[m - 1][n - 1];
}
/**
* DP ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ํ๊ณผ ์ฒซ ๋ฒ์งธ ์ด์ 1๋ก ์ด๊ธฐํํฉ๋๋ค.
* @param dp ์ด๊ธฐํํ DP ๋ฐฐ์ด
*/
private static void initializeBaseCases(int[][] dp) {
int rows = dp.length;
int cols = dp[0].length;
for (int row = 0; row < rows; row++) {
dp[row][0] = 1;
}
for (int col = 0; col < cols; col++) {
dp[0][col] = 1;
}
}
}
class Solution2 {
public int uniquePaths(int m, int n) {
int[] dp = new int[n];
Arrays.fill(dp, 1);
for (int row = 1; row < m; ++row) {
for (int col = 1; col < n; ++col) {
dp[col] += dp[col - 1];
}
}
return dp[n - 1];
}
}- ํจํด: Dynamic Programming, Greedy
- ์ค๋ช : ์ฝ๋๋ ๋ ๊ฐ์ง ๋ฐฉ์์ผ๋ก ๊ฒฝ๋ก์ ์๋ฅผ DP๋ก ๊ณ์ฐํฉ๋๋ค. 2D ๋ฐฐ์ด๊ณผ 1D ๋ฐฐ์ด ๋ชจ๋์์ ํ์ฌ ์์น์ ๊ฒฝ๋ก ์๋ฅผ ์/์ข์ ํฉ์ผ๋ก ๊ฐฑ์ ํ๋ฏ๋ก DP ํจํด์ ์ํฉ๋๋ค. ์ฃผ์ด์ง ๋ฌธ์ ์ ํด๋ฅผ ์์ ๋ถ๋ถ ๋ฌธ์ ๋ก ๋๋์ด ํด๊ฒฐํ๋ ํน์ง์ด ๋ช ํํฉ๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
โน๏ธ ์ด ํ์ผ์๋ 2๊ฐ์ง ํ์ด๊ฐ ํฌํจ๋์ด ์์ด ๊ฐ๊ฐ ๋ถ์ํฉ๋๋ค.
ํ์ด 1: Solution.uniquePaths โ Time: O(m*n) / Space: O(m*n)
| ๋ณต์ก๋ | |
|---|---|
| Time | O(m*n) |
| Space | O(m*n) |
ํผ๋๋ฐฑ: 2์ฐจ์ DP ๋ฐฐ์ด์ ์ฌ์ฉํด ์ด์ ์ ์ ํฉ์ผ๋ก ํ์ฌ ์ ์ ๊ฒฝ๋ก ์๋ฅผ ๊ณ์ฐํฉ๋๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ ๋ช ํํ์ง๋ง ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ ์ค์ด๋ ค๋ฉด ๊ณต๊ฐ ์ต์ ํ ๋ฒ์ (1์ฐจ์ DP)์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ํ์ด 2: Solution2.uniquePaths โ Time: O(m*n) / Space: O(n)
| ๋ณต์ก๋ | |
|---|---|
| Time | O(m*n) |
| Space | O(n) |
ํผ๋๋ฐฑ: 1์ฐจ์ ๋ฐฐ์ด dp๋ฅผ ํตํด ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ์ค์๊ณ , ๊ฐ ํ๋ง๋ค ์ด์ ์ด์ ๊ฐ์ ์ด์ฉํด ํ์ฌ ๊ฐ์ ์ ๋ฐ์ดํธํ๋ค.
๊ฐ์ ์ ์: ์ถ๊ฐ์ ์ผ๋ก ์ปค์คํ ์ต์ ํ๋ ์ด๊ธฐํ ๋ก์ง์ ๊ฐ์ํํด๋ ์ข๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
unique-paths/ICE0208.java
import java.util.Arrays;
class Solution {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
// ์ฒซ ๋ฒ์งธ ํ๊ณผ ์ด์ ํ ๋ฐฉํฅ์ผ๋ก๋ง ์ด๋ํด ๋๋ฌํ ์ ์์ผ๋ฏ๋ก 1๋ก ์ด๊ธฐํํฉ๋๋ค.
initializeBaseCases(dp);
for (int row = 1; row < m; ++row) {
for (int col = 1; col < n; ++col) {
dp[row][col] = dp[row - 1][col] + dp[row][col - 1];
}
}
return dp[m - 1][n - 1];
}
/**
* DP ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ํ๊ณผ ์ฒซ ๋ฒ์งธ ์ด์ 1๋ก ์ด๊ธฐํํฉ๋๋ค.
* @param dp ์ด๊ธฐํํ DP ๋ฐฐ์ด
*/
private static void initializeBaseCases(int[][] dp) {
int rows = dp.length;
int cols = dp[0].length;
for (int row = 0; row < rows; row++) {
dp[row][0] = 1;
}
for (int col = 0; col < cols; col++) {
dp[0][col] = 1;
}
}
}
class Solution2 {
public int uniquePaths(int m, int n) {
int[] dp = new int[n];
Arrays.fill(dp, 1);
for (int row = 1; row < m; ++row) {
for (int col = 1; col < n; ++col) {
dp[col] += dp[col - 1];
}
}
return dp[n - 1];
}
}- ํจํด: Dynamic Programming, Monotonic Stack
- ์ค๋ช : ๋ ๊ฐ์ง ํ์ด ๋ชจ๋ DP๋ฅผ ์ด์ฉํ ๊ฒฝ๋ก ์ ๊ณ์ฐ ํจํด์ผ๋ก ๊ฐ ์ ์ ๊ฒฝ๋ก ์๋ฅผ ํฉ์ฐํฉ๋๋ค. ๋ํ ๋ ๋ฒ์งธ ํ์ด์์ 1์ฐจ์ DP ๋ฐฐ์ด๋ก ๊ณต๊ฐ ์ต์ ํํ๋ ์ผ๋ฐ์ DP ํจํด์ด ๋ณด์ ๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(m*n) |
| Space | O(m*n) |
ํผ๋๋ฐฑ: 2D ๋ฐฐ์ด ๋ฒ์ ์ ์ง๊ด์ ์ด๊ณ ์ดํดํ๊ธฐ ์ฝ๊ณ , 1D ๋ฐฐ์ด ๋ฒ์ ์ ๊ณต๊ฐ์ ์ ์ฝํฉ๋๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
set-matrix-zeroes/ICE0208.java
import java.util.Arrays;
class Solution {
public void setZeroes(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
// ์ฒซ ๋ฒ์งธ ํ๊ณผ ์ด์ marker๋ก ์ฌ์ฉํ๋ฏ๋ก, ์๋ 0์ด ์์๋์ง ๋ณ๋๋ก ์ ์ฅํ๋ค.
boolean firstRowHasZero = false;
for (int col = 0; col < cols; ++col) {
if (matrix[0][col] == 0) {
firstRowHasZero = true;
break;
}
}
boolean firstColumnHasZero = false;
for (int row = 0; row < rows; ++row) {
if (matrix[row][0] == 0) {
firstColumnHasZero = true;
break;
}
}
// ์ฒซ ๋ฒ์งธ ํ๊ณผ ์ด์ ๊ฐ ํ๊ณผ ์ด์ zero marker๋ก ์ฌ์ฉํ๋ค.
for (int row = 1; row < rows; ++row) {
for (int col = 1; col < cols; ++col) {
if (matrix[row][col] == 0) {
matrix[row][0] = 0;
matrix[0][col] = 0;
}
}
}
// marker๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ด๋ถ ์์๋ฅผ 0์ผ๋ก ๋ณ๊ฒฝํ๋ค.
for (int row = 1; row < rows; ++row) {
for (int col = 1; col < cols; ++col) {
if (matrix[row][0] == 0 || matrix[0][col] == 0) {
matrix[row][col] = 0;
}
}
}
if (firstRowHasZero) {
Arrays.fill(matrix[0], 0);
}
if (firstColumnHasZero) {
for (int row = 0; row < rows; ++row) {
matrix[row][0] = 0;
}
}
}
}- ํจํด: Greedy, Dynamic Programming, Divide and Conquer, Two Pointers, Hash Map / Hash Set, Binary Search, Monotonic Stack, Heap / Priority Queue, DFS, BFS, Backtracking, Union Find, Trie, Bit Manipulation, Sliding Window
- ์ค๋ช : ์ด ์ฝ๋๋ 2D ๋งคํธ๋ฆญ์ค์์ ํน์ ํ/์ด์ ๋ง์ปค๋ก ์ด์ฉํด ์ ๋ก๋ฅผ ํ์ฐ์ํค๋ ๋ฌธ์ ๋ก, ๊ณต๊ฐ์ ์ถ๊ฐ๋ก ์ฌ์ฉํ์ง ์๊ณ ๊ธฐ์กด ํ/์ด์ ๋ง์ปค๋ก ์ฌํ์ฉํ๋ ๋ฐฉ์์ด ํต์ฌ์ด๋ค. ์ ํ๋ ๊ณต๊ฐ์์ ์ํ๋ฅผ ์ ์ฅํ๊ณ ์กฐ๊ฑด์ ๋ฐ๋ผ ์์๋ฅผ ์ ๋ฐ์ดํธํ๋ ์์ด๋์ด๋ ๋ค์์ ๊ณต๊ฐ ์ต์ ํ ํจํด๊ณผ ์ฐ๊ด๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(m * n) |
| Space | O(1) |
ํผ๋๋ฐฑ: ์ ๋ ฅ ํ๋ ฌ์ ์ถ๊ฐ์ ์ธ ๊ณต๊ฐ์ ์ฌ์ฉํ์ง ์๊ณ , ์ฒซ ํ/์ด์ ๋ง์ปค๋ก ํ์ฉํ์ฌ ์ ์ฒด ์์๋ฅผ ํ ๋ฒ์ฉ ์ค์บํ๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
๋ต์ ์ ์ถ ๋ฌธ์
์์ฑ์ ์ฒดํฌ ๋ฆฌ์คํธ
In Review๋ก ์ค์ ํด์ฃผ์ธ์.๊ฒํ ์ ์ฒดํฌ ๋ฆฌ์คํธ
Important
๋ณธ์ธ ๋ต์ ์ ์ถ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ถ PR ํ๋ ์ด์์ ๋ฐ๋์ ๊ฒํ ๋ฅผ ํด์ฃผ์ ์ผ ํฉ๋๋ค!