๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Programming/Coding Test

[Level 2] ์‚ผ๊ฐ ๋‹ฌํŒฝ์ด (Python)

 

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

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

 

def solution(n):
    answer = []
    
    mtx = [[0]*(i+1) for i in range(n)]

    i = -1
    j = 0
    num = 0

    while True:
        for _ in range(n):
            num += 1
            i += 1
            mtx[i][j] = num 
        n -= 1

        if n == 0:
            break
        
        for _ in range(n):
            num += 1
            j += 1
            mtx[i][j] = num
        n -= 1

        if n == 0:
            break

        for _ in range(n):
            i -= 1
            j -= 1
            num += 1
            mtx[i][j] = num
        n -= 1

        if n == 0:
            break
        
    for i in mtx:
        for j in i:
            answer.append(j)
            
    return answer

 

โ–ท ๊ฐ€์žฅ ์œ„์— ์œ„์น˜ํ•œ ๊ผญ์ง€์ ์„ ๊ธฐ์ค€์œผ๋กœ ํ…Œ๋‘๋ฆฌ์— ํ• ๋‹น๋˜๋Š” ์ˆซ์ž๋Š” ์ฃผ์–ด์ง„ n์—์„œ ํ•˜๋‚˜์”ฉ ์ค„์–ด๋“œ๋Š” ๊ทœ์น™์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์™ผ์ชฝ ์œ„ ์ฒซ ๋ฒˆ์งธ ํ…Œ๋‘๋ฆฌ๋Š” n, ๋‹ค์Œ ์•„๋ž˜ ํ…Œ๋‘๋ฆฌ๋Š” n - 1, ๊ทธ ๋‹ค์Œ ์˜ค๋ฅธ์ชฝ ์œ„ ํ…Œ๋‘๋ฆฌ๋Š” n - 2๋งŒํผ ํ• ๋‹น๋œ๋‹ค.

 

โ–ท ์ด๋•Œ, ์ˆซ์ž๊ฐ€ ํ• ๋‹น๋  ๋•Œ๋งˆ๋‹ค 1 ์”ฉ ์ฆ๊ฐ€ํ•˜์—ฌ์•ผ ๋ฌธ์ œ์˜ ๊ทœ์น™์„ ๋งŒ์กฑํ•˜๊ฒŒ ๋œ๋‹ค. 

 

โ–ท ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด i์™€ j๋Š” ํ• ๋‹น๋˜๋Š” ์œ„์น˜, num์€ ํ• ๋‹น๋  ๋•Œ์˜ ์ˆซ์ž๋กœ ์ •์˜ํ•˜๊ณ , ํ•˜๋‚˜์”ฉ ์ค„์–ด๋“  n์ด 0์ด ๋˜๋Š” ๊ฒฝ์šฐ ๋ฉˆ์ถ”๋„๋ก ํ•œ๋‹ค.

 

โ–ถ for [Indicator] in [Array] ๋ฌธ์— N์ฐจ์› ๋ฐฐ์—ด์„ ๋„ฃ๊ฒŒ ๋˜๋ฉด, ๋ฐฐ์—ด์˜ ์›์†Œ์ธ N-1์ฐจ์› ๋ฐฐ์—ด์„ ์ˆœ์„œ๋Œ€๋กœ Indicator์— ํ• ๋‹นํ•œ๋‹ค.

'Programming > Coding Test' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Level 2] H-Index (Python)  (0) 2020.10.05
[Level 2] ์œ„์žฅ (Python)  (0) 2020.09.30
[Level 2] ๊ด„ํ˜ธ ๋ณ€ํ™˜ (Python)  (0) 2020.09.30
[Level 2] ์นดํŽซ (Python)  (0) 2020.09.24
[Level 2] ๋‹ค์Œ ํฐ ์ˆซ์ž (Python)  (0) 2020.09.24