Skip to content

[unn04012] 25.01.16#29

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

[unn04012] 25.01.16#29
JooKangsan merged 15 commits intomainfrom
unn04012

Conversation

@unn04012
Copy link
Collaborator

[큐]

📌 푼 문제


📝 간단한 풀이 과정

순서쌍의 개수

  • �n의 약수의 갯수를 이용했습니다.
function solution(n) {
  const divisors = [];

  for (let i = 1; i <= n; i++) if (n % i === 0) divisors.push(i);
  return divisors.length;
}

점의 위치 구하기

  • �각 분면의 위치 특성을 이용해서 구했습니다.
function solution(dot) {
  let answer = 0;
  const [x, y] = dot;

  const quadrant = [
    [1, 1],
    [-1, 1],
    [-1, -1],
    [1, -1],
  ];

  for (let i = 0; i < quadrant.length; i++) {
    const [x, y] = quadrant[i];

    const newX = x * dot[0];
    const newY = y * dot[1];

    if (newX > 0 && newY > 0) {
      answer = i + 1;
      break;
    }
  }

  return answer;
}

카드 뭉치

  • �flag를 이용해서 두 큐에서 특정 단어를 처리 할 수 있는지 판별했습니다.
  • 연산량을 줄이기 위해서 reverse() 후 pop()을 이용하여 처리하였습니다.
function solution(cards1, cards2, goal) {
  const reversedCard1 = cards1.reverse();
  const reversedCard2 = cards2.reverse();

  for (const word of goal) {
    let flag = false;
    if (reversedCard1[reversedCard1.length - 1] === word) {
      reversedCard1.pop();
      flag = true;
    } else if (reversedCard2[reversedCard2.length - 1] === word) {
      reversedCard2.pop();
      flag = true;
    }
    if (!flag) return 'No';
  }

  return 'Yes';
}

햄버거 만들기

  • 큐의 길이가 4이상일 경우 배열의 끝부터 길이 4까지의 값이 1231일 경우 햄버거를 만들 수 있다고 판별했습니다.
function solution(ingredients) {
  let answer = 0;
  const queue = [];

  for (const ingredient of ingredients) {
    queue.push(ingredient);
    if (queue.length >= 4) {
      const hamburger = queue.slice(-4).join('');

      if (hamburger === '1231') {
        answer++;
        for (let i = 0; i < 4; i++) queue.pop();
      }
    }
  }

  return answer;
}

프로세스

  • �풀이를 참고했습니다..! (https://html-jc.tistory.com/653)
  • location이 처음 인덱스 순서와 일치하는 경우 실행되었던 큐의 길이를 이용하여 몇 번째로 실행되는지 판별합니다.
function solution(priorities, location) {
  let answer = 1;
  const queue = [];

  const prioritiesWithIndex = priorities.map((priority, index) => ({ priority, index }));

  while (prioritiesWithIndex.length) {
    const { priority, index } = prioritiesWithIndex.shift();

    const checkPriority = prioritiesWithIndex.findIndex((e) => e.priority > priority);

    // 우선순위가 더 큰 프로세스가 없으면
    if (checkPriority === -1) {
      if (location === index) return queue.length + 1;
      queue.push(priority);
      continue;
    }

    prioritiesWithIndex.push({ priority, index });
  }

  return answer;
}

Copy link
Collaborator

@JustDevRae JustDevRae left a comment

Choose a reason for hiding this comment

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

수고 많으셨습니다!

Comment on lines +2 to +3
const reversedCard1 = cards1.reverse();
const reversedCard2 = cards2.reverse();
Copy link
Collaborator

Choose a reason for hiding this comment

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

reverse & pop을 사용하면 shift를 사용하는 것보다 시간 복잡도 면에서 더 효율적이군요. 배워갑니다!

Copy link
Collaborator

Choose a reason for hiding this comment

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

이 코드에서 queue 배열이 딱히 우선순위 값을 담는 것 말고 사용이 되지 않는 것 같아요. 혹시 다른 의도가 있었는지 궁금합니다!

Copy link
Collaborator

@JooKangsan JooKangsan left a comment

Choose a reason for hiding this comment

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

이번주도 화이팅입니다!! 코드 보며 한번더 고민하고 찾아보는 시간을 가진거같아서 좋네요!

@@ -0,0 +1,27 @@
function solution(dot) {
let answer = 0;
const [x, y] = dot;
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 코드는 사용안하는거같아요!

Copy link
Collaborator

Choose a reason for hiding this comment

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

단순히 길이를 반환하는 거라면 const divisors = [];를 count 로 가져가는게 메모리적으로 더 좋을것 같아요

function solution(n) {
  let count = 0;

  for (let i = 1; i <= n; i++) if (n % i === 0) count++;
  return count;
}

console.log(solution(100));

while (prioritiesWithIndex.length) {
const { priority, index } = prioritiesWithIndex.shift();

const checkPriority = prioritiesWithIndex.findIndex((e) => e.priority > priority);
Copy link
Collaborator

Choose a reason for hiding this comment

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

some 메소드를 고려해 보시는건 어떨까요??
findIndex는 전체 배열을 순회하지만, some은 조건을 만족하는 요소를 찾으면 즉시 반환합니다!!

@JooKangsan JooKangsan merged commit 61fb053 into main Jan 23, 2025
3 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