Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js sol

/**
 * @param {number} n
 * @param {number[][]} trust
 * @return {number}
 */
var findJudge = function(n, trust) {
    // If only the judge exists return the judge
    if (!trust.length && n === 1) return n
    
    // find the number everyone trusts
    // and find the number that trusts nobody
    // if the numbers of the two conditions above exists and are equal 
    // and the there is no trust[i][0] that matches the value of the statements above 
    // return the number else return -1
    let maxTrustFreq = -Infinity, trustFreq = new Map()
    
    for (const [truster, trustee] of trust) {
       if (!trustFreq.has(trustee)) {
           trustFreq.set(trustee, 1)
       } else {
           trustFreq.set(trustee, trustFreq.get(trustee) + 1) 
       }
        
       // Find running max trust value
       const freq = trustFreq.get(trustee)
       if (freq > maxTrustFreq) {
           maxTrustFreq = freq
       }
    }
    
    const trustBank = new Map(trust)
    for (let i = 1; i <= n; i++) {
        if (!trustBank.has(i) && maxTrustFreq === n - 1) return i
    }
    
    return -1
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: get gravatar javascript 
Javascript :: javascript check if valid url 
Javascript :: javascript how to multiply numbers 
Javascript :: build function component react 
Javascript :: nestjs openapi yaml file 
Javascript :: javascript get script path name 
Javascript :: jacascript loop array 
Javascript :: rename data table button 
Javascript :: Lodash Cypress for each function 
Javascript :: add grepper code 
Javascript :: evaluate polynomial 
Javascript :: The app structure generator Express 
Javascript :: create array, fill with spaces, convert to string and concat 
Javascript :: lement.style { } 
Javascript :: how to use classnames 
Javascript :: Node-Red: Bit Switch 
Javascript :: trigger many url calls JavaScript 
Javascript :: cant find variable idbindex react native 
Javascript :: how to merge data rn 
Javascript :: custu, loading next js 
Javascript :: axios with load more 
Javascript :: int[] arr = new int[5]; for(int i=0; i<arr.length; i++){ arr[i] = i; } for(int i=0; i<arr.length; i++) { System.out.print(arr[i]); } 
Javascript :: Private slots are new and can be created via Private slot checks 
Javascript :: npm init step by step 
Javascript :: react js exoirt examoek 
Javascript :: python range equivalent in javascript 
Javascript :: dynamically create html table in javascript 
Javascript :: Spread syntax in ES6 
Javascript :: this ....object of. 
Javascript :: ReactComponent as DeleteIcon 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =