문제풀이/백준
백준 1806 - 부분 합
레옹
2022. 1. 2. 21:31
https://www.acmicpc.net/problem/1806
#include <iostream>
#include <queue>
#include <vector>
#include <math.h>
using namespace std;
int N, S, minLength, L, R;
vector<int> num;
int main() {
L = 0;
R = 0;
cin >> N >> S;
minLength = N + 1;
for (int i = 0; i < N; i++) {
int key;
cin >> key;
num.push_back(key);
}
int sum = 0;
L = 0;
R = 0;
sum = num[0];
while (L <= R && R < N) {
if (sum < S) {
R += 1;
if(R < N)
sum += num[R];
}
else {
minLength = min(minLength, R - L + 1);
sum -= num[L++];
}
}
if (minLength == N + 1)
minLength = 0;
cout << minLength << endl;
return 0;
}
투포인터를 이용한 문제