๐Ÿ”ฅ[99ํด๋Ÿฝ ์ฝ”ํ…Œ ์Šคํ„ฐ๋””] 41์ผ์ฐจ TIL - Unique Paths II

HOONSSACยท2024๋…„ 8์›” 31์ผ
1

99Club Coding Test Study

๋ชฉ๋ก ๋ณด๊ธฐ
41/41
post-thumbnail
post-custom-banner

โณ๋ฌธ์ œ

You are given an m x n integer array grid. There is a robot initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

An obstacle and space are marked as 1 or 0 respectively in grid. A path that the robot takes cannot include any square that is an obstacle.

Return the number of possible unique paths that the robot can take to reach the bottom-right corner.

The testcases are generated so that the answer will be less than or equal to 2โˆ—1092 * 10^9.

๐Ÿ“„์ž…์ถœ๋ ฅ ์˜ˆ

์ž…์ถœ๋ ฅ ์˜ˆ #1

Input : obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
Output : 2
Explanation : There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right

์ž…์ถœ๋ ฅ ์˜ˆ #2

Input : obstacleGrid = [[0,1],[0,0]]
Output : 1

๐Ÿšจ์ œํ•œ ์‚ฌํ•ญ

  • m == obstacleGrid.length
  • n == obstacleGrid[i].length
  • 1 <= m, n <= 100
  • obstacleGrid[i][j] is 0 or 1.

โœ๏ธํ’€์ด

์ด ๋ฌธ์ œ๋Š” ์–ด์ œ ํ’€์—ˆ๋˜ Unique Paths ๋ฌธ์ œ์™€ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š๋‹ค.
๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ ์žฅ์• ๋ฌผ์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์žฅ์• ๋ฌผ์„ ํ”ผํ•ด๊ฐ€๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•˜๋‹ค.
๊ฒฝ๋กœ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•ด๋‚˜๊ฐ€๋Š” ๊ณผ์ •์€ ๊ธฐ์กด์˜ ์ ํ™”์‹๊ณผ ๋™์ผํ•œ๋ฐ,

grid[m][n] = grid[m-1][n] + grid[m][n-1]

์žฅ์• ๋ฌผ์€ 0์œผ๋กœ ํ‘œ์‹œ๋งŒ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€, ์žฅ์• ๋ฌผ์˜ ์œ„์น˜์ด๋‹ค.
์žฅ์• ๋ฌผ์ด ์ถœ๋ฐœ์ง€ [0, 0]์— ์žˆ๋Š” ๊ฒฝ์šฐ์™€ 0๋ฒˆ์งธ ํ–‰ ๋˜๋Š” 0๋ฒˆ์งธ ์—ด์— ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๋“œ ์ดˆ๊ธฐํ™”

    // ์ถœ๋ฐœ์ง€ ์œ„์น˜์— ์žฅ์• ๋ฌผ์ด ์žˆ๋Š” ๊ฒฝ์šฐ
    if (obstacleGrid[0][0] == 1) {
    	return 0;
    }

    obstacleGrid[0][0] = 1;
	// 0์—ด ์ดˆ๊ธฐํ™”
    for (int i = 1; i < m; i++) {
        // ์žฅ์• ๋ฌผ์€ 0์œผ๋กœ ์ €์žฅ
        if (obstacleGrid[i][0] == 1) {
            obstacleGrid[i][0] = 0;
        }
        else {
            obstacleGrid[i][0] = obstacleGrid[i-1][0];
        }
    }

    // 0ํ–‰ ์ดˆ๊ธฐํ™”
    for (int i = 1; i < n; i++) {
        if (obstacleGrid[0][i] == 1) {
            obstacleGrid[0][i] = 0;
        }
        else {
            obstacleGrid[0][i] = obstacleGrid[0][i-1];
        }
    }

