강의

멘토링

로드맵

Inflearn Community Q&A

yundosa2's profile image
yundosa2

asked

Introduction to Javascript Algorithm Problem Solving (Coding Test Preparation)

3. Continuous Subsequence 1 (Two Pointers Algorithm)

안녕하세요 강사님! 이렇게 짜도 괜찮을까요?

Written on

·

259

0

<html>

<head>
    <meta charset="UTF-8">
    <title>3. 연속부분수열1</title>
</head>

<body>
    <script>
        function solution(n, m, arr) {
            let answer = lt = 0, rt = 1;
            let sum = arr[lt];
            while (lt < n{
                if (rt === n{
                    if (sum < mbreak;
                    else if (sum > msum -= arr[lt++];
                };
                if (sum < msum += arr[rt++];
                else if (sum > msum -= arr[lt++];
                else {
                    sum = sum - arr[lt+++ arr[rt++];
                    answer++
                };
            }
            return answer;
        }

        let a = [1, 2, 1, 3, 1, 1, 1, 2];
        console.log(solution(8, 6, a));
        let a1 = [1, 1, 1, 1, 1];
        console.log(solution(5, 3, a1));
        let a1 = [1, 1, 1];
        console.log(solution(3, 2, a1));
    </script>
</body>

</html>

다른 질문에서 올려주신 테스트케이스는 전부 잘 나오는것으로 확인했는데, 시간복잡도의 개념이 잘 잡혀있지 않아서 효율성이 있는 코드인지 궁금합니다.

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?

Answer 3

0

highJoon님의 프로필 이미지
highJoon
Questioner

감사합니다. 착오가 있었습니다 ㅜㅜ

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

 보내주신 코드에 예도 답이 정상이 아닙니다.

let a1 = [1, 1, 1, 1, 1];

console.log(solution(5, 3, a1));

let a2 = [1, 1, 1];

console.log(solution(3, 2, a2));

a1 배열의 정답은 3이고, a2 배열의 정답은 2입니다.

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

반례가 존재하는 코드입니다.

let a=[12131113];

console.log(solution(86, a));

답은 4입니다.

yundosa2's profile image
yundosa2

asked

Ask a question