herrDeng網內搜尋

自訂搜尋

Ads

2024年2月22日 星期四

python C++計算 indegree解Leetcode 997 Find the Town Judge


python C++計算 indegree解Leetcode 997  Find the Town Judge
使用圖形解決方案。
由於信任中的每一對 e=(a, b) 實際上表示一種信任關係,可以將其視為有向邊。C++程式請進
---
Use graph solution.
Since each pair e=(a, b) in trust denotes in fact a trusting relation which can be consider as a directed edge.
  1. #pragma GCC optimize("O3", "unroll-loops")
  2. class Solution {
  3. public:
  4. int findJudge(int n, vector<vector<int>>& trust) {
  5. int trusting[1001]={0}, trusted[1001]={0};//1 <= n <= 1000
  6. for (vector<int>& e: trust){
  7. trusting[e[0]]++; //outdeg
  8. trusted[e[1]]++; // indeg
  9. }
  10. for(int i=1; i<=n; i++)
  11. if (trusting[i]==0 && trusted[i]==n-1) return i;
  12. return -1;
  13. }
  14. };

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章