개발자 첫걸음/프로그래머스

프로그래머스 [level2] 두 큐 합 같게 만들기

프로아마추어 2022. 10. 3. 16:35

https://school.programmers.co.kr/learn/courses/30/lessons/118667

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

나의 풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function solution(queue1, queue2) {
    const totalArr = [...queue1, ...queue2];
   
// 배열 전체를 2번 순회할 수 있도록 변수 설정
    let maxCount = totalArr.length * 2;
    let start = 0;
    let end = queue1.length;
 
    const sum = (numArr) => numArr.reduce((acc, num) => acc + num, 0);
    let sumValue = sum(totalArr.slice(start, end));
    let targetValue = sum(totalArr) / 2;
    let count = 0;
 
    while (count <= maxCount) {
      if (sumValue < targetValue) {
        sumValue += totalArr[end];
        end++;
      } else if (sumValue > targetValue) {
        sumValue -= totalArr[start];
        start++;
      } else if(sumValue === targetValue) {
        return count;
      }
      count++;
    }
    return -1;
}
cs

 

참고

https://velog.io/@chldmswnl/JS-%EB%91%90-%ED%81%90-%ED%95%A9-%EA%B0%99%EA%B2%8C-%EB%A7%8C%EB%93%A4%EA%B8%B0

 

[JS] 프로그래머스 - 두 큐 합 같게 만들기

투포인터라는 존재를 모르던 몇달 전, 카카오 코테를 보면서 이 문제를 어떻게 풀어야할지 한참을 고민했다.코테가 끝나고 사람들에게 물어봤는데 투포인터로 풀었다길래 감도안잡히고 나중에

velog.io