Find the maximum total node score over all paths in a DAG

Quick Overview

Given a directed acyclic graph with a score on every node and a cost on every edge, find the largest total node score of any path, where a path may start and end at any node. It tests topological ordering and dynamic programming over a DAG with up to 10,000 nodes.

Find the maximum total node score over all paths in a DAG

Company: Airbnb

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a weighted directed acyclic graph (DAG) with `n` nodes labeled from `0` to `n - 1`. Each node has a score, and each directed edge has a time cost. Find the maximum total score achievable by traversing any path in the DAG. The total score of a path is the sum of the scores of all nodes visited along the path. You may start and end at any node. You are given an integer `n`, an array `scores` where `scores[i]` is the score of node `i`, and a list of directed edges where each edge is represented as `[from, to, cost]`. Return the maximum total score among all possible paths. ### Function Signature ```python def max_path_score(n: int, scores: list[int], edges: list[list[int]]) -> int: ``` ### Rules - `edges[i] = [from, to, cost]` is a directed edge from node `from` to node `to`. A path follows edges only in their direction. - The edge cost is part of the input but does not count toward a path's score. Only node scores are summed. - A single node is a valid path, so the answer is at least `max(scores)`. ### Constraints - `2 <= n <= 10^4` - `0 <= edges.length <= 5 * 10^4` - `scores.length == n` - `1 <= scores[i] <= 10^4` - `edges[i].length == 3` - `0 <= from_i, to_i < n` - `from_i != to_i` - `1 <= cost_i <= 10^4` - The graph is guaranteed to be a DAG (no cycles). ### Examples ```text Input: n = 5, scores = [3,7,2,5,8], edges = [[0,1,2],[0,2,4],[1,3,1],[2,3,3],[3,4,2]] Output: 23 ``` Two full paths run from node 0 to node 4. `0 -> 1 -> 3 -> 4` scores 3 + 7 + 5 + 8 = 23 and `0 -> 2 -> 3 -> 4` scores 3 + 2 + 5 + 8 = 18, so the maximum is 23. ```text Input: n = 4, scores = [10,1,1,10], edges = [[0,1,5],[0,2,3],[1,3,2],[2,3,4]] Output: 21 ``` Both `0 -> 1 -> 3` and `0 -> 2 -> 3` score 10 + 1 + 10 = 21, and no longer path exists. ```text Input: n = 3, scores = [4,9,2], edges = [] Output: 9 ``` With no edges every path is a single node, and the best one is node 1 with score 9.

Overview: Given a directed acyclic graph with a score on every node and a cost on every edge, find the largest total node score of any path, where a path may start and end at any node. It tests topological ordering and dynamic programming over a DAG with up to 10,000 nodes.

Airbnb logo
Airbnb
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a weighted directed acyclic graph (DAG) with n nodes labeled from 0 to n - 1. Each node has a score, and each directed edge has a time cost.

Find the maximum total score achievable by traversing any path in the DAG. The total score of a path is the sum of the scores of all nodes visited along the path. You may start and end at any node.

You are given an integer n, an array scores where scores[i] is the score of node i, and a list of directed edges where each edge is represented as [from, to, cost]. Return the maximum total score among all possible paths.

Function Signature

def max_path_score(n: int, scores: list[int], edges: list[list[int]]) -> int:

Rules

  • edges[i] = [from, to, cost] is a directed edge from node from to node to . A path follows edges only in their direction.
  • The edge cost is part of the input but does not count toward a path's score. Only node scores are summed.
  • A single node is a valid path, so the answer is at least max(scores) .

Constraints

  • 2 <= n <= 10^4
  • 0 <= edges.length <= 5 * 10^4
  • scores.length == n
  • 1 <= scores[i] <= 10^4
  • edges[i].length == 3
  • 0 <= from_i, to_i < n
  • from_i != to_i
  • 1 <= cost_i <= 10^4
  • The graph is guaranteed to be a DAG (no cycles).

Examples

Input: n = 5, scores = [3,7,2,5,8], edges = [[0,1,2],[0,2,4],[1,3,1],[2,3,3],[3,4,2]]
Output: 23

Two full paths run from node 0 to node 4. 0 -> 1 -> 3 -> 4 scores 3 + 7 + 5 + 8 = 23 and 0 -> 2 -> 3 -> 4 scores 3 + 2 + 5 + 8 = 18, so the maximum is 23.

Input: n = 4, scores = [10,1,1,10], edges = [[0,1,5],[0,2,3],[1,3,2],[2,3,4]]
Output: 21

Both 0 -> 1 -> 3 and 0 -> 2 -> 3 score 10 + 1 + 10 = 21, and no longer path exists.

Input: n = 3, scores = [4,9,2], edges = []
Output: 9

With no edges every path is a single node, and the best one is node 1 with score 9.