๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’๊ณต๋ถ€/์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.2] ์‚ผ๊ฐ ๋‹ฌํŒฝ์ด

by rindev 2021. 1. 5.

programmers.co.kr/learn/courses/30/lessons/68645

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‚ผ๊ฐ ๋‹ฌํŒฝ์ด

5 [1,2,12,3,13,11,4,14,15,10,5,6,7,8,9] 6 [1,2,15,3,16,14,4,17,21,13,5,18,19,20,12,6,7,8,9,10,11]

programmers.co.kr

 

 

์Šฌ์Šฌ ์ด๊ฒŒ ๋ ˆ๋ฒจ 2๋ผ๊ณ ?!!?!? ํ•˜๋Š” ๋ฌธ์ œ๋“ค์ด ๋‚˜์˜จ๋‹ค...๐Ÿคฆ‍โ™€๏ธ ๋ฒŒ์จ ๋ ˆ๋ฒจ 3์ด ๋ฌด์„ญ๋‹ค...... 

 

์ขŒํ‘œ๋ฅผ ์ด๋™ํ•ด์„œ ํ’€์–ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐ์€ ํ–ˆ์œผ๋‚˜ n*n ๋ฐฐ์—ด์ด ์•„๋‹ˆ๋ผ

1, 2, 3, 4, 5.... ์ด๋Ÿฐ ์‹์œผ๋กœ ์ด์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค๋ ค๊ณ  ํ–ˆ์Œ ( [ [0], [0,0], [0,0,0], [0,0,0,0] .... ] )

๊ทธ๋Ÿฌ๋‹ค ๋„ˆ๋ฌด ์–ด๋ ค์›Œ์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ์–ด๋–ป๊ฒŒ ํ’€์—ˆ๋‚˜ ์„ ์กฐ๋“ค์˜ ์ง€ํ˜œ๋ฅผ ๋นŒ๋ ธ์Œ..

 

๊ทธ๋ž˜์„œ ์œ„์— ๋งํ–ˆ๋“ฏ์ด n*n ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ์ˆซ์ž๋ฅผ ๋„ฃ์€ ํ›„, 0์„ ์ง€์›Œ๋‚˜๊ฐ€๋ฉด ๋˜๋Š” ๋ฐฉ๋ฒ•์„ ๊นจ๋‹ฌ์•˜๋‹ค.

 

๋ฐ‘์—๋Š” ๋ฌธ์ œ ํ’€๊ณ  ๋ฐ”๋กœ ๊นŒ๋จน์„๊นŒ๋ด ์ •๋ฆฌํ•œ๊ฑฐ!

 

4*4 ๋งŒ ๋ณด๋ฉด ํ—ท๊ฐˆ๋ฆด๊นŒ๋ด ๋” ํฐ ๋ฐฐ์—ด๋„ ์ค€๋น„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ๋ž˜์˜ ๋‚  ์œ„ํ•ด์„œ...

 

์ด์ •๋„ ์นœ์ ˆํ•จ์ด๋ฉด ๋ฏธ๋ž˜์˜ ๋‚ด๊ฐ€ ์•Œ์•„๋ณด๊ฒ ์ง€...? ๐Ÿ˜ข

 

์ž˜ ๋ชจ๋ฅด๊ฒ ์œผ๋ฉด ์ฝ”๋“œ ํ•œ๋ฒˆ ๋ณด๊ณ  ๊ทธ๋ฆผ ํ•œ๋ฒˆ ๋ณด๊ณ  ํ•˜๋ฉด์„œ ์ดํ•ดํ•ด๋ด…์‹œ๋‹ค!

 

 

๐Ÿ™‹‍โ™€๏ธ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

๋”๋ณด๊ธฐ

 

def solution(n):
    answer = []
    
    tmp_list = [[0 for _ in range(n)] for _ in range(n)]
    
    x = 0 #๊ฐ€๋กœ
    y = -1 #์„ธ๋กœ
    
    num = 1
    for i in range(n):
        for j in range(i, n):
            if i % 3 == 0: y += 1 #๋ฐ‘์œผ๋กœ ์ด๋™
            elif i % 3 == 1: x += 1 #์˜ค๋ฅธ์ชฝ ์ด๋™
            elif i % 3 == 2: #์™ผ์ชฝ ์œ„ ๋Œ€๊ฐ์„  ์ด๋™
                x -= 1
                y -= 1
            tmp_list[y][x] = num #์ขŒํ‘œ์— ์ˆซ์ž๋ฅผ ์ž…๋ ฅ
            num += 1
    
    for tmp in tmp_list:
        for t in tmp:
            if t != 0:
                answer.append(t)
    
    return answer

 

๋Œ“๊ธ€