๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋“ฑ๊ตฃ๊ธธ (๋™์ ๊ณ„ํš๋ฒ•, Dynamic Programming)

๐ŸŽฎinspirer9 2023. 2. 27. 12:31
728x90
๋ฐ˜์‘ํ˜•

์ฒจ์— ๋˜ ๊ธธ์ฐพ๊ธฐ๋ƒ? ์ฝ”๋“œ ์žฌํ™œ์šฉํ•ด์•ผ์ง€ ใ…‹ใ…‹ใ…‹ ํ–ˆ๋Š”๋ฐ ์ˆ˜ํ•™๋ฌธ์ œ์˜€๋‹ค. ์ฟจ๋Ÿญ...

๋ฌธ์ œ ์„ค๋ช…

๊ณ„์†๋˜๋Š” ํญ์šฐ๋กœ ์ผ๋ถ€ ์ง€์—ญ์ด ๋ฌผ์— ์ž ๊ฒผ์Šต๋‹ˆ๋‹ค. ๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š์€ ์ง€์—ญ์„ ํ†ตํ•ด ํ•™๊ต๋ฅผ ๊ฐ€๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ง‘์—์„œ ํ•™๊ต๊นŒ์ง€ ๊ฐ€๋Š” ๊ธธ์€ m x n ํฌ๊ธฐ์˜ ๊ฒฉ์ž๋ชจ์–‘์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์•„๋ž˜ ๊ทธ๋ฆผ์€ m = 4, n = 3 ์ธ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.

๊ฐ€์žฅ ์™ผ์ชฝ ์œ„, ์ฆ‰ ์ง‘์ด ์žˆ๋Š” ๊ณณ์˜ ์ขŒํ‘œ๋Š” (1, 1)๋กœ ๋‚˜ํƒ€๋‚ด๊ณ  ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜, ์ฆ‰ ํ•™๊ต๊ฐ€ ์žˆ๋Š” ๊ณณ์˜ ์ขŒํ‘œ๋Š” (m, n)์œผ๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
๊ฒฉ์ž์˜ ํฌ๊ธฐ m, n๊ณผ ๋ฌผ์ด ์ž ๊ธด ์ง€์—ญ์˜ ์ขŒํ‘œ๋ฅผ ๋‹ด์€ 2์ฐจ์› ๋ฐฐ์—ด puddles์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์˜ค๋ฅธ์ชฝ๊ณผ ์•„๋ž˜์ชฝ์œผ๋กœ๋งŒ ์›€์ง์—ฌ ์ง‘์—์„œ ํ•™๊ต๊นŒ์ง€ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋‹จ๊ฒฝ๋กœ์˜ ๊ฐœ์ˆ˜๋ฅผ 1,000,000,007๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • ๊ฒฉ์ž์˜ ํฌ๊ธฐ m, n์€ 1 ์ด์ƒ 100 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
    • m๊ณผ n์ด ๋ชจ๋‘ 1์ธ ๊ฒฝ์šฐ๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๋ฌผ์— ์ž ๊ธด ์ง€์—ญ์€ 0๊ฐœ ์ด์ƒ 10๊ฐœ ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์ง‘๊ณผ ํ•™๊ต๊ฐ€ ๋ฌผ์— ์ž ๊ธด ๊ฒฝ์šฐ๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

m n puddles return
4 3 [[2, 2]] 4

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

