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

 

1. ๋ฌธ์ œ : Link

์‚ผ๊ฐํ˜•์œผ๋กœ ๋Œ์•„๊ฐ€๋ฉด์„œ ์ˆซ์ž๋ฅผ ์ฑ„์›Œ๋„ฃ๋Š” ์‚ผ๊ฐ๋‹ฌํŒฝ์ด ์™„์„ฑํ•˜๋Š” ๋ฌธ์ œ

 

2. ํ’€์ด

๋จผ์ € ์‚ผ๊ฐ ๋‹ฌํŒฝ์ด์˜ ๊ป๋ฐ๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ํšŒ์ „ํ•˜๋ฉด์„œ ๊ฐ’์„ ๋Œ€์ž…ํ•ด์ค€๋‹ค.

 

3. ์ฝ”๋“œ

def solution(n):
    res = [[0] * n for _ in range(n)]
    answer = []
    x, y = -1, 0
    num = 1
    
    for i in range(n):
        for j in range(i, n):
            #down
            if i % 3 == 0:
                x += 1
                
            #right
            elif i % 3 == 1:
                y += 1
                
            #up
            elif i % 3 == 2:
                x -= 1
                y -= 1
            
			#์ €์žฅ  
            res[x][y] = num
            num += 1
            
    for i in res:
        for j in i:
            if j != 0:
                answer.append(j)
    return answer

* ์ฐธ๊ณ ) ์Šคํ‚ฌํŠธ๋ฆฌ for-esle๋ฌธ

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