A content delivery setup has n servers numbered 0 to n - 1, joined by n - 1 one-way links so that, ignoring direction, every server is connected (the links form a tree). links[i] = [a, b] means data can flow from a to b.
Every server must be able to send data to the hub, server 0. You may flip the direction of any link. Write fewestFlips(n, links) that returns the fewest links you must flip so that every server can reach server 0.
Input: n = 5, links = [[0,1],[1,2],[3,1],[3,4]] Output: 3
Links 0→1, 1→2, and 3→4 point away from the hub and must be flipped. 3→1 already points toward it.
Input: n = 3, links = [[1,0],[2,0]] Output: 0
Both links already point at the hub.
1 <= n <= 50000links.length === n - 1Run 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).
I'm here to help with #419 Point Every Route at the Hub. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.