강의

멘토링

커뮤니티

Inflearn Community Q&A

choije4014677's profile image
choije4014677

asked

Introduction to Javascript Algorithm Problem Solving (Coding Test Preparation)

3. Continuous Subsequence 1 (Two Pointers Algorithm)

이중 반복문 없이 이렇게 작성해도 괜찮을까요?

Written on

·

318

1

function solution(m, arr) {
  let sum = (cases = 0);
  let i = (j = 0);

  while (j < arr.length) {
    if (sum + arr[j] < m) {
      sum += arr[j++];
      continue;
    } else if (sum + arr[j] === m) {
      sum += arr[j++];
      cases++;
    }
    sum -= arr[i++];
  }

  return cases;
}

이중 반복문으로 처리하면 depth가 깊어지는 것 같아 while문 안에 또 while문을 쓰지는 않았습니다. 몇개의 테스트를 해봤을 때는 오류가 없는 것 같은데 혹시 놓친 부분이 있을까요?

javascript코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

네. 잘 하신 코드입니다.

choije4014677's profile image
choije4014677

asked

Ask a question