Skip to content

[ICE0208] WEEK 07 Solutions - #2798

Open
ICE0208 wants to merge 4 commits into
DaleStudy:mainfrom
ICE0208:week07
Open

[ICE0208] WEEK 07 Solutions#2798
ICE0208 wants to merge 4 commits into
DaleStudy:mainfrom
ICE0208:week07

Conversation

@ICE0208

@ICE0208 ICE0208 commented Aug 6, 2026

Copy link
Copy Markdown
Member

๋‹ต์•ˆ ์ œ์ถœ ๋ฌธ์ œ

์ž‘์„ฑ์ž ์ฒดํฌ ๋ฆฌ์ŠคํŠธ

  • Projects์˜ ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ(โ–ผ)์„ ๋ˆŒ๋Ÿฌ ํ™•์žฅํ•œ ๋’ค, Week๋ฅผ ํ˜„์žฌ ์ฃผ์ฐจ๋กœ ์„ค์ •ํ•ด์ฃผ์„ธ์š”.
  • ๋ฌธ์ œ๋ฅผ ๋ชจ๋‘ ํ‘ธ์‹œ๋ฉด ํ”„๋กœ์ ํŠธ์—์„œ Status๋ฅผ In Review๋กœ ์„ค์ •ํ•ด์ฃผ์„ธ์š”.
  • ์ฝ”๋“œ ๊ฒ€ํ† ์ž 1๋ถ„ ์ด์ƒ์œผ๋กœ๋ถ€ํ„ฐ ์Šน์ธ์„ ๋ฐ›์œผ์…จ๋‹ค๋ฉด PR์„ ๋ณ‘ํ•ฉํ•ด์ฃผ์„ธ์š”.

๊ฒ€ํ† ์ž ์ฒดํฌ ๋ฆฌ์ŠคํŠธ

Important

๋ณธ์ธ ๋‹ต์•ˆ ์ œ์ถœ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๋ถ„ PR ํ•˜๋‚˜ ์ด์ƒ์„ ๋ฐ˜๋“œ์‹œ ๊ฒ€ํ† ๋ฅผ ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค!

  • ๋ฐ”๋กœ ์ด์ „์— ์˜ฌ๋ผ์˜จ PR์— ๋ณธ์ธ์„ ์ฝ”๋“œ ๋ฆฌ๋ทฐ์–ด๋กœ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”.
  • ๋ณธ์ธ์ด ๊ฒ€ํ† ํ•ด์•ผํ•˜๋Š” PR์˜ ๋‹ต์•ˆ ์ฝ”๋“œ์— ํ”ผ๋“œ๋ฐฑ์„ ์ฃผ์„ธ์š”.
  • ํ† ์š”์ผ ์ „๊นŒ์ง€ PR์„ ๋ณ‘ํ•ฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์Šน์ธํ•ด์ฃผ์„ธ์š”.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿท๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒจํ„ด ๋ถ„์„

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๋กœ ํ‘œ์‹œํ•ด ์ค‘๋ณต ๋ฐฉ๋ฌธ์„ ๋ง‰๋Š”๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ํ˜„์žฌ ๊ตฌํ˜„์ด ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

@dalestudy

dalestudy Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

๐Ÿ“Š ICE0208 ๋‹˜์˜ ํ•™์Šต ํ˜„ํ™ฉ

์ด๋ฒˆ ์ฃผ ์ œ์ถœ ๋ฌธ์ œ

๋ฌธ์ œ ๋‚œ์ด๋„ ์œ ํ˜• ๋ถ„์„
number-of-islands Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•
set-matrix-zeroes Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•
unique-paths Medium โœ… ์˜๋„ํ•œ ์œ ํ˜•

๋ˆ„์  ํ•™์Šต ์š”์•ฝ

  • ํ’€์ดํ•œ ๋ฌธ์ œ: 30 / 75๊ฐœ
  • ์ด๋ฒˆ ์ฃผ ์œ ํ˜• ์ผ์น˜์œจ: 100% (3๋ฌธ์ œ ์ค‘ 3๋ฌธ์ œ ์ผ์น˜)

๋ฌธ์ œ ํ’€์ด ํ˜„ํ™ฉ

