Skip to content

[moonjonghoo] 05.01.19#30

Merged
JooKangsan merged 12 commits intomainfrom
jh
Jan 23, 2025
Merged

[moonjonghoo] 05.01.19#30
JooKangsan merged 12 commits intomainfrom
jh

Conversation

@Moonjonghoo
Copy link
Collaborator

📌 푼 문제

문제이름 문제링크
순서쌍의 개수 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/42587


📝 간단한 풀이 과정

순서쌍의개수

주어진 정수 n의 약수 중에서 (a, b) 형태로 곱했을 때 a * b = n이 되는 모든 순서쌍의 개수를 구하는 문제입니다.

function solution(n) {
  let count = 0;

  for (let i = 1; i <= Math.sqrt(n); i++) {
    if (n % i === 0) {
      count++; // i가 약수
      if (i !== n / i) {
        count++; // i가 n의 제곱근이 아니면 나머지 짝도 추가
      }
    }
  }

  return count;
}

핵심 아이디어
1부터 √n까지의 숫자만 탐색하여 약수를 확인하고, 각 약수에 대해 (a, b) 쌍을 찾는 방식으로 해결


점의 위치 구하기

function solution(dot) {
  const [x, y] = dot;

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

핵심 아이디어
x, y의 부호를 기준으로 사분면을 결정
각각의 경우를 조건문으로 분기하여 사분면 번호를 반환하도록 구현했습니다.


로그인 성공?

function solution(id_pw, db) {
  const [id, pw] = id_pw;

  for (const [dbId, dbPw] of db) {
    if (id === dbId) {
      return pw === dbPw ? "login" : "wrong pw";
    }
  }

  return "fail";
}


특이한 정렬

function solution(numlist, n) {
  return numlist.sort((a, b) => {
    const diffA = Math.abs(a - n);
    const diffB = Math.abs(b - n);

    if (diffA === diffB) {
      return b - a; // 거리가 같으면 큰 숫자가 우선
    }
    return diffA - diffB; // 거리 기준으로 오름차순 정렬
  });
}

핵심 아이디어
절대값 거리 계산: 각 숫자와 기준값 n 간의 거리(Math.abs(a - n))를 계산하여 정렬 기준으로 사용.
거리가 같으면 숫자 크기(b - a)로 정렬.
거리가 다르면 거리 기준으로 오름차순(diffA - diffB) 정렬.


프로세스

function solution(priorities, location) {
  let order = 0;

  while (priorities.length > 0) {
    const current = priorities.shift();
    if (priorities.some((priority) => priority > current)) {
      priorities.push(current);
      location = location === 0 ? priorities.length - 1 : location - 1;
    } else {
      order++;
      if (location === 0) {
        return order; // 목표 프로세스가 실행되었을 때 순서를 반환
      }
      location--;
    }
  }
}

** 핵심 아이디어 **
priorities 배열을 큐처럼 활용하여 구현했고, some() 메서드를 통해 높은 우선순위를 구별했습니다.

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.

전체적으로 너무 깔끔한 코드였습니다!! 잘봤습니다!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Math.sqrt(n) 생각도 못하고 있었는데 감사합니다!

Copy link
Collaborator

Choose a reason for hiding this comment

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

간결해서 좋은거 같아요!

@JooKangsan JooKangsan merged commit 2b5ed8e 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.

2 participants