LeetCode Problem

How to Solve Maximum Profit from Valid Topological Order in DAG

The problem asks to find the maximum profit by processing nodes in a valid topological order in a Directed Acyclic Graph (DAG). By calculating the profit as the sum of each node's score multiplied by its position in the order, a bitmask dynamic programming approach is effective for this problem. A correct approach requires handling graph indegree and efficiently sorting the nodes.

GhostInterview Help

Need help with Maximum Profit from Valid Topological Order in DAG without spending extra time grinding it?

GhostInterview can read Maximum Profit from Valid Topological Order in DAG from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3530Graph indegree plus topological orderingReviewed 2026-03-08
Difficulty
Hard
Primary pattern
Graph indegree plus topological ordering
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The problem asks to find the maximum profit by processing nodes in a valid topological order in a Directed Acyclic Graph (DAG). By calculating the profit as the sum of each node's score multiplied by its position in the order, a bitmask dynamic programming approach is effective for this problem. A correct approach requires handling graph indegree and efficiently sorting the nodes.

Problem Statement

Given a Directed Acyclic Graph (DAG) with n nodes labeled from 0 to n-1, represented by a 2D array edges, where edges[i] = [ui, vi] indicates a directed edge from node ui to node vi. Each node has a score specified in the array score where score[i] represents the score of node i.

You need to process the nodes in a valid topological order. The goal is to maximize the total profit by summing up the product of each node's score and its position in the order. The position is assigned based on a valid topological ordering of the nodes.

Examples

Example 1

Input: n = 2, edges = [[0,1]], score = [2,3]

Output: 8

Node 1 depends on node 0, so a valid order is [0, 1] . The maximum total profit achievable over all valid topological orders is 2 + 6 = 8 .

Example 2

Input: n = 3, edges = [[0,1],[0,2]], score = [1,6,3]

Output: 25

Nodes 1 and 2 depend on node 0, so the most optimal valid order is [0, 2, 1] . The maximum total profit achievable over all valid topological orders is 1 + 6 + 18 = 25 .

Constraints

  • 1 <= n == score.length <= 22
  • 1 <= score[i] <= 105
  • 0 <= edges.length <= n * (n - 1) / 2
  • edges[i] == [ui, vi] denotes a directed edge from ui to vi.
  • 0 <= ui, vi < n
  • ui != vi
  • The input graph is guaranteed to be a DAG.
  • There are no duplicate edges.

Solution Approach

Graph Representation & Indegree Calculation

Start by representing the DAG using an adjacency list. For each node, calculate the indegree, which helps identify nodes that can be processed first in the topological order. Nodes with zero indegree can be processed next.

Bitmask Dynamic Programming

Apply a bitmask dynamic programming (DP) approach to efficiently keep track of all possible subsets of processed nodes. This allows for efficiently calculating the maximum profit as we process the nodes in valid topological orders.

Maximizing Profit with Node Positioning

As each node is processed, calculate its profit based on its position in the topological order. Ensure to calculate the maximum achievable profit by considering all valid orderings and node placements.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity of the approach depends on the number of nodes n and the number of subsets of nodes, which is 2^n. Therefore, the time complexity is O(n * 2^n). The space complexity also depends on the state space, which is O(2^n), due to the bitmask representation.

What Interviewers Usually Probe

  • Look for the candidate's ability to represent the graph efficiently.
  • Check if the candidate is familiar with dynamic programming and bitmasking for subset problems.
  • Observe the candidate's approach to maximizing profit using node positions in a topological sort.

Common Pitfalls or Variants

Common pitfalls

  • Misunderstanding the need for a valid topological order, leading to incorrect node placements.
  • Not efficiently calculating the profit for each node in relation to its position in the order.
  • Overcomplicating the bitmask dynamic programming approach or failing to implement it correctly.

Follow-up variants

  • Consider adding a constraint where each node has multiple outgoing edges, increasing complexity.
  • Implement the problem with additional edge weights, where the profit calculation also depends on edge values.
  • Introduce a constraint where not all nodes must be processed, but a subset of nodes should be considered for the topological order.

How GhostInterview Helps

  • GhostInterview's problem-solving approach helps candidates systematically address the graph representation and dynamic programming requirements.
  • By offering step-by-step guidance on implementing the bitmask dynamic programming solution, GhostInterview ensures candidates don’t miss key efficiency considerations.
  • The tool helps candidates understand the trade-offs in node ordering and profit calculation for maximizing the solution.

Topic Pages

FAQ

What is the primary approach to solve the Maximum Profit from Valid Topological Order in DAG?

The primary approach involves graph representation using adjacency lists, calculating indegrees, and applying bitmask dynamic programming to efficiently find the maximum profit.

How does the bitmask dynamic programming technique help in this problem?

Bitmask dynamic programming helps track subsets of nodes that have been processed, allowing for efficient calculation of the maximum profit by iterating through all valid topological orderings.

What is a valid topological order and why is it important here?

A valid topological order is an ordering of nodes in a directed graph where for every directed edge from node A to node B, node A comes before node B. It is crucial because the nodes must be processed in this order to calculate the correct profit.

How do you calculate the profit for each node in the problem?

The profit for each node is calculated by multiplying its score by its position in the valid topological order, and summing these values for all nodes gives the total profit.

What are some common mistakes in solving the Maximum Profit from Valid Topological Order in DAG?

Common mistakes include failing to correctly compute the topological order, miscalculating node profit based on its position, or struggling with the bitmask dynamic programming implementation.

GhostInterview Solver

Need direct help with Maximum Profit from Valid Topological Order in DAG instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Maximum Profit from Valid Topological Order in DAG from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.