Skip to content

Comments

[JustDevRae] 25.01.16#26

Merged
JooKangsan merged 20 commits intomainfrom
JustDevRae
Jan 23, 2025
Merged

[JustDevRae] 25.01.16#26
JooKangsan merged 20 commits intomainfrom
JustDevRae

Conversation

@JustDevRae
Copy link
Collaborator

📌 푼 문제


📝 간단한 풀이 과정

순서쌍의 개수

주어진 숫자 n에 대해 1부터 n까지 순회하며, 약수를 찾아 개수를 세는 방식으로 해결했습니다.

function solution(n) {
  var answer = 0;

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

점의 위치 구하기

주어진 점의 x좌표(dot[0])와 y좌표(dot[1])의 부호를 비교하여, 각각의 사분면(1, 2, 3, 4)을 조건문으로 판단하고 해당 값을 반환하는 방식으로 해결했습니다.

function solution(dot) {
  var answer = 0;

  if (dot[0] > 0 && dot[1] > 0) answer = 1;
  if (dot[0] < 0 && dot[1] > 0) answer = 2;
  if (dot[0] < 0 && dot[1] < 0) answer = 3;
  if (dot[0] > 0 && dot[1] < 0) answer = 4;

  return answer;
}

로그인 성공?

주어진 배열을 순회하면서 db 배열의 i번째 행의 요소값이 id_pw[0]의 요소와 id_pw[1]의 요소가 모두 일치하면 "login", id_pw[0]만 일치하면 "wrong pw", 둘 다 일치하지 않다면 "fail"을 반환하는 방식을 해결했습니다.

function solution(id_pw, db) {
  var answer = "";
  for (let i = 0; i < db.length; i++) {
    if (id_pw[0] === db[i][0]) {
      if (id_pw[1] === db[i][1]) {
        answer = "login";
        break;
      }
      answer = "wrong pw";
      break;
    } else {
      answer = "fail";
    }
  }
  return answer;
}

카드 뭉치

목표 단어 배열(goal)을 순서대로 확인하며, 각 단어가 cards1이나 cards2의 첫 번째 단어와 일치하면 해당 단어를 제거하는 방식으로 해결했습니다.

function solution(cards1, cards2, goal) {
  var answer = "";
  for (let word of goal) {
    if (word == cards1[0]) {
      cards1.shift();
    } else if (word == cards2[0]) {
      cards2.shift();
    } else {
      answer = "No";
      break;
    }
  }

  return answer = "Yes";
}

햄버거 만들기

ingredient 배열을 순회하며, 빈 스택 배열 burger 에 재료를 추가하고 마지막 4개의 재료가 (1, 2, 3, 1) 순서라면 이를 제거하고 햄버거 개수를 증가시키는 방식으로 해결했습니다.

function solution(ingredient) {
  const burger = [];
  let answer = 0;
  for (let i of ingredient) {
    burger.push(i);

    if (
      burger.length >= 4 &&
      burger[burger.length - 4] === 1 &&
      burger[burger.length - 3] === 2 &&
      burger[burger.length - 2] === 3 &&
      burger[burger.length - 1] === 1
    ) {
      burger.splice(burger.length - 4, 4);
      answer++;
    }
  }

  return answer;
}

프로세스

주어진 priorities 배열의 요소를 { 우선순위, 인덱스 } 객체를 요소로 가진 새로운 queue 배열로 반환. 이후 queue를 순회하면서 프로세스의 우선순위가 높지 않다면 다시 배열에 집어 넣고, 우선순위가 높다면 answer을 증가시키고 해당 프로세스가 location 값과 일치하는 확인하는 방법으로 해결했습니다.

function solution(priorities, location) {
  var answer = 0;
  const queue = priorities.map((priority, index) => ({ priority, index }));

  while (queue.length > 0) {
    const current = queue.shift();

    const highPriority = queue.some((item) => item.priority > current.priority);

    if (highPriority) {
      queue.push(current);
    } else {
      answer++;

      if (current.index === location) {
        return answer;
      }
    }
  }
}

Copy link
Collaborator

@oh-chaeyeon oh-chaeyeon 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.

심화문제는 풀지 못해서 리뷰하면서 문제도 처음보게 되었는데, 문제 이해하는 데도 오래걸렸는데 코드리뷰하면서 some메서드 활용하면서 간단히 구현하는 부분 등 많이 배우고 갑니다👍

@JooKangsan JooKangsan merged commit 246c1b1 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