Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    3530 - Maximum Profit from Valid Topological Order in DAG.

    Hard

    You are given a Directed Acyclic Graph (DAG) with n nodes labeled from 0 to n - 1, represented by a 2D array edges, where <code>edgesi = u<sub>i</sub>, v<sub>i</sub></code> indicates a directed edge from node <code>u<sub>i</sub></code> to <code>v<sub>i</sub></code>. Each node has an associated score given in an array score, where score[i] represents the score of node i.

    You must process the nodes in a valid topological order. Each node is assigned a 1-based position in the processing order.

    The profit is calculated by summing up the product of each node's score and its position in the ordering.

    Return the maximum possible profit achievable with an optimal topological order.

    A topological order of a DAG is a linear ordering of its nodes such that for every directed edge u &rarr; v, node u comes before v in the ordering.

    Example 1:

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

    Output: 8

    Explanation:

    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

    Explanation:

    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 &lt;= n == score.length &lt;= 22

    • <code>1 <= scorei<= 10<sup>5</sup></code>

    • 0 &lt;= edges.length &lt;= n * (n - 1) / 2

    • <code>edgesi == u<sub>i</sub>, v<sub>i</sub></code> denotes a directed edge from <code>u<sub>i</sub></code> to <code>v<sub>i</sub></code>.

    • <code>0 <= u<sub>i</sub>, v<sub>i</sub>< n</code>

    • <code>u<sub>i</sub> != v<sub>i</sub></code>

    • The input graph is guaranteed to be a DAG.

    • There are no duplicate edges.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer maxProfit(Integer n, Array<IntArray> edges, IntArray score)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait