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.
#pragma GCC optimize("O3", "unroll-loops")
class Solution {
public:
   int findJudge(int n, vector<vector<int>>& trust) {
      int trusting[1001]={0}, trusted[1001]={0};//1 <= n <= 1000
      for (vector<int>& e: trust){
          trusting[e[0]]++; //outdeg
          trusted[e[1]]++; // indeg
      }
      for(int i=1; i<=n; i++)
         if (trusting[i]==0 && trusted[i]==n-1) return i;
     return -1; 
  }
};

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章