ํ’€์ด

  • ์ˆ˜ํ•™ ๋ฌธ์ œ์ธ ์ค„ ์•Œ์•˜๋Š”๋ฐ ์›…๋ฉ์ด ์œ„์น˜๊ฐ€ ์–ด๋””์ธ์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†๋„ค?
    • ์›…๋ฉ์ด๊ฐ€ ํ•™๊ต๋ฅผ ๋ง‰๊ณ  ์žˆ์œผ๋ฉด ์›…๋ฉ์ด ๊ฐฏ์ˆ˜๊ฐ€ ์ ์–ด๋„ ํ•™๊ต๋กœ ๊ฐˆ ์ˆ˜๊ฐ€ ์—†๋‹ค.
      • ํ•˜์ง€๋งŒ "์ง‘๊ณผ ํ•™๊ต๊ฐ€ ๋ฌผ์— ์ž ๊ธด ๊ฒฝ์šฐ๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค."๋ผ๊ณ  ใ…‹ใ…‹ใ…‹
        • ๊ทธ๋Ÿผ ์ˆ˜ํ•™ ๋ฌธ์  ๊ฐ€? ๋‹ค์‹œ ใ…‹ใ…‹ใ…‹
  • ๊ทธ๋ ‡๋”๋ผ๋„ ์›…๋ฉ์ด ์œ„์น˜์— ๋”ฐ๋ผ ๊ฐฏ์ˆ˜์™€ ์ƒ๊ด€์—†์ด ๋‹ต์ด ๊ฐ™์„ ์ˆ˜ ์žˆ๋‹ค.
    • ๊ทธ๋Ÿผ ๊ฐ™๊ฒŒ ๋งŒ๋“ค๋ฉด ๋˜๋Š”๊ฑฐ ์•„๋‹ˆ๋ƒ???
      • ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†๋‹ค. ์ด๋™์„ ์šฐ์ธก์ด๋ž‘ ์•„๋ž˜๋กœ๋งŒ ๊ฐˆ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ง€๋„๋ฅผ ๋ณด์ •ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

  • ๊ทธ๋ฆฌ๊ณ  ์ด ๋ฌธ์ œ์˜ ํ•จ์ •์€
    • ์ขŒํ‘œ ๋ฌธ์ œ
      • m x n์„ m์€ ๊ฐ€๋กœ, n์€ ์„ธ๋กœ๋กœ ์ฃผ๊ณ ,
      • puddles์˜ ์ขŒํ‘œ๋Š” [[2,2]]๋กœ ์ค˜๋†“๊ณ , ์–ด๋–ค๊ฒŒ ๊ฐ€๋กœ, ์„ธ๋กœ์ธ์ง€ ์•ˆ ์•Œ๋ ค์คฌ๋‹ค๋Š” ์ 
      • ๋ณดํ†ต ์ƒ์‹์ ์œผ๋กœ m x n์ด ๊ฐ€๋กœ, ์„ธ๋กœ๋ฉด, puddles๋„ ๊ฐ€๋กœ ์„ธ๋กœ๋ผ๊ณ  ์ƒ๊ฐํ•˜์ง€...
      • ๋ˆ„๊ฐ€ ๋’ค์ง‘์–ด์„œ ์ƒ๊ฐํ•˜๊ฒ ๋ƒ๊ณ ใ…‹ใ…‹ใ…‹ (์‹ค์ˆ˜๋กœ ์ž˜๋ชป ์ฝ”๋”ฉํ•˜๋ฉด ํ•œ๋ฒˆ์— ๋งž์Œ)
    • ๋‘๋ฒˆ์งธ๋Š” ์ตœ๋‹จ๊ฒฝ๋กœ...
      • ์–ด์ฐจํ”ผ ์˜ค๋ฅธ์ชฝ์ด๋ž‘ ์•„๋ž˜๋กœ๋งŒ ์ด๋™ํ•  ์ˆ˜ ๋ฐ–์— ์—†๊ธฐ ๋•Œ๋ฌธ์—... ๊ทธ๋ƒฅ ๊ฐ€๋ฉด ์ตœ๋‹จ๊ฒฝ๋กœ์ž„...
  • ๊ฒฐ๋ก ์€ DP๋กœ ๊ฐ„๋‹ค.
    • ์˜ค๋ฅธ์ชฝ๊ณผ ์•„๋ž˜์ชฝ์œผ๋กœ 2๋ช…์”ฉ ๋‹Œ์ž ๋ถ„์‹ ์ˆ ์„ ์จ์„œ ๋ถ„์‹ ์ด ๋ช‡ ๋งˆ๋ฆฌ๊ฐ€ ํ•™๊ต์— ๋„์ฐฉํ•˜๋Š”์ง€๋ฅผ ๋‹ต์œผ๋กœ ์“ฐ๋Š” ๋ฌธ์ œ
      • ์ฒ˜์Œ ์ง‘์—์„œ
        • ์˜ค๋ฅธ์ชฝ ๊ธธ์ด ๋น„์–ด์žˆ์œผ๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ถ„์‹  ํ•œ๋ช… ๋ณด๋ƒ„
        • ์•„๋žซ์ชฝ ๊ธธ๋„ ๋น„์–ด์žˆ์œผ๋ฉด ์•„๋žซ์ชฝ์œผ๋กœ๋„ ๋ถ„์‹  ํ•œ๋ช… ๋ณด๋ƒ„
      • ๋ถ„์‹ ์€ ์ด๋™ํ•˜๋ฉด์„œ ๋ณธ์ฒด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์•„๋žซ์ชฝ์œผ๋กœ ๋ถ„์‹ ์„ ๋งŒ๋“ฌ
      • ํ•™๊ต์— ๋ถ„์‹ ์ด ๋„์ฐฉํ•  ๋•Œ ๋งˆ๋‹ค +1
    • ๋‚˜์ง€๋ง‰์— 1000000007๋กœ ๋‚˜๋ˆ„๋Š” ๊ฑฐ ๊นŒ๋จน์ง€ ์•Š๋Š”๋‹ค.
      • ๊ตฌ์ƒ์€ ๋. ์ด์ œ ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉ ๊ฐ€์ฆˆ์•„!
  • ๋ฌด์ง€์„ฑ์œผ๋กœ ํ•ด์„œ ๊ทธ๋Ÿฐ๊ฐ€...???
    • ์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ๋Š” ๋‹ค ํ†ต๊ณผ์ธ๋ฐ, ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ๋Š” ๋‹ค ์‹คํŒจ... ์ด์ƒํ•˜๋‹ค?
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.17ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.28ms, 10.6MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	์‹คํŒจ (23.34ms, 10.6MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	์‹คํŒจ (7.10ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	์‹คํŒจ (9.52ms, 10.5MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	์‹คํŒจ (13.87ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	์‹คํŒจ (12.20ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	์‹คํŒจ (23.99ms, 10.5MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	์‹คํŒจ (8.48ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	์‹คํŒจ (15.59ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (17.96ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (15.38ms, 10.4MB)
  • ์ฝ”๋“œ ์•„๋ฌด๋ฆฌ ๋ด๋„ ๋ญ๊ฐ€ ๋ฌธ์ œ์ธ์ง€ ๋ชจ๋ฆ„... ใ…‹ใ…‹ใ…‹
    • ๊ฒŒ๋‹ค๊ฐ€ ์ฃผ์„์„ ๋‹ค ์ง€์›Œ๋ฒ„๋ ค์„œ... ์™œ ์ €๋ ‡๊ฒŒ ์งฏ์ง€? ํ—ท๊ฐˆ๋ฆผ
def solution(m, n, puddles):
    ๋‹Œ์ž์Šค = []
    ์ง€๋„ = [[0 for _ in range(m)] for _ in range(n) ]
    
    ๋‹Œ์ž์Šค.append([0,0])
    ์ง€๋„[0][0] = 1
    
    for puddle in puddles:
        x, y = puddle
        ์ง€๋„[y-1][x-1] = -1
        
    while len(๋‹Œ์ž์Šค) > 0:
        ๋‹Œ์ž์ˆ˜ = len(๋‹Œ์ž์Šค)
        for i in range(๋‹Œ์ž์ˆ˜):
            x,y = ๋‹Œ์ž์Šค.pop(0)
            if x < m-1 and ์ง€๋„[y][x+1] != -1:
                ์ง€๋„[y][x+1] += ์ง€๋„[y][x]
                if [x+1,y] not in ๋‹Œ์ž์Šค:
                    ๋‹Œ์ž์Šค.append([x+1,y])
            if y < n-1 and ์ง€๋„[y+1][x] != -1:
                ์ง€๋„[y+1][x] += ์ง€๋„[y][x]
                if [x,y+1] not in ๋‹Œ์ž์Šค:
                    ๋‹Œ์ž์Šค.append([x,y+1])
                    
    return ์ง€๋„[n-1][m-1] #ํ•™๊ต์—๋„์ฐฉํ•œ๋ถ„์‹ ๊ฐฏ์ˆ˜
  • ๋ณ‘์‹  ์ฒ˜๋Ÿผ ์‹œ๊ฐ„ ๋‚ญ๋น„ํ•จ. ใ…‹ใ…‹ใ…‹
    • ๋ฆฌํ„ดํ•  ๋•Œ 1000000007๋กœ ๋‚˜๋ˆˆ ๋ชซ์„ ๋„˜๊ธฐ๋ผ๊ณ  ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹
      • ์‹œ๊ฐ„ ๋‚ญ๋น„ ์˜ค์กŒ๋‹ค.
def solution(m, n, puddles):
    ๋‹Œ์ž์Šค = []
    ์ง€๋„ = [[0 for _ in range(m)] for _ in range(n) ]
    
    ๋‹Œ์ž์Šค.append([0,0])
    ์ง€๋„[0][0] = 1
    
    for puddle in puddles:
        x, y = puddle
        ์ง€๋„[y-1][x-1] = -1
        
    while len(๋‹Œ์ž์Šค) > 0:
        ๋‹Œ์ž์ˆ˜ = len(๋‹Œ์ž์Šค)
        for i in range(๋‹Œ์ž์ˆ˜):
            x,y = ๋‹Œ์ž์Šค.pop(0)
            if x < m-1 and ์ง€๋„[y][x+1] != -1:
                ์ง€๋„[y][x+1] += ์ง€๋„[y][x]
                if [x+1,y] not in ๋‹Œ์ž์Šค:
                    ๋‹Œ์ž์Šค.append([x+1,y])
            if y < n-1 and ์ง€๋„[y+1][x] != -1:
                ์ง€๋„[y+1][x] += ์ง€๋„[y][x]
                if [x,y+1] not in ๋‹Œ์ž์Šค:
                    ๋‹Œ์ž์Šค.append([x,y+1])
                    
    return ์ง€๋„[n-1][m-1] % 1000000007 #ํ•™๊ต์—๋„์ฐฉํ•œ๋ถ„์‹ ๊ฐฏ์ˆ˜
  • ๊ณ ์ˆ˜์˜ ์ฝ”๋“œ.... ํšจ์œจ์„ฑ ๊ฐœ๋น ๋ฆ„
    • ์•„.. ์œ„์— ํ•œ์ค„ ๋” ๋งŒ๋“ค์–ด์„œ ์ธ๋ฑ์Šค ์—๋Ÿฌ ๋ฐฉ์ง€. ๋Œ€๋ฐ•์ด๋‹ค.
      • ๋‚˜๋„ ์ฒ˜์Œ์— ๋‹Œ์ž๋ฅผ ๋จผ์ € ๋ณด๋‚ด๊ณ  ์ƒ/์ขŒ์—์„œ ๊ฐ’์„ ๋”ํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ...
      • ๋ช‡๋ฒˆ ์งœ๋‹ค๊ฐ€ ์ฝ”๋“œ ์—๋Ÿฌ ๋‚˜์„œ ํฌ๊ธฐํ–ˆ๋Š”๋ฐ... ์ง€๊ธˆ๋ณด๋‹ˆ๊นŒ ์ด๋ ‡๊ฒŒ ํ•˜๋Š”๊ฒŒ ๋” ์‰ฝ๋‹ค.
      • ์›…๋ฉ์ด ์ฒดํฌ ์•ˆํ•ด๋„ ๋˜๊ณ , ๊ทธ๋ƒฅ ๊ฐ’๋งŒ ๋”ํ•˜๋ฉด ๋œ๋‹ค.
def solution(m,n,puddles):
    grid = [[0]*(m+1) for i in range(n+1)] #์™ผ์ชฝ, ์œ„๋กœ ํ•œ์ค„์”ฉ ๋งŒ๋“ค์–ด์„œ IndexError ๋ฐฉ์ง€
    if puddles != [[]]:                    #๋ฌผ์ด ์ž ๊ธด ์ง€์—ญ์ด 0์ผ ์ˆ˜ ์žˆ์Œ
        for a, b in puddles:
            grid[b][a] = -1                #๋ฏธ๋ฆฌ -1๋กœ ์ฒดํฌ
    grid[1][1] = 1
    for j in range(1,n+1):
        for k in range(1,m+1):
            if j == k == 1:                #(1,1)์€ 1๋กœ ๋งŒ๋“ค์–ด๋‘๊ณ , 0์ด ๋˜์ง€ ์•Š๋„๋ก
                continue
            if grid[j][k] == -1:           #์›…๋ฉ์ด๋Š” 0์œผ๋กœ ๋งŒ๋“ค์–ด ๋‹ค์Œ ๋ง์…ˆ ๋•Œ ์˜ํ–ฅ๋ผ์น˜์ง€ ์•Š๊ฒŒ
                grid[j][k] = 0
                continue
            grid[j][k] = (grid[j][k-1] + grid[j-1][k])%1000000007   #[a,b] = [a-1,b] + [a,b-1] ๊ณต์‹

    return grid[n][m]
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 9.93MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.04ms, 9.96MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.05ms, 10MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.04ms, 9.99MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.06ms, 10MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (2.23ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (1.17ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (1.08ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (1.99ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.64ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (2.41ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (1.19ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (1.72ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (1.96ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (1.92ms, 10.3MB)
  • ๊ทธ๋ฆฌ๊ณ  ์ด๊ฑธ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ํ•˜๋Š” ์‚ฌ๋žŒ๋„ ์žˆ๋„ค ใ…‹ใ…‹ใ…‹
    • ์™œ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ํ•˜๋ƒ? ๊ทผ๋ฐ ๋‚ด ์ฝ”๋“œ ๋ณด๋‹ค ๋น ๋ฆ„ ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹
def solution(m, n, puddles):
    answer = 0
    info = dict([((2, 1), 1), ((1, 2), 1)])
    for puddle in puddles:
        info[tuple(puddle)] = 0

    def func(m, n):
        if m < 1 or n < 1:
            return 0
        if (m, n) in info:
            return info[(m, n)]
        return info.setdefault((m, n), func(m - 1, n) + func(m, n - 1))
    return  func(m, n) % 1000000007
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.05ms, 10MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.14ms, 10MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.11ms, 10MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (5.32ms, 11.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (2.23ms, 10.7MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (2.70ms, 10.6MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (3.95ms, 11.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (3.34ms, 10.6MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (5.40ms, 11.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (2.53ms, 10.5MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (4.03ms, 11.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (4.21ms, 11.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (4.21ms, 11.1MB)
  • ์•”ํŠผ ์ด๊ฒƒ๋„ ์‚ฝ์งˆ์ด๋‹ˆ๊นŒ ๊ธฐ๋กํ•ด๋‘์ž
    • ๊ฒฝ์šฐ์˜ ์ˆ˜๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ฐฉ๊ฐํ–ˆ๋‹ค๊ฐ€
    • ์›…๋ฉ์ด ์œ„์น˜ ๋•Œ๋ฌธ์— ๊ทธ๋ƒฅ ๊ธธ์ฐพ๊ธฐ๋กœ ํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค๊ฐ€
    • ์ด์ „ ๊ฒฐ๊ณผ๋ฅผ ๋”ํ•˜๋ฉด ๋˜๋Š” ๋”ํ•˜๊ธฐ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ
    • ๊ตฌํ˜„ํ•˜๋‹ค๊ฐ€ ์ฝ”๋“œ์—๋Ÿฌ๋‚˜์„œ ๋‹ค์‹œ ๋‹Œ์ž ๋ณด๋‚ด๊ธฐ๋กœ ๋ฐ”๊พผ ๊ฒฐ๊ณผ๋ฌผ ใ…‹ใ…‹ใ…‹

  • ๋‹ค์Œ ๋ฒˆ์—” ์ข€ ๋” ์ƒ๊ฐํ•ด๋ณด๊ณ  ์ด๋Ÿฐ ์ฝ”๋“œ๋ฅผ ์งœ๋„๋ก ํ•ด๋ณด์ž.
    • ์™„์ „ ๊น”๋”ํ•˜๋„ค...
def solution(m, n, puddles):
    # dp table ์ƒ์„ฑ - padding
    dp = [[0 for i in range(m + 1)] for _ in range(n + 1)]
    # ์ถœ๋ฐœ์ง€์  ๋ฐ ์›…๋ฉ์ด ์ฒ˜๋ฆฌ
    dp[1][1] = 1
    for puddle in puddles:
        dp[puddle[1]][puddle[0]] = -1

    # dp table ์™„์„ฑ
    for i in range(1, n+1):
        for j in range(1, m+1):
            # ๋งŒ์•ฝ ์›…๋ฉ์ด๋ผ๋ฉด
            if dp[i][j] == -1:
                dp[i][j] = 0
                continue
            dp[i][j] += dp[i-1][j] + dp[i][j-1]

    return dp[n][m] % 1000000007
728x90
๋ฐ˜์‘ํ˜•