๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 1806 - ๋ถ€๋ถ„ํ•ฉ

by HandHand 2021. 3. 8.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 1806๋ฒˆ

ํ’€์ด ๊ณผ์ •

ํˆฌ ํฌ์ธํ„ฐ ๋ฅผ ํ™œ์šฉํ•ด์„œ ์—ฐ์†๋œ ์ˆ˜์—ด์˜ ํ•ฉ์ด S ์ด์ƒ์ธ ๊ตฌ๊ฐ„ ์ค‘ ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ์งง์€ ๊ตฌ๊ฐ„์„ ์ฐพ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๋ชจ๋“  ์ˆซ์ž๊ฐ€ ์–‘์˜ ์ •์ˆ˜์ด๋ฏ€๋กœ ๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ head, tail ์„ ์‚ฌ์šฉํ•ด์„œ ํ•ฉ์ด S ๋ณด๋‹ค ์ปค์งˆ ๊ฒฝ์šฐ head ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ 

S ๋ณด๋‹ค ์ž‘์•„์งˆ ๊ฒฝ์šฐ tail ์„ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys

N, S = list(map(int, sys.stdin.readline().split()))
nums = list(map(int, sys.stdin.readline().split()))


def solution():
    answer = sys.maxsize
    head, tail = 0, 1
    acc = nums[head] + nums[tail]

    while head <= tail:
        if acc < S:
            tail += 1
            if tail < N:
                acc += nums[tail]
            else:
                break
        else:
            answer = min(answer, tail - head + 1)
            acc -= nums[head]
            head += 1

    return answer if answer != sys.maxsize else 0


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 2458 - ํ‚ค ์ˆœ์„œ  (0) 2021.03.08
BOJ 18111 - ๋งˆ์ธํฌ๋ž˜ํ”„ํŠธ  (0) 2021.03.08
BOJ 6186 - Best Grass  (0) 2021.03.08
BOJ 8972 - ๋ฏธ์นœ ์•„๋‘์ด๋…ธ  (0) 2021.03.08
BOJ 19941 - ํ–„๋ฒ„๊ฑฐ ๋ถ„๋ฐฐ  (0) 2021.03.08

๐Ÿ’ฌ ๋Œ“๊ธ€