-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbers.js
More file actions
40 lines (35 loc) · 780 Bytes
/
numbers.js
File metadata and controls
40 lines (35 loc) · 780 Bytes
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 : programmers
* @desc level: 2
* @desc solution : 투포인터 알고리즘
*/
/**
* solution
* @param {number} num : 대상 숫자
*/
function solution(num) {
const numbers = [];
let answer = 0;
let sum = 0;
let left = 0;
let right = 0;
//기초배열 생성
for (let index = 0; index < num; index++) {
numbers.push(index + 1);
}
while (right <= num - 1) {
sum += numbers[right];
if (sum === num) {
answer++;
right++;
} else if (sum > num) {
sum = sum - numbers[left] - numbers[right];
left++;
} else {
right++;
}
}
return answer;
}
const answer = solution(15);