-
Notifications
You must be signed in to change notification settings - Fork 0
Conversation
PR Reviewer Guide 🔍
🧪 No relevant tests
⚡ Recommended focus areas for review
복잡한 동적 프로그래밍
알고리즘 최적화
복잡한 동적 프로그래밍
트리 기반 후위순회 동적 프로그래밍 알고리즘의 시간 및 공간 복잡도를 검토해야 합니다. 특히 최적의 팀원 선택 로직의 정확성을 확인해야 합니다.
// 3) dp 배열 준비 (1-indexed) // dp0: v가 참석했을 때의 워크숍 참석 인원의 총 하루 평균 매출액 최솟값 // dp1: v가 참석하지 않았을 때의 워크숍 참석 인원의 총 하루 평균 매출액 최솟값 const dp0 = new Array(n + 1).fill(0); const dp1 = new Array(n + 1).fill(0); // 4) Bottom-up DP 계산 (order를 역순으로 돌면서 후위순회) for (let i = order.length - 1; i >= 0; i--) { const v = order[i]; const childs = children[v]; // case 1) 리프인 경우 (순수 팀원인 경우) // - dp0[v] = sales[v] (본인이 참석) // - dp1[v] = 0 (본인 불참. 아래 팀이 없기 때문에 자식 중 한명이 꼭 참석해야 한다는 강제가 없다. 불참하면 끝.) if (childs.length === 0) { dp1[v] = sales[v - 1]; dp0[v] = 0; continue; } // case 2) 팀장이고 팀원인 경우 (하위 팀이 있음) // a) 참석하는 경우 : 같은 팀의 다른 팀원들은 참석하든 안하든 상관 없음 // -> dp1[v] = sales[v] + ∑ min(dp0, dp1) // b) 불참하는 경우 : 같은 팀의 다른 팀원 중 적어도 한명은 참석해야 함 // -> 우선 모든 팀원들에서 가장 싼 상태를 더한 뒤 (sumMin) // -> 참석으로 돌렸을 때 비용이 가장 적게 추가되는 인원을 참석으로 돌린다. (extraMin) // -> dp0[v] = sumMin + extraMin let sumMin = 0; let extraMin = Infinity; for (const c of childs) { const m = Math.min(dp0[c], dp1[c]); // 참석하는 것과 안하는 것 중 최솟값 sumMin += m; const extra = dp1[c] - m; // (참석으로 돌렸을 때 추가되는 비용) if (extra < extraMin) { extraMin = extra; } } dp1[v] = sales[v - 1] + sumMin; dp0[v] = sumMin + extraMin; }
알고리즘 최적화
현재 구현된 알고리즘의 시간 복잡도는 O(n)이지만, 추가 최적화 가능성을 검토해야 합니다. 특히 extraMin 계산 로직의 효율성을 점검해야 합니다.
let sumMin = 0; let extraMin = Infinity; for (const c of childs) { const m = Math.min(dp0[c], dp1[c]); // 참석하는 것과 안하는 것 중 최솟값 sumMin += m; const extra = dp1[c] - m; // (참석으로 돌렸을 때 추가되는 비용) if (extra < extraMin) { extraMin = extra; } }
PR Code Suggestions ✨Explore these optional code suggestions:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Uh oh!
There was an error while loading. Please reload this page.
User description
오늘도 멋져요 👍✨
PR Type
Enhancement
Description
프로그래머스 72416 문제 (매출 하락 최소화) 해결
복잡한 동적 프로그래밍 알고리즘 구현
후위순회 기반 트리 순회 로직 적용