일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 릿코드
- dfs
- 자연어처리
- GenerativeAI
- 파이썬알고리즘
- 리트코드
- 파이썬기초100제
- two-pointer
- gcp
- LeetCode
- codeup
- sql코테
- Python
- Greedy
- nlp
- Python3
- GenAI
- 투포인터
- 생성형AI
- 알고리즘
- heap
- Stack
- array
- 코드업
- 파이썬
- SQL
- slidingwindow
- 슬라이딩윈도우
- stratascratch
- 니트코드
Archives
- Today
- Total
Tech for good
[Leetcode/Graph (In-Degree/Out-Degree)] 277. Find the Celebrity 본문
IT/Computer Science
[Leetcode/Graph (In-Degree/Out-Degree)] 277. Find the Celebrity
Diana Kang 2025. 6. 2. 22:42# The knows API is already defined for you.
# return a bool, whether a knows b
# def knows(a: int, b: int) -> bool:
class Solution:
def findCelebrity(self, n: int) -> int:
# Step 1: Find the celebrity candidate
candidate = 0
for i in range(1, n):
if knows(candidate, i):
# Candidate knows i → candidate is not the celebrity
candidate = i
# Else: candidate doesn't know i → i can't be the celebrity
# Step 2: Verify the candidate
for i in range(n):
if i == candidate:
continue
# Candidate must not know anyone else
# Everyone else must know the candidate
if knows(candidate, i) or not knows(i, candidate):
return -1
return candidate
'IT > Computer Science' 카테고리의 다른 글
[Leetcode/Greedy] 55. Jump Game (1) | 2025.06.09 |
---|---|
[Leetcode/Linked List] 206. Reverse Linked List (0) | 2025.06.03 |
[Leetcode/Graph (In-Degree/Out-Degree)] 997. Find the Town Judge (0) | 2025.06.02 |
[Neetcode/Linked List] Linked List Cycle Detection (0) | 2025.06.02 |
[Neetcode/Linked List] Merge Two Sorted Linked Lists (0) | 2025.06.02 |