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
This discussion was converted from issue #31 on September 15, 2026 11:00.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Problem link
https://leetcode.com/problems/largest-color-value-in-a-directed-graph/
Problem Summary
노드에 색이 칠해져 있는 방향 그래프가 주어진다.
그래프의 path 중 가장 많이 나온 색깔의 개수를 구하는 문제. 사이클이 있다면 -1을 출력.
Solution
처음엔 dfs로 구현해서 제출했는데 엣지 케이스 처리가 까다롭다. (사이클 탐지, visited 체크 등)
결국 위상 정렬로 다시 제출.
그리디적인 방법을 생각해보면 가장 색깔이 많이 나오려면 경로의 제일 처음부터 돌려야 한다. 경로의 시작 지점은 위상 정렬로 쉽게 구할 수 있다.
사이클 탐지도 꽤 간단한데, 위상 정렬을 했을 때 방문하지 못하는 노드는 사이클이 존재하는 것이다.
색깔 카운트는 자기 자신은 ++, 다음 노드는 이전 노드의 값 중 최댓값을 복사해 줌.
Source Code
All reactions