Created
February 16, 2019 03:02
-
-
Save fpdjsns/de452683e1d872f959aecf4b67bc5833 to your computer and use it in GitHub Desktop.
[leetcode][2003] 수들의 합 2 : https://www.acmicpc.net/problem/2003 부분합. 투포인터
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by fpdjsns | |
// Copyright © 2019 fpdjsns. All rights reserved. | |
// | |
/* | |
* 시간복잡도 : O(N) | |
* 공간복잡도 : O(N) | |
*/ | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int main() { | |
int N, M; | |
cin >> N >> M; | |
vector<int> subSum(N + 1, 0); | |
int tmp; | |
for (int i = 1; i <= N; i++){ | |
cin >> tmp; | |
subSum[i] = subSum[i - 1] + tmp; | |
} | |
int ans = 0; | |
int l = 0, r = 1; | |
while (l <= N && r <= N) { | |
int sum = subSum[r] - subSum[l]; | |
if (sum == M) { | |
int cnt = 1; | |
while (r + 1 <= N && subSum[r] == subSum[r + 1]) { | |
cnt++; r++; | |
} | |
while (l + 1 <= N && subSum[l] == subSum[l +1]) { | |
cnt++; l++; | |
} | |
l++; r++; | |
ans += cnt; | |
} | |
else if(sum < M){ | |
r++; | |
} | |
else { | |
l++; | |
} | |
} | |
cout << ans; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment