Skip to content

Conversation

@uyeon0
Copy link
Collaborator

@uyeon0 uyeon0 commented Oct 3, 2025

User description

오늘도 멋져요 👍✨


PR Type

Enhancement


Description

  • 크레인 인형뽑기 게임 문제에 스택 기반 최적화 솔루션 추가

  • 주식가격 문제에 스택 기반 O(n) 솔루션 추가

  • n^2 배열 자르기 문제에 수학적 접근 솔루션 추가

  • 배열 관련 기초 문제 4개 추가 (Level 0/1)


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

github-actions bot commented Oct 3, 2025

PR Reviewer Guide 🔍

🧪 PR contains tests
⚡ Recommended focus areas for review

성능 최적화

스택을 사용한 O(n) 솔루션에서 불필요한 객체 생성이 있습니다. price와 idx를 별도의 스택으로 관리하면 메모리 사용량을 줄일 수 있습니다.

if (stack.length === 0 || stack[stack.length - 1].price <= price) {
  stack.push({ idx, price });
  continue;
엣지 케이스

board가 비어있거나 moves가 비어있는 경우에 대한 초기 검증이 없습니다. 또한 topIdxs 배열 초기화 시 undefined 대신 -1을 사용하면 더 명확할 수 있습니다.

const topIdxs = new Array(board.length);
const basket = [];
let answer = 0;

// 게임 판 각 열의 top 인덱스를 저장하기
for (let col = 0; col < boardSize; col++) {
  for (let row = 0; row < boardSize; row++) {
    if (board[row][col] === 0) continue;
    topIdxs[col] = row;
    break;
  }
  if (topIdxs[col] === undefined) topIdxs[col] = boardSize;
}
메모리 최적화

answer 배열을 미리 할당하고 right-left+1 크기로 초기화하면 배열 리사이징 비용을 줄일 수 있습니다.

let answer = [];

for (let idx = left; idx <= right; idx++) {
  const div = parseInt(idx / n);
  const mod = idx % n;

  answer.push(Math.max(div, mod) + 1);
}

return answer;

@github-actions
Copy link

github-actions bot commented Oct 3, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
함수형 배열 생성으로 개선

Array.from()을 사용하여 더 선언적이고 간결한 방식으로 배열을 생성할 수 있습니다. 또한 parseInt 대신 Math.floor를 사용하면
더 명시적이고 성능이 좋습니다.

Programmers/Level2/87390_n^2_배열_자르기.js [10-17]

-let answer = [];
-for (let idx = left; idx <= right; idx++) {
-  const div = parseInt(idx / n);
-  const mod = idx % n;
-  answer.push(Math.max(div, mod) + 1);
-}
+return Array.from({ length: right - left + 1 }, (_, i) => {
+  const idx = left + i;
+  const row = Math.floor(idx / n);
+  const col = idx % n;
+  return Math.max(row, col) + 1;
+});
Suggestion importance[1-10]: 5

__

Why: Array.from()을 사용한 함수형 접근은 더 선언적이고 간결하지만, 현재 구현도 충분히 명확합니다. Math.floor로의 변경은 미미한 성능 개선만 제공합니다.

Low
스택 조작 로직 개선

스택의 top 요소에 대한 반복적인 접근을 개선하기 위해 별도의 함수로 분리하고, 스택이 비어있는지 확인하는 로직을 더 명확하게 만들 수 있습니다.
이렇게 하면 코드의 가독성과 유지보수성이 향상됩니다.

Programmers/Level2/42584_주식가격.js [22-27]

-let top = stack[stack.length - 1];
-while (top && top.price > price) {
-  stack.pop();
+const getStackTop = (stack) => stack[stack.length - 1];
+while (stack.length > 0 && getStackTop(stack).price > price) {
+  const top = stack.pop();
   answer[top.idx] = idx - top.idx;
-  top = stack[stack.length - 1];
 }
Suggestion importance[1-10]: 4

__

Why: 스택 조작을 함수로 분리하는 것은 코드 가독성을 약간 개선하지만, 현재 구현도 충분히 명확하며 성능상의 이점도 없습니다. getStackTop 함수 추가는 오히려 불필요한 추상화일 수 있습니다.

Low
중첩 조건문 단순화

바구니 조작 로직을 더 간결하게 리팩토링할 수 있습니다. 조건문을 줄이고 로직을 단순화하면 코드의 가독성이 향상되고 유지보수가 쉬워집니다.

Programmers/Level1/64061_크레인_인형뽑기_게임.js [35-47]

-if (basket.length === 0) {
+const basketTop = basket[basket.length - 1];
+if (basketTop === top) {
+  basket.pop();
+  answer += 2;
+} else {
   basket.push(top);
-} else {
-  const basketTop = basket[basket.length - 1];
-  if (top === basketTop) {
-    // 같으면 터진다
-    basket.pop();
-    answer += 2;
-  } else {
-    // 다르면 그냥 넣는다.
-    basket.push(top);
-  }
 }
Suggestion importance[1-10]: 3

__

Why: 제안된 개선은 코드를 약간 더 간결하게 만들지만, 현재 구현도 충분히 명확하며 빈 바구니 처리에 대한 명시적인 로직이 오히려 더 이해하기 쉬울 수 있습니다.

Low

@yoouyeon yoouyeon added the ready-to-merge pr을 머지해주세요 label Oct 4, 2025
@uyeon0 uyeon0 merged commit 4d1b252 into main Oct 4, 2025
9 checks passed
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