Skip to content

Conversation

@uyeon0
Copy link
Collaborator

@uyeon0 uyeon0 commented Jan 31, 2026

User description

오늘도 멋져요 👍✨


PR Type

Enhancement


Description

  • 프로그래머스 92343 문제 (양과 늑대) 해결

  • DFS 알고리즘을 사용한 트리 탐색 구현

  • 양과 늑대 수 조건에 따른 최대 양 수집 로직


@uyeon0 uyeon0 added the programmers Programmers 문제 풀이 label Jan 31, 2026
@github-actions
Copy link

PR Reviewer Guide 🔍

🧪 No relevant tests
⚡ Recommended focus areas for review

알고리즘 최적화

DFS 탐색 알고리즘의 시간 복잡도와 공간 복잡도 개선 필요. 현재 구현은 모든 가능한 경로를 탐색하므로 비효율적일 수 있음.

function dfs(current, sheep, wolf, possible) {
    const [newSheep, newWolf] = info[current] ? [sheep, wolf + 1] : [sheep + 1, wolf];
    // 만약 양이 늑대보다 많지 않다면 게임 오버 (바로 리턴한다.)
    if (newSheep <= newWolf) {
        return;
    }

    // 양의 수 업데이트(더 많은 것으로 업데이트한다.)
    answer = Math.max(answer, newSheep);
    // 방문 가능 목록 업데이트하기 (이미 방문한 곳은 제거)
    const newPossible = new Set(possible);
    newPossible.delete(current);
    tree[current].forEach((child) => newPossible.add((child)))
    // 방문 가능한 모든 노드 시도
    newPossible.forEach((next) => dfs(next, newSheep, newWolf, newPossible));
}
가능한 경로 탐색

현재 구현은 모든 노드를 방문하는 완전 탐색 방식으로, 불필요한 경로 탐색을 줄이는 가지치기 전략 고려 필요.

const newPossible = new Set(possible);
newPossible.delete(current);
tree[current].forEach((child) => newPossible.add((child)))
// 방문 가능한 모든 노드 시도
newPossible.forEach((next) => dfs(next, newSheep, newWolf, newPossible));

@github-actions
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
노드 방문 로직 최적화

현재 코드는 불필요한 Set 복사와 중복된 노드 추가 로직이 있어 비효율적입니다. 방문 가능한 노드 업데이트 로직을 간소화하고 성능을 개선할 수
있습니다.

Programmers/Level3/92343_양과_늑대.js [21-36]

 function dfs(current, sheep, wolf, possible) {
   const [newSheep, newWolf] = info[current] ? [sheep, wolf + 1] : [sheep + 1, wolf];
-  // 만약 양이 늑대보다 많지 않다면 게임 오버 (바로 리턴한다.)
-  if (newSheep <= newWolf) {
-      return;
+  if (newSheep <= newWolf) return;
+  
+  answer = Math.max(answer, newSheep);
+  
+  const nextPossible = new Set(possible);
+  nextPossible.delete(current);
+  tree[current].forEach(child => nextPossible.add(child));
+  
+  for (const next of nextPossible) {
+    dfs(next, newSheep, newWolf, nextPossible);
   }
-  
-  // 양의 수 업데이트(더 많은 것으로 업데이트한다.)
-  answer = Math.max(answer, newSheep);
-  // 방문 가능 목록 업데이트하기 (이미 방문한 곳은 제거)
-  const newPossible = new Set(possible);
-  newPossible.delete(current);
-  tree[current].forEach((child) => newPossible.add((child)))
-  // 방문 가능한 모든 노드 시도
-  newPossible.forEach((next) => dfs(next, newSheep, newWolf, newPossible));
 }
Suggestion importance[1-10]: 7

__

Why: The suggestion improves the code's readability and potentially its performance by simplifying the node traversal logic. The changes include using a for...of loop instead of forEach and reducing unnecessary Set operations.

Medium
트리 구조 생성 최적화

트리 구조를 더 명확하고 효율적으로 생성할 수 있습니다. 객체 기반의 인접 리스트를 사용하면 코드 가독성과 성능을 개선할 수 있습니다.

Programmers/Level3/92343_양과_늑대.js [13-14]

-const tree = Array.from({length: nodeCount}, () => []);
-edges.forEach(([parent, child]) => tree[parent].push(child));
+const tree = edges.reduce((acc, [parent, child]) => {
+  acc[parent] = acc[parent] || [];
+  acc[parent].push(child);
+  return acc;
+}, {});
Suggestion importance[1-10]: 6

__

Why: The suggestion replaces the Array.from() initialization with a more concise reduce() method. While the change improves code readability, the performance impact might be minimal.

Low

@yoouyeon yoouyeon self-assigned this Jan 31, 2026
@yoouyeon yoouyeon added the ready-to-merge pr을 머지해주세요 label Jan 31, 2026
@uyeon0 uyeon0 merged commit 9022fd4 into main Jan 31, 2026
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