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.
[codes on Leecode]https://leetcode.com/problems/find-the-town-judge/solutions/4764867/graph-solution-indeg-outdeg-array-vs-hash-table-81ms-beats-100/
[Leetcode playList]https://www.youtube.com/watch?v=WjrWkPysfRM&list=PLYRlUBnWnd5IdDHk2BjqXwesydU17z_xk
[Dynamic Programming Playlist]https://www.youtube.com/watch?v=30yq3fmE6E8&list=PLYRlUBnWnd5K_XYUesV9oc6M9ONXII61T
#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;
}
};
沒有留言:
張貼留言