Skip to content

Comments

[JustDevRae] 25.01.25#33

Merged
JooKangsan merged 25 commits intomainfrom
JustDevRae
Feb 6, 2025
Merged

[JustDevRae] 25.01.25#33
JooKangsan merged 25 commits intomainfrom
JustDevRae

Conversation

@JustDevRae
Copy link
Collaborator

📌 푼 문제


📝 간단한 풀이 과정

모스 부호

모스 부호를 키, 알파벳을 값으로 가지는 morse 객체를 생성하고, 공백으로 나눠진 모스 부호 배열을 순회하면서 해당 알파벳을 찾아 결과 문자열에 추가하는 방식으로 해결했습니다.

function solution(letter) {
  var answer = "";
  const morse = {
    ".-": "a",
    "-...": "b",
    "-.-.": "c",
    "-..": "d",
    ".": "e",
    "..-.": "f",
    "--.": "g",
    "....": "h",
    "..": "i",
    ".---": "j",
    "-.-": "k",
    ".-..": "l",
    "--": "m",
    "-.": "n",
    "---": "o",
    ".--.": "p",
    "--.-": "q",
    ".-.": "r",
    "...": "s",
    "-": "t",
    "..-": "u",
    "...-": "v",
    ".--": "w",
    "-..-": "x",
    "-.--": "y",
    "--..": "z",
  };
  const str = letter.split(" ");

  for (const s of str) {
    answer += morse[s];
  }
  return answer;
}

A로 B 만들기

각 문자열의 문자를 카운팅하여 객체로 만든 후, 각 키와 값을 비교하여 모든 문자가 동일하면 1, 그렇지 않으면 0을 반환하는 방식으로 해결했습니다.

function solution(before, after) {
  var answer = 0;
  const beforeCount = {};
  const afterCount = {};

  for (const char of before) {
    beforeCount[char] = (beforeCount[char] || 0) + 1;
  }

  for (const char of after) {
    afterCount[char] = (afterCount[char] || 0) + 1;
  }

  for (const key in beforeCount) {
    if (beforeCount[key] !== afterCount[key]) {
      return (answer = 0);
    }
  }

  return (answer = 1);
}

진료 순서 정하기

배열을 내림차순으로 정렬하여 각 값의 순위를 매핑하는 Map 객체를 생성한 뒤, 원래 배열의 각 값에 대해 순위를 찾아 반환하는 방식으로 해결했습니다.

function solution(emergency) {
  const sorted = [...emergency].sort((a, b) => b - a);
  const rankMap = new Map();

  sorted.forEach((value, index) => {
    rankMap.set(value, index + 1);
  });

  return emergency.map((value) => rankMap.get(value));
}

등수 매기기

평균 점수를 계산한 뒤, 내림차순으로 정렬된 배열을 생성하고, 원래 배열의 각 평균 점수에 대해 정렬된 배열에서의 순위를 찾아 반환하는 방식으로 구현했습니다.

function solution(score) {
  const averages = score.map(([english, math]) => (english + math) / 2);
  const sortedAverages = [...averages].sort((a, b) => b - a);

  return averages.map((avg) => sortedAverages.indexOf(avg) + 1);
}

완주하지 못한 선수

참가자 이름의 개수를 세어 객체로 저장한 뒤, 완주한 이름에 대해 개수를 줄이고, 최종적으로 값이 0보다 큰 이름을 반환하는 방식으로 구현했습니다.

function solution(participant, completion) {
  const obj = {};

  for (const p of participant) {
    if (obj[p]) {
      obj[p] += 1;
    } else {
      obj[p] = 1;
    }
  }

  for (const c of completion) {
    obj[c] -= 1;
  }

  for (const key in obj) {
    if (obj[key] > 0) {
      return key;
    }
  }
}

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.

중복을 줄이거나 계산과정에서 미리 결과를 더하는식으로 수정해봐도 좋을거같아요!
한주간 고생하셨습니다.

Copy link
Collaborator

@unn04012 unn04012 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 8d39ffd into main Feb 6, 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.

4 participants