Skip to content

[JooKangsan] 25.01.16#24

Merged
JooKangsan merged 15 commits intomainfrom
JooKangsan
Jan 23, 2025
Merged

[JooKangsan] 25.01.16#24
JooKangsan merged 15 commits intomainfrom
JooKangsan

Conversation

@JooKangsan
Copy link
Collaborator

배열

md 파일로 추가했습니다!

📌 푼 문제

문제이름 문제링크
순서쌍의 개수 https://school.programmers.co.kr/learn/courses/30/lessons/120836
점의 위치 구하기 https://school.programmers.co.kr/learn/courses/30/lessons/120841
로그인 성공? https://school.programmers.co.kr/learn/courses/30/lessons/120883
특이한 정렬 https://school.programmers.co.kr/learn/courses/30/lessons/120880
카드 뭉치 https://school.programmers.co.kr/learn/courses/30/lessons/159994
공원 산책 https://school.programmers.co.kr/learn/courses/30/lessons/172928
햄버거 만들기 https://school.programmers.co.kr/learn/courses/30/lessons/133502


📝 간단한 풀이 과정

왠만해서 모든 문제를 큐로 풀고 싶었는데 마지막 버거 만들기는 큐보다는 스택이 더 맞아서 스택으로 풀었습니다....

순서쌍의 개수

  • 큐를 사용해 n의 약수를 찾고 순서쌍의 개수를 계산했습니다.
function solution(n) {
  let queue = [];
  let count = 0;
  for(let i = 1; i <= Math.sqrt(n); i++) {
      if(n % i === 0) {
          queue.push(i);
      }
  }
  while(queue.length > 0) {
      const num = queue.shift();
      const pair = n / num;

      if(num === pair) {
          count++;
      } else {
          count += 2;
      }
  }

  return count;
}

점의 위치 구하기

  • 큐에서 x,y 좌표를 꺼내 어떤 사분면인지 반환했습니다.
function solution(dot) {
  let queue = [...dot]

  const x = queue.shift();
  const y = queue.shift();

  if(x > 0 && y > 0) return 1;
  if(x < 0 && y > 0) return 2;
  if(x < 0 && y < 0) return 3;
  return 4;
}

로그인 성공?

  • 큐에서 하나씩 DB 정보를 꺼내며 ID/PW 매칭이 맞는지 확인하고 매칭이맞으면 "login"을 반환했습니다.
function solution(id_pw, db) {
  let queue = [...db];

  const [id, pw] = id_pw;
  let hasId = false;

  while(queue.length > 0) {
      const [dbId, dbPw] = queue.shift();

      if(dbId === id) {
          hasId = true;
          if(dbPw === pw) {
              return "login";
          }
      }
  }

  return hasId ? "wrong pw" : "fail";
}

특이한 정렬

  • 큐를 이용해 각 숫자와 n과의 거리를 계산하고 정렬했습니다.
function solution(numlist, n) {
  let queue = [...numlist];
  let result = [];

  queue.sort((a, b) => {
      const A = Math.abs(n - a);
      const B = Math.abs(n - b);
      if(A === B) {
          return b - a;
      }
      return A - B;
  });

  while(queue.length > 0) {
      result.push(queue.shift());
  }

  return result;
}

카드 뭉치

  • 두 개의 큐에서 순서대로 카드를 뽑아 목표 배열을 생성 할 수 있는지 확인했습니다.
function solution(cards1, cards2, goal) {
  let queue1 = [...cards1];
  let queue2 = [...cards2];

  for(let word of goal) {
      if(queue1.length > 0 && word === queue1[0]) {
          queue1.shift();
      }
      else if(queue2.length > 0 && word === queue2[0]) {
          queue2.shift();
      }
      else {
          return "No";
      }
  }
  return "Yes";
}

공원산책

  • 큐에서 명령어를 하나씩 꺼내 강아지의 이동 가능 여부를 확인하고 가능하면 위치 이동 불가능하면 이동하지 않고 그대로 반환했습니다.
function solution(park, routes) {
  let queue = [...routes];

  let [x, y] = park.map((s, i) => [s.indexOf('S'), i]).find(v => v[0] > -1);

  let [h, w] = [park.length, park[0].length];

  while(queue.length) {
      let [d, n] = queue.shift().split(' ');
      let [nx, ny] = [x, y];
      let go = true;

      for(let i = 0; i < n; i++) {
          if(d === 'N') ny--;
          if(d === 'S') ny++;
          if(d === 'W') nx--;
          if(d === 'E') nx++;

          if(ny < 0 || ny >= h || nx < 0 || nx >= w || park[ny][nx] === 'X') {
              go = false;
              break;
          }
      }

      if(go) [x, y] = [nx, ny];
  }

  return [y, x];
}

햄버거 만들기

  • 기존에 queue를 쓰려고 했으나 . 매 반복마다 stack.push(queue.shift())에서 o(n)이 동작해서 런타임오류가 떠서 스택으로 변경했습니다.
  • 재료를 스택에 쌓으며 햄버거 완성(1,2,3,1)의 여부를 확인하고 카운트를 증가시킵니다 . 그뒤로 완성된 햄버거를 스택에서 제거합니다.
  • 재료를 스택에 하나씩 쌓으면서 마지막 4개의 재료가 햄버거 순서(1,2,3,1)와 일치하는지 확인하고, 일치하면 해당 재료 4개를 스택에서 제거하고 햄버거 카운트를 1 증가시킵니다."
function solution(ingredient) {
    let stack = [];
    let count = 0;
    let i = 0;

    while(i < ingredient.length) {
        stack.push(ingredient[i]);

        if(stack.length >= 4) {
            const len = stack.length;
            if(stack[len-4] === 1 && 
               stack[len-3] === 2 && 
               stack[len-2] === 3 && 
               stack[len-1] === 1) {
                stack.splice(-4);
                count++;
            }
        }
        i++;
    }

    return count;
 }

@JooKangsan JooKangsan self-assigned this Jan 16, 2025
@JooKangsan JooKangsan changed the title Joo kangsan [JooKangsan] 25.01.016 Jan 16, 2025
@JooKangsan JooKangsan changed the title [JooKangsan] 25.01.016 [JooKangsan] 25.01.16 Jan 16, 2025
@JooKangsan JooKangsan requested review from tkddbs587 and removed request for geniusgo January 16, 2025 12:57
Copy link
Collaborator

@Moonjonghoo Moonjonghoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한주간 고생하셨습니다!

}

return result;
} No newline at end of file
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배열복사를안하고 numlist 를 그대로정렬했는데 복사해서 처리하셔서 신기했습니다!
따로 복사해서 처리하신 이유가있을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이번 문제를 다 복사해서 풀다보니 너무 자연스럽게 복사부터한거같네요... 따로 복사를 안해도 결과는 같을거 같아요!! 이렇게 듣고보니 불필요한 코드인거같아요!! 감사합니다!

Copy link
Collaborator

@tkddbs587 tkddbs587 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

보면서 많이 배워갑니다 수고하셨습니다!

@JooKangsan JooKangsan merged commit 0a5d8e1 into main Jan 23, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants