Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    3543 - Maximum Weighted K-Edge Path.

    Medium

    You are given an integer n and a Directed Acyclic Graph (DAG) with n nodes labeled from 0 to n - 1. This is represented by a 2D array edges, where <code>edgesi = u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub></code> indicates a directed edge from node <code>u<sub>i</sub></code> to <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.

    You are also given two integers, k and t.

    Your task is to determine the maximum possible sum of edge weights for any path in the graph such that:

    • The path contains exactly k edges.

    • The total sum of edge weights in the path is strictly less than t.

    Return the maximum possible sum of weights for such a path. If no such path exists, return -1.

    Example 1:

    Input: n = 3, edges = [0,1,1,1,2,2], k = 2, t = 4

    Output: 3

    Explanation:

    • The only path with k = 2 edges is 0 -&gt; 1 -&gt; 2 with weight 1 + 2 = 3 &lt; t.

    • Thus, the maximum possible sum of weights less than t is 3.

    Example 2:

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

    Output: 2

    Explanation:

    • There are two paths with k = 1 edge:

    • Thus, the maximum possible sum of weights less than t is 2.

    Example 3:

    Input: n = 3, edges = [0,1,6,1,2,8], k = 1, t = 6

    Output: \-1

    Explanation:

    • There are two paths with k = 1 edge:

    • Since there is no path with sum of weights strictly less than t, the answer is -1.

    Constraints:

    • 1 &lt;= n &lt;= 300

    • 0 &lt;= edges.length &lt;= 300

    • <code>edgesi = u<sub>i</sub>, v<sub>i</sub>, w<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>

    • <code>1 <= w<sub>i</sub><= 10</code>

    • 0 &lt;= k &lt;= 300

    • 1 &lt;= t &lt;= 600

    • 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 maxWeight(Integer n, Array<IntArray> edges, Integer k, Integer t)
      • Methods inherited from class java.lang.Object

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