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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.3] ํ’์„  ํ„ฐํŠธ๋ฆฌ๊ธฐ

by rindev 2021. 4. 20.

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ’์„  ํ„ฐํŠธ๋ฆฌ๊ธฐ

[-16,27,65,-2,58,-92,-71,-68,-61,-33] 6

programmers.co.kr

 

์ฒ˜์Œ์—๋Š” ์ด๊ฑธ ์–ด์ฉŒ๋ผ๋Š”๊ฑฐ์•ผ.. ์‹ถ์—ˆ๋‹ค๐Ÿ˜‚

๊ทธ๋ž˜์„œ ์ผ๋‹จ ํ•œ๋ฒˆ ๊ฒ€์ƒ‰ํ•ด์„œ ์ ‘๊ทผ๋ฒ•์„ ์•Œ์•„๋‚ธ ํ›„ ๊ณ„์‚ฐํ•ด๋ดค๋‹ค...!!!

 

์ ‘๊ทผ๋ฒ•์€ ์ง€๊ธˆ ์ˆซ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ, ์™ผ์ชฝ/์˜ค๋ฅธ์ชฝ์—์„œ ์ตœ์†Ÿ๊ฐ’์„ ํ™•์ธํ•˜์—ฌ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

์ž…์ถœ๋ ฅ์˜ˆ์ œ 2๋ฅผ ๋ณด๋ฉด ํ‘œ์ฒ˜๋Ÿผ ์ •๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋‹ค!

์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ์˜ ์ตœ์†Œ๊ฐ’์„ ์ €์žฅํ•  small_nums ๋ผ๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ ๋‹ค์Œ for๋ฌธ์„ ๋Œ๋ฉด์„œ ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด์ค€ ๊ฒƒ์ด๋‹ค.

 

58๊ณผ -68์„ ์˜ˆ๋กœ ๋“ค์–ด ์„ค๋ช…ํ•˜๋ฉด...

58์„ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ(-16, 27, 65, -2) ์—์„œ ์ตœ์†Œ๊ฐ’์€ -16์ด๊ณ ,
์˜ค๋ฅธ์ชฝ(-92, -71, -68, -61, -33)์—์„œ ์ตœ์†Œ๊ฐ’์€ -92์ด๋‹ค!
-68์„ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ(-16, 27, 65, -2, 58, -92, -71) ์—์„œ ์ตœ์†Œ๊ฐ’์€ -92์ด๊ณ ,
์˜ค๋ฅธ์ชฝ (-61, -33) ์€ ๋‘˜ ๋‹ค -68๋ณด๋‹ค ํฌ๋ฏ€๋กœ, ์ตœ์†Œ๊ฐ’์„ -68๋กœ ๋ณ€๊ฒฝํ•œ ํ›„ ์ €์žฅํ•œ๋‹ค!

 

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

๋”๋ณด๊ธฐ
def solution(a):
    answer = 0
    small_nums= [[0, 0] for _ in range(len(a))]

    INF = int(10e9)
    left = INF
    right = INF
    
    #์™ผ์ชฝ ํ™•์ธ
    for i in range(len(a)):
        if left > a[i]:
            left = a[i]
        small_nums[i][0] = left
    
    #์˜ค๋ฅธ์ชฝ ํ™•์ธ
    for i in range(len(a)-1, -1, -1): #๋งจ ์˜ค๋ฅธ์ชฝ์—์„œ๋ถ€ํ„ฐ <- ์ด์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ ์™€์•ผ ํ•˜๋ฏ€๋กœ
        if right > a[i]:
            right = a[i]
        small_nums[i][1] = right
    
    for i in range(len(a)):
        if a[i] <= small_nums[i][0] or a[i] <= small_nums[i][1]:
            answer += 1
    print(small_nums)
    
    return answer

 

๋Œ“๊ธ€