Cute Running Puppy

algorithm/Programmers

[Programmers] ์—ฐ์†๋œ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ํ•ฉ (Python)

R.silver 2024. 7. 16. 15:39
๋ฐ˜์‘ํ˜•

๐Ÿ“–๋ฌธ์ œ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์—ฐ์†๋œ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ํ•ฉ | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์Šค์ฟจ (programmers.co.kr)

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

โœจํ•ต์‹ฌ ๋‚ด์šฉ 

ํ•ฉ์ด k์ธ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค์™€ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜
- ํ•ฉ์ด k ์ธ ๋ถ€๋ถ„ ์ˆ˜์—ด์ด ์—ฌ๋Ÿฌ ๊ฐœ ์ผ ๊ฒฝ์šฐ ๊ธธ์ด๊ฐ€ ์งง์€ ์ˆ˜์—ด ๋ฐ˜ํ™˜
- ๊ธธ์ด๊ฐ€ ์งง์€ ์ˆ˜์—ด์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ ์‹œ์ž‘ ์ธ๋ฑ์Šค๊ฐ€ ์ž‘์€ ์ˆ˜์—ด ๋ฐ˜ํ™˜

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

 

๐Ÿค”ํ•ด๊ฒฐ ์•„์ด๋””์–ด 

์œ ํ˜•: ํˆฌํฌ์ธํ„ฐ

1. start ํฌ์ธํ„ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ end ํฌ์ธํ„ฐ๋ฅผ ์ด๋™์‹œํ‚จ๋‹ค (for ๋ฌธ)

2. ๋งŒ์•ฝ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ํ•ฉ์ด k๋ผ๋ฉด (์‹œ์ž‘ ์ธ๋ฑ์Šค, ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค, ๋ฐฐ์—ด์˜ ๊ธธ์ด)์˜ ๊ฐ’์„ ans์— ์ถ”๊ฐ€ํ•œ๋‹ค 

3. ๋งŒ์•ฝ ๋ถ€๋ถ„ ์ˆ˜์—ด์ด ํ•ฉ์ด k ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด, ํ˜„์žฌ end ์œ„์น˜์˜ ๊ฐ’์„ ๋”ํ•ด์ฃผ๊ณ  end ๊ฐ’์„ ์ด๋™์‹œํ‚จ๋‹ค

4. ๋งŒ์•ฝ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ํ•ฉ์ด k ๋ณด๋‹ค ํฌ๋‹ค๋ฉด, while ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๊ณ  ๋‹ค์Œ start ๋ถ„๊ธฐ๋กœ ์ด๋™ํ•œ๋‹ค 

5. ๊ธธ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ ๋’ค ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค

 

๐ŸŸก์ฃผ์˜ํ•  ์ 

- while ์กฐ๊ฑด, ์ธ๋ฑ์Šค ๋ฒ”์œ„๋ฅผ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค 


โœ…์ •๋‹ต ์ฝ”๋“œ (Python)

๐ŸŸก sol1) start๋ฅผ for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋™

def solution(sequence, k):
    n = len(sequence)
    end = 0
    total = 0
    ans = []
    
    # start๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ธ๋ฑ์Šค ์ด๋™ 
    for start in range(n):
        while total < k and end < n:
            total += sequence[end] # ํ˜„์žฌ ๊นŒ์ง€ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ํ•ฉ ๊ณ„์‚ฐ
            end += 1 # ์ธ๋ฑ์Šค ์ด๋™ 
            
        # ์กฐ๊ฑด ๋งŒ์กฑ์‹œ 
        if total == k:
            ans.append((start, end -1, end - 1 - start)) # start, end, ๊ธธ์ด
        
        # total > k or end >= n ์ด๋ฉด 
        total -= sequence[start] # start ์ด๋™ ์ „ ๊ฐ’ ๋นผ์ฃผ๊ธฐ 
    
    # ๊ธธ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ 
    ans.sort(key = lambda x: x[2])
    # ์‹œ์ž‘ ์ธ๋ฑ์Šค์™€ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋งŒ ๋ฐ˜ํ™˜
    return ans[0][:2]

 

๐ŸŸก sol2) start์™€ end๋ฅผ ์ง์ ‘ ์ด๋™ 

def solution(sequence, k):
        n = len(sequence)
        start, end, sum = 0, 0, sequence[0]
        ans = [0, float('inf')]
        
        while start <= end and end < n:
            # 1. ํ˜„์žฌ ์ˆ˜์—ด์˜ ํ•ฉ < k
            # end += 1
            if sum < k and end < n-1: # end += 1 ํ•  ๊ฒƒ์ด๊ธฐ์— ๋ฒ”์œ„ ์ฒดํฌ
                end += 1
                sum += sequence[end]
            
            # 2. ํ˜„์žฌ ์ˆ˜์—ด์˜ ํ•ฉ > k
            # start += 1
            elif sum > k:
                sum -= sequence[start]
                start += 1
                
            # 3. ํ˜„์žฌ ์ˆ˜์—ด์˜ ํ•ฉ == k
            elif sum == k:
                # ๊ธฐ์กด์— ๋“ฑ๋ก๋œ ์ˆ˜์—ด์˜ ๊ธธ์ด์™€ ๋น„๊ต 
                # ๊ฐ™์œผ๋ฉด ์ธ๋ฑ์Šค ์ž‘์€ ๊ฒƒ์„ ๋“ฑ๋กํ•ด์•ผ ํ•˜๊ธฐ์— ๊ฐฑ์‹ ํ•˜์ง€ ์•Š๋Š”๋‹ค
                if ans[1] - ans[0] > end - start: 
                    ans = [start, end]
                
                # sum = k๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๋” ์งง์€ ์ˆ˜์—ด์ด ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ์— start += 1 
                sum -= sequence[start]
                start += 1
            
            # 4. (1, 2, 3) ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ–ˆ๋Š”๋ฐ end๋ฅผ ๊ฐฑ์‹ ํ•˜์ง€ ๋ชปํ•ด ๋ฌดํ•œ๋ฃจํ”„์ธ ๊ฒฝ์šฐ ๋ฐฉ์ง€
            else:
                if end == n-1:
                    break
                
        return ans
๋ฐ˜์‘ํ˜•