Posts

Showing posts with the label in degree

277. Find the Celebrity

Image
https://leetcode.com/problems/find-the-celebrity/ https://www.lintcode.com/problem/find-the-celebrity/description https://github.com/openset/leetcode/tree/master/problems/find-the-celebrity Suppose you are at a party with  n  people (labeled from  0  to  n - 1 ) and among them, there may exist one celebrity. The definition of a celebrity is that all the other  n - 1  people know him/her but he/she does not know any of them. Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get information of whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense). You are given a helper function  bool knows(a, b)  which tells you whether A knows B. Implement a function  int findCelebrity(n) . There will be exactly one celebrity if he/...

997. Find the Town Judge

https://leetcode.com/problems/find-the-town-judge/ In a town, there are  N  people labelled from  1  to  N .  There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: The town judge trusts nobody. Everybody (except for the town judge) trusts the town judge. There is exactly one person that satisfies properties 1 and 2. You are given  trust , an array of pairs  trust[i] = [a, b]  representing that the person labelled  a  trusts the person labelled  b . If the town judge exists and can be identified, return the label of the town judge.  Otherwise, return  -1 . Example 1: Input: N = 2 , trust = [[1,2]] Output: 2 Example 2: Input: N = 3 , trust = [[1,3],[2,3]] Output: 3 Example 3: Input: N = 3 , trust = [[1,3],[2,3],[3,1]] Output: -1 Example 4: Input: N = 3 , trust = [[1,2],[2,3]] Output: -1 Example 5: Input: N = 4 , trust = [[1,...

802. Find Eventual Safe States

Image
https://leetcode.com/problems/find-eventual-safe-states/ In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.  If we reach a node that is terminal (that is, it has no outgoing directed edges), we stop. Now, say our starting node is  eventually safe  if and only if we must eventually walk to a terminal node.  More specifically, there exists a natural number  K  so that for any choice of where to walk, we must have stopped at a terminal node in less than  K  steps. Which nodes are eventually safe?  Return them as an array in sorted order. The directed graph has  N  nodes with labels  0, 1, ..., N-1 , where  N  is the length of  graph .  The graph is given in the following form:  graph[i]  is a list of labels  j  such that  (i, j)  is a directed edge of the graph. Example: Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]...