해결된 질문
작성
·
208
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(n, k, arr) {
let lt = rt = currentSum = 0;
let max = Number.MIN_SAFE_INTEGER;
while (rt < n) {
currentSum += arr[rt];
if (rt - lt + 1 === k) {
max = Math.max(max, currentSum);
currentSum -= arr[lt++];
}
rt++;
}
return max;
}
let a = [12, 15, 11, 20, 25, 10, 20, 19, 13, 15];
console.log(solution(3, a));
</script>
</body>
</html>
감사합니다.
함수 밖에서 매개변수 n에 arr.length를 할당해서 그렇습니다.