๐ฆ1806: subtotal
[์๊ณ ๋ฆฌ์ฆ ํ์ด] ๊ฐ๋๊ท
Reviewed by Kade Kang (devkade12@gmail.com)
Reviewed:: 2024-02-19
๋ฌธ์ ํ์ธํ๊ธฐ
ํ์ด
- ์ด์ค for ๋ฌธ์ ์ฌ์ฉํ ๊ฒฝ์ฐ sum์ ์ฒ์๋ถํฐ ์๋ก ๋ํด๊ฐ๋ฉด์ ๊ตฌํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ค.
- ๋๋ฌธ์ sum, ๋์ ํฉ์ ๊ฐฑ์ ํด์ค์ผ ํ๋ค.
- start, end๊ฐ 0๋ถํฐ ์์ํด์ start๋ end๊น์ง, end๋ N๊น์ง๋ก ๋ฒ์๋ฅผ ์ง์ ํ๋ค.
- ์ด ์์์ ๋ชฉํ๊ฐ๊น์ง ๋๋ฌํ์ง ์๋๋ค๋ฉด end๋ฅผ ๋๋ ค ๋ชฉํ๊ฐ์ ๋๊ธธ ์ ์๋๋ก ๋ง๋ค๊ณ ๋ชฉํ๊ฐ์ ๋๋ฌํ๋ฉด start๋ฅผ ๋๋ ค ์ต์ ๋ฒ์๋ฅผ ์ฐพ์ ์ ์๋๋ก ํ๋ค.
#include <iostream>
#define endl '\n'
using namespace std;
int N, S;
int arr[100001];
int res, sum=0;
void Input(){
cin >> N >> S;
for(int i=0; i<N; i++){
cin >> arr[i];
}
}
void Solution(){
res = INT32_MAX;
sum = arr[0];
int start = 0;
int end = 0;
while(start <= end && end < N){
if(sum >= S) res = min(res, end-start+1);
if(sum < S){
end++;
sum += arr[end];
}
else{
sum -= arr[start];
start++;
}
}
if(res == INT32_MAX) cout << 0 << endl;
else cout << res << endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
Input();
Solution();
return 0;
}