A team draws its services as n nodes numbered 0 to n - 1, with links[i] = [a, b] meaning services a and b talk to each other (both ways).
A gateway is a service that is linked directly to every other service. Write findGateway(n, links) that returns the gateway's number, or -1 if there isn't one. If more than one service qualifies, return the smallest number.
No link appears twice and no service links to itself.
Input: n = 4, links = [[0,1],[1,2],[1,3]] Output: 1
Service 1 is linked to 0, 2, and 3: all three other services.
Input: n = 4, links = [[0,1],[1,2],[2,3]] Output: -1
Every service is missing a link to at least one other service.
2 <= n <= 100000 <= links.length <= 500000 <= a, b < n and a !== bRun 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 #405 Find the Gateway Service. 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.