Zadanie · algorytmy trudny Python ·
Course order
There are n courses numbered 0..n-1 and prerequisite pairs [a, b] meaning course b must be taken before course a (pairs may repeat). Return the order in which to take all courses such that whenever several courses are available, the smallest-numbered one is taken first; this order is unique. Return [] if it's impossible to take all courses. Up to 100,000 courses and 300,000 pairs.
Sygnatura
def course_order(n: int, prereqs: list[list[int]]) -> list[int]:Przykłady
course_order(4, [[1, 0], [2, 0], [3, 1], [3, 2]]) # -> [0, 1, 2, 3]
course_order(2, [[0, 1], [1, 0]]) # -> []
course_order(3, []) # -> [0, 1, 2]Oraz 7 ukrytych testów i 1 test wydajnościowy z limitem czasu; dane wejściowe nie są widoczne. Zadania są przekazywane modelom w języku angielskim.
#1OpenAI GPT-6 Lunarozwiązane · 11 z 11 testówodpowiedź w 2.0 s · czas wykonania 587 ms · $0.0001 ✓ course_order(4, [[1, 0], [2, 0], [3, 1], [3, 2]])oczekiwano [0, 1, 2, 3] 1 ms ✓ course_order(2, [[0, 1], [1, 0]])oczekiwano [] <1 ms ✓ course_order(3, [])oczekiwano [0, 1, 2] <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ 581 msKod
import heapq def course_order(n: int, prereqs: list[list[int]]) -> list[int]: graph = [[] for _ in range(n)] indegree = [0] * n for a, b in prereqs: graph[b].append(a) indegree[a] += 1 ready = [i for i, degree in enumerate(indegree) if degree == 0] heapq.heapify(ready) order = [] while ready: course = heapq.heappop(ready) order.append(course) for nxt in graph[course]: indegree[nxt] -= 1 if indegree[nxt] == 0: heapq.heappush(ready, nxt) return order if len(order) == n else []#1Z.ai GLM 5.3rozwiązane · 11 z 11 testówodpowiedź w 901 ms · czas wykonania 617 ms · $0.0003 ✓ course_order(4, [[1, 0], [2, 0], [3, 1], [3, 2]])oczekiwano [0, 1, 2, 3] <1 ms ✓ course_order(2, [[0, 1], [1, 0]])oczekiwano [] 2 ms ✓ course_order(3, [])oczekiwano [0, 1, 2] <1 ms ✓ 2 ms✓ 1 ms✓ 2 ms✓ <1 ms✓ 2 ms✓ 1 ms✓ 1 ms⏱ 604 msKod
import heapq def course_order(n: int, prereqs: list[list[int]]) -> list[int]: adj = [[] for _ in range(n)] indeg = [0] * n for a, b in prereqs: adj[b].append(a) indeg[a] += 1 heap = [i for i in range(n) if indeg[i] == 0] heapq.heapify(heap) order = [] while heap: u = heapq.heappop(heap) order.append(u) for v in adj[u]: indeg[v] -= 1 if indeg[v] == 0: heapq.heappush(heap, v) return order if len(order) == n else []#1Anthropic Claude Opus 5.5rozwiązane · 11 z 11 testówodpowiedź w 6.1 s · czas wykonania 675 ms · $0.01 ✓ course_order(4, [[1, 0], [2, 0], [3, 1], [3, 2]])oczekiwano [0, 1, 2, 3] 1 ms ✓ course_order(2, [[0, 1], [1, 0]])oczekiwano [] <1 ms ✓ course_order(3, [])oczekiwano [0, 1, 2] <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 11 ms✓ <1 ms⏱ 657 msKod
import heapq def course_order(n: int, prereqs: list[list[int]]) -> list[int]: if n <= 0: return [] if not prereqs: return list(range(n)) adj = [[] for _ in range(n)] indeg = [0] * n for a, b in prereqs: adj[b].append(a) indeg[a] += 1 heap = [i for i in range(n) if indeg[i] == 0] heapq.heapify(heap) res = [] push = heapq.heappush pop = heapq.heappop app = res.append while heap: u = pop(heap) app(u) for v in adj[u]: d = indeg[v] - 1 indeg[v] = d if d == 0: push(heap, v) return res if len(res) == n else []#1Qwen3 Coder Nextrozwiązane · 11 z 11 testówodpowiedź w 3.9 s · czas wykonania 712 ms · $0.0002 ✓ course_order(4, [[1, 0], [2, 0], [3, 1], [3, 2]])oczekiwano [0, 1, 2, 3] 1 ms ✓ course_order(2, [[0, 1], [1, 0]])oczekiwano [] <1 ms ✓ course_order(3, [])oczekiwano [0, 1, 2] <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ 706 msKod
import heapq from collections import defaultdict def course_order(n: int, prereqs: list[list[int]]) -> list[int]: # Build adjacency list and in-degree count graph = defaultdict(list) in_degree = [0] * n for a, b in prereqs: graph[b].append(a) in_degree[a] += 1 # Min-heap to always get smallest available course heap = [] for i in range(n): if in_degree[i] == 0: heapq.heappush(heap, i) result = [] while heap: course = heapq.heappop(heap) result.append(course) for neighbor in graph[course]: in_degree[neighbor] -= 1 if in_degree[neighbor] == 0: heapq.heappush(heap, neighbor) # If we couldn't take all courses, there's a cycle if len(result) != n: return [] return result
Koszt tokenów: $0.01 · Zasady rund i punktacji →