[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ–‰๋ ฌ ํ…Œ๋‘๋ฆฌ ํšŒ์ „ํ•˜๊ธฐ / ํŒŒ์ด์ฌ / Python
Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ–‰๋ ฌ ํ…Œ๋‘๋ฆฌ ํšŒ์ „ํ•˜๊ธฐ / ํŒŒ์ด์ฌ / Python

728x90
๋ฐ˜์‘ํ˜•

 

๐Ÿ’กsolutions )

๐Ÿ’ฌ ๋‹จ์ˆœํžˆ ๊ตฌํ˜„ํ•˜๋Š” ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ

๐Ÿ’ฌ ํ…Œ๋‘๋ฆฌ ์˜์—ญ์˜ ์ˆซ์ž๋“ค์„ ํšŒ์ „์‹œํ‚ค๊ธฐ ์œ„ํ•ด for๋ฌธ์—์„œ ์ธ๋ฑ์Šค ๋ฒ”์œ„๋ฅผ ์ž˜ ๊ตฌ๋ถ„ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•จ

๐Ÿ’ฌ Python์—์„  ๋‘ ๋ณ€์ˆ˜๋ฅผ ํ•œ๋ฒˆ์— swap์ด ๊ฐ€๋Šฅํ•˜๋‹ค -> e.g) a,b = b,a๋กœ a์™€ b๊ฐ’ ๊ตํ™˜ ๊ฐ€๋Šฅ

 

๐Ÿ‘จ‍๐Ÿ’ปcode )

def solution(rows, columns, queries):
    arr = [[0]*columns for _ in range(rows)]
    cnt = 1
    result = []

    for i in range(rows):
        for j in range(columns):
            arr[i][j] = cnt
            cnt += 1

    for r1, c1, r2, c2 in queries:
        min_v = 10001
        before = arr[r1][c1-1]

        # ์œ„
        for i in range(c1-1, c2):
            min_v = min(arr[r1-1][i], min_v)
            before, arr[r1-1][i] = arr[r1-1][i], before

        # ์˜ค๋ฅธ์ชฝ
        for i in range(r1, r2):
            min_v = min(arr[i][c2-1], min_v)
            before, arr[i][c2-1] = arr[i][c2-1], before  
            
        # ์•„๋ž˜
        for i in range(c2-2, c1-2, -1):
            min_v = min(arr[r2-1][i], min_v)
            before, arr[r2-1][i] = arr[r2-1][i], before
            
        # ์™ผ์ชฝ
        for i in range(r2-2, r1-1, -1):
            min_v = min(arr[i][c1-1], min_v)
            before, arr[i][c1-1] = arr[i][c1-1], before
    
        result.append(min_v)
    return result

 

๐Ÿ“Œdescription )

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ–‰๋ ฌ ํ…Œ๋‘๋ฆฌ ํšŒ์ „ํ•˜๊ธฐ

6 6 [[2,2,5,4],[3,3,6,6],[5,1,6,3]] [8, 10, 25] 3 3 [[1,1,2,2],[1,2,2,3],[2,1,3,2],[2,2,3,3]] [1, 1, 5, 3]

programmers.co.kr


  • ํ–‰๋ ฌ ํ…Œ๋‘๋ฆฌ ํšŒ์ „ํ•˜๊ธฐ

๋ฌธ์ œ ์„ค๋ช…

rows x columns ํฌ๊ธฐ์ธ ํ–‰๋ ฌ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ–‰๋ ฌ์—๋Š” 1๋ถ€ํ„ฐ rows x columns๊นŒ์ง€์˜ ์ˆซ์ž๊ฐ€ ํ•œ ์ค„์”ฉ ์ˆœ์„œ๋Œ€๋กœ ์ ํ˜€์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ–‰๋ ฌ์—์„œ ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ๋ฒ”์œ„๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์„ ํƒํ•ด, ํ…Œ๋‘๋ฆฌ ๋ถ€๋ถ„์— ์žˆ๋Š” ์ˆซ์ž๋“ค์„ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „์‹œํ‚ค๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ํšŒ์ „์€ (x1, y1, x2, y2)์ธ ์ •์ˆ˜ 4๊ฐœ๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ, ๊ทธ ์˜๋ฏธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • x1 ํ–‰ y1 ์—ด๋ถ€ํ„ฐ x2 ํ–‰ y2 ์—ด๊นŒ์ง€์˜ ์˜์—ญ์— ํ•ด๋‹นํ•˜๋Š” ์ง์‚ฌ๊ฐํ˜•์—์„œ ํ…Œ๋‘๋ฆฌ์— ์žˆ๋Š” ์ˆซ์ž๋“ค์„ ํ•œ ์นธ์”ฉ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ 6 x 6 ํฌ๊ธฐ ํ–‰๋ ฌ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

์ด ํ–‰๋ ฌ์— (2, 2, 5, 4) ํšŒ์ „์„ ์ ์šฉํ•˜๋ฉด, ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด 2ํ–‰ 2์—ด๋ถ€ํ„ฐ 5ํ–‰ 4์—ด๊นŒ์ง€ ์˜์—ญ์˜ ํ…Œ๋‘๋ฆฌ๊ฐ€ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ์ค‘์•™์˜ 15์™€ 21์ด ์žˆ๋Š” ์˜์—ญ์€ ํšŒ์ „ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ์ฃผ์˜ํ•˜์„ธ์š”.

ํ–‰๋ ฌ์˜ ์„ธ๋กœ ๊ธธ์ด(ํ–‰ ๊ฐœ์ˆ˜) rows, ๊ฐ€๋กœ ๊ธธ์ด(์—ด ๊ฐœ์ˆ˜) columns, ๊ทธ๋ฆฌ๊ณ  ํšŒ์ „๋“ค์˜ ๋ชฉ๋ก queries๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ ํšŒ์ „๋“ค์„ ๋ฐฐ์—ด์— ์ ์šฉํ•œ ๋’ค, ๊ทธ ํšŒ์ „์— ์˜ํ•ด ์œ„์น˜๊ฐ€ ๋ฐ”๋€ ์ˆซ์ž๋“ค ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆซ์ž๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

rowscolumnsqueriesresult

6 6 [[2,2,5,4],[3,3,6,6],[5,1,6,3]] [8, 10, 25]
3 3 [[1,1,2,2],[1,2,2,3],[2,1,3,2],[2,2,3,3]] [1, 1, 5, 3]
100 97 [[1,1,100,97]] [1]

 





 

๋ฐ˜์‘ํ˜•