๊ทธ๋ฆฌ๋“œ ์ดˆ๊ธฐํ™”๋Š” 0ํ–‰๊ณผ 0์—ด์— ๋ชจ๋‘ 1์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ธ๋ฐ,
์ถœ๋ฐœ์ง€์— ์žฅ์• ๋ฌผ์ด ์œ„์น˜ํ•ด ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ๋ฌด์กฐ๊ฑด ์ตœ์ข… ๋ฐ˜ํ™˜ ๊ฐ’์ด 0์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ฒ˜๋ฆฌํ•ด ์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ , 0์—ด๊ณผ 0ํ–‰์„ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ์žฅ์• ๋ฌผ์€ 0์œผ๋กœ ์ €์žฅํ•ด์ฃผ๊ณ , ์žฅ์• ๋ฌผ์ด ์•„๋‹ˆ๋ผ๋ฉด, ์ด์ „ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ €์žฅํ•จ์œผ๋กœ์จ, ์žฅ์• ๋ฌผ์˜ ์œ„์น˜๋ฅผ ๊ณ ๋ คํ•ด ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๊ฒฝ๋กœ ์ˆ˜ ํƒ์ƒ‰

        // ๊ฒฝ๋กœ ์ˆ˜ ํƒ์ƒ‰
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (obstacleGrid[i][j] == 1) {
                    obstacleGrid[i][j] = 0; // ์žฅ์• ๋ฌผ ํ‘œ์‹œ
                }
                else {
                    // ๊ฒฝ๋กœ ์ˆ˜ ์—…๋ฐ์ดํŠธ
                    obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1];
                }
            }
        }

์ด์ œ ๊ฒฝ๋กœ์˜ ์ˆ˜๋ฅผ [1,1]์ขŒํ‘œ๋ถ€ํ„ฐ ๊ธฐ์กด์˜ ์ ํ™”์‹์„ ์ด์šฉํ•ด ๊ตฌํ•ด๋‚˜๊ฐ€๋ฉด ๋œ๋‹ค.
๋‹จ, ์žฅ์• ๋ฌผ์„ ๋ฐœ๊ฒฌ ์‹œ ํ•ด๋‹น ์›์†Œ ๊ฐ’์— 0์„ ์ €์žฅํ•œ๋‹ค.

๐Ÿ‘พ์ „์ฒด ์ฝ”๋“œ

public static int uniquePathsWithObstacles(int[][] obstacleGrid) {
    int m = obstacleGrid.length; // ์„ธ๋กœ ๊ธธ์ด
    int n = obstacleGrid[0].length; // ๊ฐ€๋กœ ๊ธธ์ด

    // ์ถœ๋ฐœ์ง€ ์œ„์น˜์— ์žฅ์• ๋ฌผ์ด ์žˆ๋Š” ๊ฒฝ์šฐ
    if (obstacleGrid[0][0] == 1) {
        return 0;
    }

    obstacleGrid[0][0] = 1;

    // 0์—ด ์ดˆ๊ธฐํ™”
    for (int i = 1; i < m; i++) {
        // ์žฅ์• ๋ฌผ์€ 0์œผ๋กœ ์ €์žฅ
        if (obstacleGrid[i][0] == 1) {
            obstacleGrid[i][0] = 0;
        }
        else {
            obstacleGrid[i][0] = obstacleGrid[i-1][0];
        }
    }

    // 0ํ–‰ ์ดˆ๊ธฐํ™”
    for (int i = 1; i < n; i++) {
        if (obstacleGrid[0][i] == 1) {
            obstacleGrid[0][i] = 0;
        }
        else {
            obstacleGrid[0][i] = obstacleGrid[0][i-1];
        }
    }

    // ๊ฒฝ๋กœ ์ˆ˜ ํƒ์ƒ‰
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (obstacleGrid[i][j] == 1) {
                obstacleGrid[i][j] = 0; // ์žฅ์• ๋ฌผ ํ‘œ์‹œ
            }
            else {
                // ๊ฒฝ๋กœ ์ˆ˜ ์—…๋ฐ์ดํŠธ
                obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1];
            }
        }
    }

    return obstacleGrid[m-1][n-1];
}


๐Ÿ”—๋ฌธ์ œ ๋งํฌ
๐Ÿ’ปRepository

profile
ํ›ˆ์‹น์˜ ๊ฐœ๋ฐœ์—ฌํ–‰
post-custom-banner

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2024๋…„ 9์›” 1์ผ

ํ•ญํ•ด 99 ์ข…๋ฃŒ..! ๊ณ ์ƒํ–ˆ์–ด์š”์˜ค๐Ÿ‘๐Ÿ‘

1๊ฐœ์˜ ๋‹ต๊ธ€