문제: 2003번: 수들의 합 2
basic-algo-lecture/workbook/0x14.md at master · encrypted-def/basic-algo-lecture
basic-algo-lecture/workbook/0x14.md at master · encrypted-def/basic-algo-lecture
바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.
github.com
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <limits>
using namespace std;
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
// 일단 st 랑 en 둘 다 0 으로 설정해놩
int st = 0;
int en = 0;
int ans = 0; // 여기는 합이 m 이 되는 경우 카운트
int tmpSum = 0;
while (en < n) {
tmpSum += nums[en];
en++;
// 야 현재 누적합이 m 보다 크거나 같으면 st 값을 증가시켜야함
while (tmpSum >= m) {
if (tmpSum == m) {
ans++;
}
tmpSum -= nums[st];
st++;
}
}
cout << ans;
return 0;
}
아니 똑같은 코드 제출했는데 처음엔 컴파일 에러떴다. 어이가 없다.
아무튼 문제는 그냥 기본적인 투포인터 문제 해결법 이용하면 쉽게 해결할 수 있다.