Skip to content

Conversation

@uyeon0
Copy link
Collaborator

@uyeon0 uyeon0 commented Oct 2, 2025

User description

오늘도 멋져요 👍✨


PR Type

Enhancement


Description

  • 프로그래머스 문제 5개 풀이 추가

  • 행렬의 곱셈, 실패율, 방문 길이, 괄호 회전하기, 짝지어 제거하기

  • 기존 실패율 문제에 새로운 풀이 방법 추가

  • README.md에 새로운 문제 링크 업데이트


@uyeon0 uyeon0 added the programmers Programmers 문제 풀이 label Oct 2, 2025
@github-actions
Copy link

github-actions bot commented Oct 2, 2025

PR Reviewer Guide 🔍

🧪 PR contains tests
⚡ Recommended focus areas for review

변수명 오류

move 함수 내에서 npos와 nPos 변수명이 일관되지 않게 사용되어 런타임 에러가 발생할 수 있습니다. 모든 케이스에서 동일한 변수명을 사용해야 합니다.

let npos;
switch (dir) {
  case "U":
    nPos = { x, y: y - 1 };
    if (isValidPos(nPos)) return nPos;
    return null;
  case "D":
    nPos = { x, y: y + 1 };
    if (isValidPos(nPos)) return nPos;
    return null;
  case "R":
    nPos = { x: x + 1, y };
    if (isValidPos(nPos)) return nPos;
    return null;
  case "L":
    nPos = { x: x - 1, y };
    if (isValidPos(nPos)) return nPos;
    return null;
  default:
    return null;
}
성능 개선 가능

행렬 곱셈에서 내부 반복문의 순서를 최적화하여 캐시 히트율을 높일 수 있습니다. j와 k의 루프 순서를 바꾸면 메모리 접근 패턴이 개선됩니다.

for (let i = 0; i < row1; i++) {
  for (let j = 0; j < col2; j++) {
    for (let k = 0; k < col1; k++) {
      answer[i][j] += arr1[i][k] * arr2[k][j];
    }
  }
}
불필요한 반복

문자열 회전 시 매번 모듈로 연산을 수행하는 대신, 문자열을 두 번 이어붙여서 슬라이딩 윈도우 방식으로 처리하면 성능을 개선할 수 있습니다.

for (let start = 0; start < n; start++) {
  const stack = [];
  let isValid = true;
  for (let offset = 0; offset < n; offset++) {
    // 회전하기
    const c = s[(start + offset) % n];

    // 여는 괄호인 경우 push
    if (c === "(" || c === "[" || c === "{") {
      stack.push(c);
      continue;
    }
    // 스택이 비어있는 경우 다음 회전 문자열을 비교하러 감
    if (stack.length === 0) {
      isValid = false;
      break;
    }
    // 닫는 괄호인 경우 top과 비교
    const top = stack.pop();
    if (c === ")" && top === "(") continue;
    if (c === "]" && top === "[") continue;
    if (c === "}" && top === "{") continue;
    isValid = false;
    break;
  }
  if (isValid && stack.length === 0) answer++;
}

@github-actions
Copy link

github-actions bot commented Oct 2, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
중복 제거로 코드 가독성 개선

중복된 코드를 제거하고 방향별 이동 로직을 객체로 추상화하여 가독성과 유지보수성을 향상시킵니다. 또한 변수명 오타(npos/nPos)를 수정합니다.

Programmers/Level2/49994_방문_길이.js [14-37]

 function move(dir, pos) {
   const { x, y } = pos;
-  let npos;
-  switch (dir) {
-    case "U":
-      nPos = { x, y: y - 1 };
-      if (isValidPos(nPos)) return nPos;
-      return null;
-    case "D":
-      nPos = { x, y: y + 1 };
-      if (isValidPos(nPos)) return nPos;
-      return null;
-    case "R":
-      nPos = { x: x + 1, y };
-      if (isValidPos(nPos)) return nPos;
-      return null;
-    case "L":
-      nPos = { x: x - 1, y };
-      if (isValidPos(nPos)) return nPos;
-      return null;
-    default:
-      return null;
-  }
+  const moves = {
+    U: { x, y: y - 1 },
+    D: { x, y: y + 1 },
+    R: { x: x + 1, y },
+    L: { x: x - 1, y }
+  };
+  const newPos = moves[dir];
+  return newPos && isValidPos(newPos) ? newPos : null;
 }
Suggestion importance[1-10]: 7

__

Why: 방향별 이동 로직을 객체로 추상화하고 중복 코드를 제거하여 코드의 가독성과 유지보수성을 크게 향상시킵니다. 또한 npos/nPos 변수명 불일치 문제도 해결합니다.

Medium
변수명 개선으로 의도 명확화

Object.entries()sort()를 체이닝할 때 더 명확한 변수명을 사용하고, 사용하지 않는 value 파라미터는 제거하여 코드를
개선합니다.

Programmers/Level1/42889_실패율.js [32-34]

 const answer = Object.entries(fails)
-  .sort(([_a, a], [_b, b]) => b - a)
-  .map(([idx, value]) => Number(idx));
+  .sort(([_, failRate1], [__, failRate2]) => failRate2 - failRate1)
+  .map(([stageNum]) => Number(stageNum));
Suggestion importance[1-10]: 4

__

Why: failRate1, failRate2, stageNum과 같은 더 명확한 변수명을 사용하여 코드의 가독성을 개선하지만, 기존 코드도 충분히 이해 가능한 수준입니다.

Low
불필요한 함수 추상화 제거

isEmpty 함수는 단순한 로직을 담고 있어 별도 함수로 분리할 필요가 없습니다. 직접 stack.length === 0을 사용하는 것이 더 명확하고
간결합니다.

Programmers/Level2/12973_짝지어_제거하기.js [8-10]

-function isEmpty(stack) {
-  return stack.length === 0;
+// isEmpty 함수 제거하고 stack.length === 0 직접 사용
+function solution(s) {
+  const stack = [];
+  
+  for (const c of s) {
+    if (stack.length === 0) stack.push(c);
+    else {
+      const top = stack[stack.length - 1];
+      if (c === top) stack.pop();
+      else stack.push(c);
+    }
+  }
+  
+  return stack.length === 0 ? 1 : 0;
 }
Suggestion importance[1-10]: 3

__

Why: 단순한 isEmpty 함수를 제거하는 것은 코드를 약간 더 직관적으로 만들지만, 현재 구현도 충분히 명확하며 함수의 재사용성 측면에서는 오히려 유용할 수 있습니다.

Low

@yoouyeon yoouyeon self-assigned this Oct 2, 2025
길고 반복되던 switch문을 객체를 이용해서 리팩토링
#6 (comment)
일반적인 변수명 개선
사용하지 않는 변수 제거
#6 (comment)
모듈러 연산 대신 문자열을 이어붙여서 회전 효과를 줌
#6 (comment)
@yoouyeon yoouyeon added the ready-to-merge pr을 머지해주세요 label Oct 2, 2025
@uyeon0 uyeon0 merged commit 7c04367 into main Oct 2, 2025
8 checks passed
uyeon0 pushed a commit that referenced this pull request Oct 2, 2025
길고 반복되던 switch문을 객체를 이용해서 리팩토링
#6 (comment)
uyeon0 pushed a commit that referenced this pull request Oct 2, 2025
일반적인 변수명 개선
사용하지 않는 변수 제거
#6 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

programmers Programmers 문제 풀이 ready-to-merge pr을 머지해주세요

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants