[1806] 부분합

yyeahh·2021년 3월 16일
0

Baekjoon

목록 보기
7/19

[1806] 부분합

[2021.03.16]
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, s, num[100005]={0};

    scanf("%d %d", &n, &s);
    for(int i=0; i<n; i++) 
        scanf("%d", &num[i]);

    int sum = num[0], l = 0, r = 0, cnt = n+1;
    while(l <= r && r < n) {
        if(sum < s) sum += num[++r];
        else {
            cnt = (cnt < r-l+1) ? cnt : r-l+1;
            sum -= num[l++];
        }
    }
    printf("%d", cnt > n ? 0: cnt);
}

0개의 댓글