Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit365ec1c

Browse files
add a solution for 277
1 parentc87ddd7 commit365ec1c

File tree

1 file changed

+11
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+11
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_277.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33
publicclass_277 {
44

55
publicstaticclassSolution1 {
6+
/**
7+
* Credit: https://leetcode.com/problems/find-the-celebrity/solution/ approach 2
8+
* 1. we narrow down the possible celebrity candidate to only one person
9+
* 2. we check to make sure that every other person knows
10+
* this candidate and this candidate doesn't know any one of them, otherwise return -1
11+
*
12+
* We can think of this is a directed graph problem, a total of n vertices, the one vertex that has zero outgoing edges
13+
* and n - 1 incoming edges is the celebrity.
14+
*/
615
publicintfindCelebrity(intn) {
716
intcandidate =0;
817
for (inti =1;i <n;i++) {
918
if (knows(candidate,i)) {
19+
//this rules out the possibility that candidiate is a celebrity since he/she knows i
20+
//so we update candidate to be i, because at least i doesn't know anybody yet.
1021
candidate =i;
1122
}
1223
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp