#include <iostream>
#include <algorithm>
#include <vector>
int N = 0;
int S = 0;
int iMax = 0;
int main()
{
std::cin >> N >> S;
int iCount = N;
std::vector<int> vec(N);
for (int i = 0; i < N; ++i)
{
std::cin >> vec[i];
iMax += vec[i];
}
if (iMax < S)
{
std::cout << 0;
return 0;
}
int iStart = 0;
int iLast = 0;
int iSum = 0;
while (iStart <= iLast)
{
if (iSum >= S)
{
iCount = std::min(iCount, iLast - iStart);
iSum -= vec[iStart];
++iStart;
}
else if (iLast == N)
break;
else
{
iSum += vec[iLast];
++iLast;
}
}
std::cout << iCount;
return 0;
}
투포인터로 풀면 되는 문제
앞부터 두개의 포인터로 시작해서 뒤쪽 포인터를 계속해서 추가하면서 최소값을 넘어간 갯수를 기억하고
앞 포인터를 출발시키고 쭉 이어가다가 뒤쪽 포인터가 맨 마지막일 경우 끝낸다.
'오늘의 알고리즘' 카테고리의 다른 글
[C++]백준 2531번 회전 초밥 (0) | 2022.04.29 |
---|---|
[C++] 백준 12919번 A와 B 2 (0) | 2022.04.28 |
[C++] 백준 1253번 좋다 (0) | 2022.04.27 |
[C++]백준 13549번 숨바꼭질3 (0) | 2022.04.25 |
[C++]백준 10021번 Watering the Fields (0) | 2022.04.22 |