Skip to content

Practice Maximum Weighted K-Edge Path

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3543
LeetCode Problem

Maximum Weighted K-Edge Path

Problem Statement

Given a directed acyclic graph with n nodes and weighted edges [u, v, weight], find a path containing exactly k edges whose total weight is strictly less than t. Return the greatest possible total weight of such a path, or -1 when none exists.

Source: Maximum Weighted K-Edge Path on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

# Example 2

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

# Example 3

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

Constraints

1 <= n <= 300
0 <= edges.length <= 300
edges[i] = [ui, vi, wi]
0 <= ui, vi < n
ui != vi
1 <= wi <= 10
0 <= k <= 300
1 <= t <= 600
The input graph is guaranteed to be a DAG.
There are no duplicate edges.

Related LeetCode Problems

Practice more problems similar to Maximum Weighted K-Edge Path.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.