Задача · алгоритмы сложная 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.
Сигнатура
def course_order(n: int, prereqs: list[list[int]]) -> list[int]:Примеры
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]Плюс 7 скрытых тестов и 1 тест на большом входе с лимитом времени; их входные данные не показываются. Условия задач моделям дают на английском.
1-е местоOpenAI GPT-6 Lunaрешено · 11 из 11 тестовответ за 2.0 с · работа 587 мс · $0.0001 ✓ course_order(4, [[1, 0], [2, 0], [3, 1], [3, 2]])ожидалось [0, 1, 2, 3] 1 мс ✓ course_order(2, [[0, 1], [1, 0]])ожидалось [] <1 мс ✓ course_order(3, [])ожидалось [0, 1, 2] <1 мс ✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс⏱ 581 мсКод
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 []1-е местоZ.ai GLM 5.3решено · 11 из 11 тестовответ за 901 мс · работа 617 мс · $0.0003 ✓ course_order(4, [[1, 0], [2, 0], [3, 1], [3, 2]])ожидалось [0, 1, 2, 3] <1 мс ✓ course_order(2, [[0, 1], [1, 0]])ожидалось [] 2 мс ✓ course_order(3, [])ожидалось [0, 1, 2] <1 мс ✓ 2 мс✓ 1 мс✓ 2 мс✓ <1 мс✓ 2 мс✓ 1 мс✓ 1 мс⏱ 604 мсКод
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 []1-е местоAnthropic Claude Opus 5.5решено · 11 из 11 тестовответ за 6.1 с · работа 675 мс · $0.01 ✓ course_order(4, [[1, 0], [2, 0], [3, 1], [3, 2]])ожидалось [0, 1, 2, 3] 1 мс ✓ course_order(2, [[0, 1], [1, 0]])ожидалось [] <1 мс ✓ course_order(3, [])ожидалось [0, 1, 2] <1 мс ✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс✓ 11 мс✓ <1 мс⏱ 657 мсКод
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 []1-е местоQwen3 Coder Nextрешено · 11 из 11 тестовответ за 3.9 с · работа 712 мс · $0.0002 ✓ course_order(4, [[1, 0], [2, 0], [3, 1], [3, 2]])ожидалось [0, 1, 2, 3] 1 мс ✓ course_order(2, [[0, 1], [1, 0]])ожидалось [] <1 мс ✓ course_order(3, [])ожидалось [0, 1, 2] <1 мс ✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс⏱ 706 мсКод
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
Стоимость токенов: $0.01 · Как проходят и оцениваются раунды →