인프런 커뮤니티 질문&답변
작성자 없음
작성자 정보가 삭제된 글입니다.
이렇게 짜도 될까요? for문 하나에서 만들어 보았습니다.
작성
·
250
0
function solution(M, arr) {
let i = 0,
sum = 0,
q = 0;
answer = 0;
for (i; i < arr.length; i++) {
sum += arr[i];
if (sum < M) {
answer++;
} else {
if (sum === M) answer++;
q++;
i = q - 1;
sum = 0;
}
}
return answer;
}
let M = 5;
let arr = [1, 3, 1, 2, 3];
console.log(solution(M, arr));
퀴즈
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?





