Post

๐Ÿน ๋“ฑ๊ตฃ๊ธธ

1. ๋ฌธ์ œ ๋งํฌ

๋“ฑ๊ตฃ๊ธธ


2. ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def solution(m, n, puddles):
    answer = 0
    dp = [[0] * (m+1) for _ in range(n+1)]
    
    # dp ํ…Œ์ด๋ธ” ์„ธํŒ…
    # puddle ํ‘œ์‹œ
    for y, x in puddles:
        dp[x][y] = -1
    # ์„ธ๋กœ ์„ธํŒ…
    for i in range(1, n+1):
        # ์›…๋ฉ์ด ์ดํ›„๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ฐ€๋Š”๊ธธ ์—†์Œ
        if dp[i][1] == -1:
            break
        dp[i][1] = 1
    # ๊ฐ€๋กœ ์„ธํŒ…
    for i in range(1, m+1):
        # ์›…๋ฉ์ด ์ดํ›„๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ฐ€๋Š”๊ธธ ์—†์Œ
        if dp[1][i] == -1:
            break
        dp[1][i] = 1
    
    # ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ฐœ์ˆ˜ ํƒ์ƒ‰
    for i in range(2, n+1):
        for j in range(2, m+1):
            # ํ˜„์žฌ ์œ„์น˜๊ฐ€ puddle์ธ ๊ฒฝ์šฐ ํŒจ์Šค
            if dp[i][j] == -1:
                continue
            # ์–‘์ชฝ ๋ชจ๋‘ puddle ์—†๋Š” ๊ฒฝ์šฐ
            if dp[i-1][j] != -1 and dp[i][j-1] != -1:
                dp[i][j] = dp[i-1][j] + dp[i][j-1]
            # ์–‘์ชฝ ๋ชจ๋‘ puddle์ธ ๊ฒฝ์šฐ
            elif dp[i-1][j] == -1 and dp[i][j-1] == -1:
                dp[i][j] = 0
            # ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ puddle์ธ ๊ฒฝ์šฐ
            else:
                # ๋‘˜ ์ค‘ puddle ์•„๋‹Œ ๊ฐœ์ˆ˜ ์„ ํƒ
                dp[i][j] = max(dp[i-1][j], dp[i][j-1])
    
    return dp[n][m] % 1000000007
  • ์ •ํ™•์„ฑ

    ํ…Œ์ŠคํŠธ 1 ใ€‰ํ†ต๊ณผ (0.01ms, 10.2MB)
    ํ…Œ์ŠคํŠธ 2 ใ€‰ํ†ต๊ณผ (0.02ms, 10.3MB)
    ํ…Œ์ŠคํŠธ 3 ใ€‰ํ†ต๊ณผ (0.02ms, 10.1MB)
    ํ…Œ์ŠคํŠธ 4 ใ€‰ํ†ต๊ณผ (0.03ms, 10.2MB)
    ํ…Œ์ŠคํŠธ 5 ใ€‰ํ†ต๊ณผ (0.06ms, 10.5MB)
    ํ…Œ์ŠคํŠธ 6 ใ€‰ํ†ต๊ณผ (0.06ms, 10.1MB)
    ํ…Œ์ŠคํŠธ 7 ใ€‰ํ†ต๊ณผ (0.08ms, 10.3MB)
    ํ…Œ์ŠคํŠธ 8 ใ€‰ํ†ต๊ณผ (0.09ms, 10.2MB)
    ํ…Œ์ŠคํŠธ 9 ใ€‰ํ†ต๊ณผ (0.05ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 10 ใ€‰ํ†ต๊ณผ (0.02ms, 10.3MB)
  • ํšจ์œจ์„ฑ

    ํ…Œ์ŠคํŠธ 1 ใ€‰ํ†ต๊ณผ (2.60ms, 10.3MB)
    ํ…Œ์ŠคํŠธ 2 ใ€‰ํ†ต๊ณผ (0.97ms, 10.4MB)
    ํ…Œ์ŠคํŠธ 3 ใ€‰ํ†ต๊ณผ (1.54ms, 10.2MB)
    ํ…Œ์ŠคํŠธ 4 ใ€‰ํ†ต๊ณผ (3.11ms, 10.3MB)
    ํ…Œ์ŠคํŠธ 5 ใ€‰ํ†ต๊ณผ (1.68ms, 10.2MB)
    ํ…Œ์ŠคํŠธ 6 ใ€‰ํ†ต๊ณผ (3.02ms, 10.2MB)
    ํ…Œ์ŠคํŠธ 7 ใ€‰ํ†ต๊ณผ (1.41ms, 10.2MB)
    ํ…Œ์ŠคํŠธ 8 ใ€‰ํ†ต๊ณผ (2.50ms, 10.3MB)
    ํ…Œ์ŠคํŠธ 9 ใ€‰ํ†ต๊ณผ (2.67ms, 10.2MB)
    ํ…Œ์ŠคํŠธ 10 ใ€‰ํ†ต๊ณผ (1.86ms, 10.2MB)


3. ํ•ด์„ค

์ค‘ํ•™๊ต ์ˆ˜ํ•™๋•Œ ๋ฐฐ์› ๋˜ ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

์œ„ ๊ทธ๋ฆผ์—์„œ ์ง‘๊ณผ ํ•™๊ต๋ฅผ ๊ผญ์ง“์ ์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ๋‹ค์‹œ ๊ทธ๋ฆฌ๋ฉด

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ตœ๋‹จ๊ฑฐ๋ฆฌ์˜ ๊ฐœ์ˆ˜๊ฐ€ 10์ด ๋‚˜์˜จ๋‹ค.

์ด๊ฒƒ์€ ํ˜„์žฌ ์œ„์น˜์—์„œ ๊ฐ๊ฐ โ€˜์œ„์ชฝ ์œ„์น˜โ€™์™€ โ€˜์™ผ์ชฝ ์œ„์น˜โ€™๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ์„ ์ด์šฉํ•œ ๊ฒƒ์ด๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด dp[i][j] = dp[i-1][j] + dp[i][j-1]์ด ์„ฑ๋ฆฝํ•œ๋‹ค.

๋งŒ์•ฝ ๋ฌผ์›…๋ฉ์ด๋ฅผ ๋งˆ์ฃผ์น˜๊ฒŒ ๋œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

  1. ์œ„, ์™ผ์ชฝ ๋ชจ๋‘ ๋ฌผ์›…๋ฉ์ด์ธ ๊ฒฝ์šฐ

    ํ˜„์žฌ ์œ„์น˜๋Š” ๊ฐˆ ์ˆ˜ ์—†๋Š” ์œ„์น˜๋กœ ๊ฐ„์ฃผํ•˜์—ฌ โ†’ dp[i][j] = 0

  2. ์œ„, ์™ผ์ชฝ ๋‘˜ ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ฌผ ์›…๋ฉ์ด์ธ ๊ฒฝ์šฐ

    ๋ฌผ ์›…๋ฉ์ด์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋Š” 0์œผ๋กœ ๊ฐ„์ฃผํ•˜์—ฌ ๋ฌผ ์›…๋ฉ์ด๊ฐ€ ์—†๋Š” ์œ„์น˜์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ฐœ์ˆ˜์™€ ๋™์ผํ•ด์ง„๋‹ค.

    โ†’ dp[i][j] = max(dp[i-1][j], dp[i][j-1])

    (์ด๋•Œ, ๋ฌผ์›…๋ฉ์ด ์œ„์น˜์˜ dp๊ฐ’์„ ์ด๋ฏธ -1๋กœ ์ดˆ๊ธฐํ™” ํ•ด๋‘์—ˆ์œผ๋ฏ€๋กœ max๊ฐ’์œผ๋กœ๋Š” ๋ฌผ ์›…๋ฉ์ด๊ฐ€ ์—†๋Š” ์œ„์น˜์˜ dp๊ฐ’์ด ๋ฆฌํ„ด๋œ๋‹ค)

๋”ฐ๋ผ์„œ ์œ„ ๊ฒฝ์šฐ๋ฅผ ๋งŒ์กฑ์‹œํ‚ค๋„๋ก dp ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”์‹œํ‚ค๊ณ  dp[n][m]์„ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

This post is licensed under CC BY 4.0 by the author.