You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DFS 탐색 알고리즘의 시간 복잡도와 공간 복잡도 개선 필요. 현재 구현은 모든 가능한 경로를 탐색하므로 비효율적일 수 있음.
functiondfs(current,sheep,wolf,possible){const[newSheep,newWolf]=info[current] ? [sheep,wolf+1] : [sheep+1,wolf];// 만약 양이 늑대보다 많지 않다면 게임 오버 (바로 리턴한다.)if(newSheep<=newWolf){return;}// 양의 수 업데이트(더 많은 것으로 업데이트한다.)answer=Math.max(answer,newSheep);// 방문 가능 목록 업데이트하기 (이미 방문한 곳은 제거)constnewPossible=newSet(possible);newPossible.delete(current);tree[current].forEach((child)=>newPossible.add((child)))// 방문 가능한 모든 노드 시도newPossible.forEach((next)=>dfs(next,newSheep,newWolf,newPossible));}
현재 구현은 모든 노드를 방문하는 완전 탐색 방식으로, 불필요한 경로 탐색을 줄이는 가지치기 전략 고려 필요.
constnewPossible=newSet(possible);newPossible.delete(current);tree[current].forEach((child)=>newPossible.add((child)))// 방문 가능한 모든 노드 시도newPossible.forEach((next)=>dfs(next,newSheep,newWolf,newPossible));
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
트리 구조 생성 최적화
트리 구조를 더 명확하고 효율적으로 생성할 수 있습니다. 객체 기반의 인접 리스트를 사용하면 코드 가독성과 성능을 개선할 수 있습니다.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
오늘도 멋져요 👍✨
PR Type
Enhancement
Description
프로그래머스 92343 문제 (양과 늑대) 해결
DFS 알고리즘을 사용한 트리 탐색 구현
양과 늑대 수 조건에 따른 최대 양 수집 로직