강의

멘토링

로드맵

인프런 커뮤니티 질문&답변

ssi02014님의 프로필 이미지
ssi02014

작성한 질문수

자바스크립트 알고리즘 문제풀이 입문(코딩테스트 대비)

3. 연속부분수열1(Two Pointers Algorithm)

이런 풀이 괜찮을 까요?

작성

·

153

0

function solution(m, arr) {
  let [left, right] = [0, 1];
  let sum = arr[left];
  let result = 0;

  while (right <= arr.length) {
    if (sum < m) {
      sum += arr[right++];
    } else if (sum > m) {
      sum -= arr[left++];
    } else {
      sum -= arr[left++];
      result++;
    }
  }
  return result;
}

퀴즈

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?

답변 1

0

김태원님의 프로필 이미지
김태원
지식공유자

안녕하세요^^

상관없어 보이지만 영상에서 알려준 방법으로 하시기를 권장합니다.

ssi02014님의 프로필 이미지
ssi02014

작성한 질문수

질문하기