์นดํ…Œ๊ณ ๋ฆฌ ์ง„ํ–‰๋„ ์™„๋ฃŒ
Array โ– โ– โ– โ– โ– โ– โ–ก 8 / 10 (Medium 5, Easy 3)
Dynamic Programming โ– โ– โ– โ– โ–กโ–กโ–ก 7 / 11 (Easy 1, Medium 6)
Matrix โ– โ– โ– โ– โ–กโ–กโ–ก 2 / 4 (Medium 2)
String โ– โ– โ– โ– โ–กโ–กโ–ก 5 / 10 (Medium 2, Easy 3)
Heap โ– โ– โ–กโ–กโ–กโ–กโ–ก 1 / 3 (Medium 1)
Tree โ– โ– โ–กโ–กโ–กโ–กโ–ก 4 / 14 (Medium 3, Easy 1)
Binary โ– โ–กโ–กโ–กโ–กโ–กโ–ก 1 / 5 (Easy 1)
Linked List โ– โ–กโ–กโ–กโ–กโ–กโ–ก 1 / 6 (Easy 1)
Graph โ– โ–กโ–กโ–กโ–กโ–กโ–ก 1 / 8 (Medium 1)
Interval โ–กโ–กโ–กโ–กโ–กโ–กโ–ก 0 / 5 โ† ์•„์ง ์‹œ์ž‘ ์•ˆ ํ•จ

๐Ÿค– ์ด ๋Œ“๊ธ€์€ GitHub App์„ ํ†ตํ•ด ์ž๋™์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ”ข API ์‚ฌ์šฉ๋Ÿ‰ (gpt-5-nano)
์š”์ฒญ ์ž…๋ ฅ ํ† ํฐ ์ถœ๋ ฅ ํ† ํฐ ํ•ฉ๊ณ„ ๋น„์šฉ
1 802 37 839 $0.000055
2 1,394 126 1,520 $0.000120
3 1,394 116 1,510 $0.000116
4 2,020 149 2,169 $0.000161
ํ•ฉ๊ณ„ 5,610 428 6,038 $0.000452

@dolphinflow86
dolphinflow86 self-requested a review August 6, 2026 11:42
Comment thread unique-paths/ICE0208.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿท๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒจํ„ด ๋ถ„์„

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๋ฅผ ํ†ตํ•ด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์˜€๊ณ , ๊ฐ ํ–‰๋งˆ๋‹ค ์ด์ „ ์—ด์˜ ๊ฐ’์„ ์ด์šฉํ•ด ํ˜„์žฌ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ์ถ”๊ฐ€์ ์œผ๋กœ ์ปค์Šคํ…€ ์ตœ์ ํ™”๋‚˜ ์ดˆ๊ธฐํ™” ๋กœ์ง์„ ๊ฐ„์†Œํ™”ํ•ด๋„ ์ข‹๋‹ค.

๐Ÿ’ก ํ’€์ด์— ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ๋ณด์„ธ์š”!

Comment thread unique-paths/ICE0208.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿท๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒจํ„ด ๋ถ„์„

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 ๋ฐฐ์—ด ๋ฒ„์ „์€ ๊ณต๊ฐ„์„ ์ ˆ์•ฝํ•ฉ๋‹ˆ๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ํ˜„์žฌ ๊ตฌํ˜„์ด ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

๐Ÿ’ก ํ’€์ด์— ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ๋ณด์„ธ์š”!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿท๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒจํ„ด ๋ถ„์„

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)

ํ”ผ๋“œ๋ฐฑ: ์ž…๋ ฅ ํ–‰๋ ฌ์— ์ถ”๊ฐ€์ ์ธ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์ฒซ ํ–‰/์—ด์„ ๋งˆ์ปค๋กœ ํ™œ์šฉํ•˜์—ฌ ์ „์ฒด ์›์†Œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์Šค์บ”ํ•œ๋‹ค.

๊ฐœ์„  ์ œ์•ˆ: ํ˜„์žฌ ๊ตฌํ˜„์ด ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

๐Ÿ’ก ํ’€์ด์— ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ฃผ์„์œผ๋กœ ๋‚จ๊ฒจ๋ณด์„ธ์š”!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

1 participant