Skip to content

Practice Number of Possible Sets of Closing Branches

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

Hard#2959
LeetCode Problem

Number of Possible Sets of Closing Branches

Problem Statement

There is a company with n branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads. The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, they have decided to close down some of these branches (possibly none). However, they want to ensure that the remaining branches have a distance of at most maxDistance from each other. The distance between two branches is the minimum total traveled length needed to reach one branch from another. You are given integers n, maxDistance, and a 0-indexed 2D array roads, where roads[i] = [ui, vi, wi] represents the undirected road between branches ui and vi with length wi. Return the number of possible sets of closing branches, so that any branch has a distance of at most maxDistance from any other. Note that, after closing a branch, the company will no longer have access to any roads connected to it. Note that, multiple roads are allowed.

Source: Number of Possible Sets of Closing Branches on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 3, maxDistance = 5, roads = [[0,1,2],[1,2,10],[0,2,10]]
Output
5

# Example 2

Input
n = 3, maxDistance = 5, roads = [[0,1,20],[0,1,10],[1,2,2],[0,2,2]]
Output
7

# Example 3

Input
n = 1, maxDistance = 10, roads = []
Output
2

Constraints

1 <= n <= 10
1 <= maxDistance <= 10^5
0 <= roads.length <= 1000
roads[i].length == 3
0 <= ui, vi <= n - 1
ui != vi
1 <= wi <= 1000
All branches are reachable from each other by traveling some roads.

Related LeetCode Problems

Practice more problems similar to Number of Possible Sets of Closing Branches.

Review your practice with PhantomCodeAI

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