Skip to the code
cf.
+10 XP

#472 Is the Cabling Budget Enough?

easy

An events company sets up n booths numbered 0 to n - 1 and gets quotes quotes[i] = [a, b, price] for running a cable between booths a and b. Cables work both ways.

Write withinBudget(n, quotes, budget) that returns true if every booth can be connected for a total of at most budget, and false otherwise (including when the booths can't be connected at all).

Example 1

Input: n = 4, quotes = [[0,1,1],[1,2,2],[0,2,4],[2,3,3],[1,3,5]], budget = 6
Output: true

The cheapest way to connect all booths costs exactly 6.

Example 2

Input: n = 4, quotes = [[0,1,1],[1,2,2],[0,2,4],[2,3,3],[1,3,5]], budget = 5
Output: false

No set of cables connecting all four booths costs less than 6.

Constraints

  • 1 <= n <= 10000
  • 0 <= quotes.length <= 50000
  • 1 <= price <= 10^4
  • 0 <= budget <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).