Cute Running Puppy

algorithm/Programmers

[Programmers] ๋“ฑ๊ตฃ๊ธธ (Python, Java)

R.silver 2024. 7. 2. 20:54
๋ฐ˜์‘ํ˜•

๐Ÿ“–๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/42898

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

โœจํ•ต์‹ฌ ๋‚ด์šฉ 

"์˜ค๋ฅธ์ชฝ๊ณผ ์•„๋ž˜์ชฝ"์œผ๋กœ๋งŒ ์›€์ง์—ฌ
์ง‘์—์„œ ํ•™๊ต๊นŒ์ง€ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋‹จ๊ฒฝ๋กœ์˜ ๊ฐœ์ˆ˜๋ฅผ "1,000,000,007"๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ return

๐Ÿค”ํ•ด๊ฒฐ ์•„์ด๋””์–ด 

์œ ํ˜•: DP

ํ˜„์žฌ ์œ„์น˜(i, j)์—์„œ ๊ฒฝ๋กœ์˜ ๊ฐ’์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋ ค๋ฉด ์™ผ์ชฝ(i, j-1) ํ˜น์€ ์œ„(i-1, j)์—์„œ ์™€์•ผ ํ•œ๋‹ค 

๐ŸŸกdp ๊ตฌ์กฐ

1. dp ์ •์˜ 

- (i, j) ์œ„์น˜๊นŒ์ง€ ์˜ฌ ์ˆ˜ ์žˆ๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ์˜ ๊ฐœ์ˆ˜ 

 

2. dp ์ดˆ๊ธฐํ™” 

- ์ง‘์˜ ์œ„์น˜: (0, 0)

- ์œ„์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ์œ„์น˜ (์™ผ์ชฝ์—์„œ ์˜ฌ ์ˆ˜ ์—†๋Š” ์œ„์น˜): (i, 0)

- ์™ผ์ชฝ์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ์œ„์น˜ (์œ„์—์„œ ์˜ฌ ์ˆ˜ ์—†๋Š” ์œ„์น˜): (0, j)

 

3. ์ ํ™”์‹ 

if (์›…๋ฉ์ด๊ฐ€ ์•„๋‹ˆ๋ฉด):
	dp[i][j] = dp[i][j-1] + dp[i-1][j]

 

๐ŸŸก์ฃผ์˜ํ•  ๊ฒƒ

1. puddles์€ ํ–‰๊ณผ ์—ด์˜ ์œ„์น˜๊ฐ€ ๋ฐ”๋€Œ์–ด ์ „๋‹ฌ๋œ๋‹ค

2. % ์—ฐ์‚ฐ์„ ํ•ด์„œ return ํ•ด์•ผ ํ•œ๋‹ค (๋ฌธ์ œ ์กฐ๊ฑด ์ž˜ ์ฝ๊ธฐ)

- ์—ฐ์‚ฐ ์ค‘๊ฐ„์— % ์—ฐ์‚ฐ์„ ํ•ด ์ฃผ์–ด์•ผ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์ง€ ์•Š๋Š”๋‹ค (๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ ๋ฒ•์น™ ํ™œ์šฉ)

 

+) ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ ๋ฒ•์น™

(A + B) % p = ((A % p) + (B % p)) % p
(A * B) % p = ((A % p) * (B % p)) % p
(A - B) % p = ((A % p) - (B % p) + p) % p

โœ…์ •๋‹ต ์ฝ”๋“œ (Python)

def solution(m, n, puddles):
    # 0. board ์ฑ„์šฐ๊ธฐ (1: ์›…๋ฉ์ด)
    board = [[0 for _ in range(m)] for _ in range(n)]

    for p in puddles:
        if p != []:
            board[p[1] - 1][p[0] - 1] = 1

    # 1.dp ์ •์˜
    dp = [[0 for _ in range(m)] for _ in range(n)]

    # 2. ์ดˆ๊ธฐํ™”
    # 2-1. ์›์  - ์ถœ๋ฐœ ์ง€์ ์—๋Š” ์›…๋ฉ์ด๊ฐ€ ์—†์Œ
    dp[0][0] = 1

	# 2-2. (i, 0) - ์œ„์—์„œ๋งŒ ์˜ฌ ์ˆ˜ ์žˆ์Œ
    for i in range(n):
        if board[i][0]:
            break
        dp[i][0] = 1

	# 2-2. (0, j) - ์™ผ์ชฝ์—์„œ๋งŒ ์˜ฌ ์ˆ˜ ์žˆ์Œ
    for j in range(m):
        if board[0][j]:
            break
        dp[0][j] = 1

    # 3. ์ ํ™”์‹
    for i in range(1, n):
        for j in range(1, m):
        	# ์™ผ์ชฝ, ์œ„์ชฝ์—์„œ ์˜จ ๊ฐ’ ๋”ํ•˜๊ธฐ
            if not board[i][j]: # ์›…๋ฉ์ด๋ผ๋ฉด 
                dp[i][j] = (dp[i][j - 1] + dp[i - 1][j]) % 1000000007
	
    // 4. output
    return dp[n - 1][m - 1] % 1000000007

โœ…์ •๋‹ต ์ฝ”๋“œ (Java)

class Solution {
    public int solution(int m, int n, int[][] puddles) {
        // 0. board ์ฑ„์šฐ๊ธฐ (1: ์›…๋ฉ์ด)
        int[][] board = new int[n][m];
        if (puddles[0].length > 0){
            for (int i = 0 ; i < puddles.length; i++) {
                board[puddles[i][1] - 1][puddles[i][0] - 1] = 1;
            }
        }        
                
        // 1. dp ์ •์˜ 
        int[][] dp = new int[n][m];
        
        // 2. ์ดˆ๊ธฐํ™”
        // 2-1. ์›์  - ์ถœ๋ฐœ ์ง€์ ์—๋Š” ์›…๋ฉ์ด๊ฐ€ ์—†์Œ
        dp[0][0] = 1;
        
        // 2-2. (i, 0) - ์œ„์—์„œ๋งŒ ์˜ฌ ์ˆ˜ ์žˆ์Œ
        for (int i = 0 ; i < n; i++) {
            if (board[i][0] == 1) {
                break;
            }
            dp[i][0] = 1;
        }
            
        
        // 2-2. (0, j) - ์™ผ์ชฝ์—์„œ๋งŒ ์˜ฌ ์ˆ˜ ์žˆ์Œ
        for (int j = 0 ; j < m; j++){
            if(board[0][j] == 1) {
                break;
            }
            dp[0][j] = 1;
        }
            
        
        // 3. ์ ํ™”์‹ 
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < m; j++) {
                // ์™ผ์ชฝ, ์œ„์ชฝ์—์„œ ์˜จ ๊ฐ’ ๋”ํ•˜๊ธฐ
                if (board[i][j] == 0) // ์›…๋ฉ์ด๋ผ๋ฉด ๋น„์›Œ๋‘๊ธฐ
                    dp[i][j] = (dp[i][j-1] + dp[i-1][j]) % 1000000007;
            }
        }
        
        // debug - ๊ทธ๋Œ€๋กœ ์ œ์ถœํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ (์ฃผ์˜)
        // for (int i = 0 ; i < n; i++) {
        //     for (int j = 0 ; j < m; j++) {
        //         System.out.printf("%d ", dp[i][j]);
        //     }
        //     System.out.println();
        // }
        
        // 4. output
        return dp[n-1][m-1] % 1000000007;
    }
}

 

 

๋ฐ˜์‘ํ˜•