Inflearn Community Q&A
while문 하나만 사용해서 구현해보았는데 검수 부탁드립니다!
Written on
·
283
1
<html>
<head>
<meta charset="UTF-8">
<title>출력결과</title>
</head>
<body>
<script>
function solution(m, arr) {
let answer = 0;
let sum = 0;
let li = 0;
let ri = 0;
while (true) {
if (sum < m) {
sum += arr[ri];
ri++;
} else {
sum -= arr[li];
li++;
}
if (sum === m) {
answer++;
}
// ri가 arr.length 이상이고, sum이 m보다 작아지면 반복문 종료
if (ri >= arr.length && sum < m) break;
}
/**/
return answer;
}
console.log(solution(6, [1, 2, 1, 3, 1, 1, 1, 2]));
console.log(solution(6, [1, 1, 1, 2, 4]));
console.log(solution(2, [1, 1, 1]));
</script>
</body>
</html>
안녕하세요, 선생님! 강의 잘 듣고 있습니다.
while문 하나만 이용하여 O(n)으로 구현해보았는데
오류가 있는지 확인해주시면 감사드리겠습니다!
코테 준비 같이 해요! javascript
Quiz
What is the main reason why two-pointer or sliding window techniques are more efficient than nested loops?
Because it uses less memory?
Is it because the code is shorter?
Is it because it achieves O(N) time complexity in most cases?
Is it because it's not affected by the input data size?





