작성
·
152
0
밑에 코드처럼 투포인터를 이용해서 풀었는데 문제 되는점이 있을지 궁금합니다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
function solution(k, arr) {
let answer = 0;
let lt = 0;
let sum = 0;
for (let rt = 0; rt < arr.length; rt++) {
sum += arr[rt];
if (rt - lt === k - 1) {
answer = Math.max(sum, answer);
sum -= arr[lt++];
}
}
return answer;
}
let a = [12, 15, 11, 20, 25, 10, 20, 19, 13, 15];
console.log(solution(3, a));
</script>
</body>
</html>