일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- realclass
- Writing
- FIT XR
- 스탭퍼
- 운동
- 영어공부
- 10분
- 개발자
- 30분
- 읽기
- 만화도
- English
- 3줄정리
- 쓰릴오브파이트
- 리얼 클래스
- 뭐든
- 파비최
- Daily Challenge
- 프로젝트
- leetcode
- 사이드
- 링피트
- 월간
- 미드시청
- 괜찮음
- 화상영어
- 영어원서읽기
- 잡생각
- Problem Solving
- 매일
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.04.09 Today's Challenge 본문
https://leetcode.com/problems/largest-color-value-in-a-directed-graph/
Largest Color Value in a Directed Graph - LeetCode
Can you solve this real interview question? Largest Color Value in a Directed Graph - There is a directed graph of n colored nodes and m edges. The nodes are numbered from 0 to n - 1. You are given a string colors where colors[i] is a lowercase English let
leetcode.com
토폴로지 정렬 활용하는 문제
class Solution:
def largestPathValue(self, colors: str, edges: List[List[int]]) -> int:
n, k = len(colors), 26
indegrees = [0] * n
graph = [[] for _ in range(n)]
for u, v in edges:
graph[u].append(v)
indegrees[v] += 1
zero_indegree = set(i for i in range(n) if indegrees[i] == 0)
counts = [[0] * k for _ in range(n)]
for i, c in enumerate(colors):
counts[i][ord(c) - ord('a')] += 1
max_count, visited = 0, 0
while zero_indegree:
u = zero_indegree.pop()
visited += 1
for v in graph[u]:
for i in range(k):
counts[v][i] = max(counts[v][i], counts[u][i] + (ord(colors[v]) - ord('a') == i))
indegrees[v] -= 1
if indegrees[v] == 0:
zero_indegree.add(v)
max_count = max(max_count, max(counts[u]))
return max_count if visited == n else -1
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.04.11 Today's Challenge (0) | 2023.04.11 |
---|---|
2023.04.10 Today's Challenge (0) | 2023.04.10 |
2023.04.08 Today's Challenge (0) | 2023.04.08 |
2023.04.07 Today's Challenge (0) | 2023.04.07 |
2023.04.06 Today's Challenge (0) | 2023.04.06 |
Comments