Re: [閒聊] 每日leetcode
https://leetcode.com/problems/find-champion-ii
2924. Find Champion II
給你一個二維陣列表示的DAG,共有n個選手,[a,b]表示a比b強,找出一個選手比
其他所有人都強,如果存在多個選手沒人比他強返回-1。
1.直接遍歷edges數每個人的弱點數量,如果恰好只有一個人弱點數為0就返回他的id
Java Code
---------------------------------
class Solution {
public int findChampion(int n, int[][] edges) {
int[] weakness = new int[n];
for (int[] edge : edges) {
weakness[edge[1]]++;
}
int res = -1;
for (int i = 0; i < n; i++) {
if (weakness[i] == 0) {
if (res == -1) {
res = i;
} else {
return -1;
}
}
}
return res;
}
}
---------------------------------
--
https://i.imgur.com/bmDGM17.jpeg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.158.191.3 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1732619646.A.084.html
討論串 (同標題文章)
完整討論串 (本文為第 1155 之 1548 篇):