알고리즘/leetcode

leetcode - Find the Town Judge - easy

유이얼 2020. 9. 22. 20:50

https://leetcode.com/problems/find-the-town-judge/ 

 

Find the Town Judge - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

class Solution {
public:
    int findJudge(int n, vector<vector<int>>& trust) {
        vector<bool> first_(n+1);
        vector<int> second_(n+1);
        int size = trust.size();
        for (int i = 0; i < size; ++i) {
            first_[trust[i][0]] = true;
            ++second_[trust[i][1]];
        }
        
        int ans = -1;
        for (int i = 1; i <= n; ++i) {
            if (first_[i]) continue;
            if (second_[i] != n - 1) continue;
            if (ans != -1) return -1;
            ans = i;
        }
        return ans;
    }
};