-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogression.js
40 lines (37 loc) · 961 Bytes
/
progression.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @desc problem : 연속 부분수열
* @desc site : Olympiad
* @desc level: 4
* @desc solution : 투포인터 알고리즘
*/
/**
* solution
* @param {array} numArray : 수열 배열
* @param {number} max : 합계 숫자
*/
function solution(numArray, max) {
let answer = 0;
let left = 0;
let right = 0;
let sum = 0;
while (right < numArray.length) {
console.log(`left : ${left}, right : ${right}, sum : ${sum}`);
sum += numArray[right++];
if (sum >= max) {
if (sum === max) {
answer++;
}
//연속으로 차감할 경우 대비
while (sum >= max) {
sum -= numArray[left++];
if (sum === max) {
answer++;
}
}
}
console.log('========');
}
return answer;
}
const answer = solution([1, 2, 1, 3, 1, 1, 1, 2], 6);
console.log(answer); //3