I can do LC in 9 languages!

in programming •  2 years ago 

Well, I also know others but which are not on LC. For example, VBScript, BASH, Lua, x86 Assembly, Delphi/Pascal...

Python 2/3

class Solution:
    def edgeScore(self, edges: List[int]) -> int:
        n = len(edges)
        G = [0] * n
        for i, v in enumerate(edges):
            G[v] += i
        x = max(G)
        return G.index(x)

C

#define max(a, b) (a)>(b)?(a):(b)

int edgeScore(int* edges, int n){
    long long *G = calloc(n, sizeof(long long));
    long long mx = 0;
    for (int i = 0; i < n; ++ i) {
        G[edges[i]] += i;
        mx = max(mx, G[edges[i]]);
    }
    int ans = -1;
    for (int i = 0; i < n; ++ i) {
        if (G[i] == mx) {
            ans = i; 
            break;
        }
    }
    free(G);
    return ans;
}

C Sharp

public class Solution {
    public int EdgeScore(int[] edges) {
        var n = edges.Length;
        var G = new long[n];
        for (var i = 0; i < n; ++ i) {
            G[edges[i]] += i;
        }
        long mx = G.Max();
        return G.ToList().IndexOf(mx);
    }
}

Javascript

/**
 * @param {number[]} edges
 * @return {number}
 */
var edgeScore = function(edges) {
    const n = edges.length;
    const G = new Map();
    for (let i = 0; i < n; ++ i) {
        const v = G.has(edges[i]) ? G.get(edges[i]) : 0;
        G.set(edges[i], v + i);
    }
    const mx = Math.max(...G.values());
    for (let i = 0; i < n; ++ i) {
        if (G.get(i) == mx) {
            return i;
        }
    }
};

Rust

impl Solution {
    pub fn edge_score(edges: Vec<i32>) -> i32 {
        let n = edges.len();
        let mut G = vec![0; n];
        for (i, &v) in edges.iter().enumerate() {
            G[v as usize] += i;
        }
        let mx = G.iter().max().unwrap();
        return G.iter().position(|r| r == mx).unwrap() as i32;
    }
}

Go

func edgeScore(edges []int) int {
    var n = len(edges)
    var G = make([]int, n)
    for i, v := range edges {
        G[v] += i
    }
    var mx = maxArray(G)
    return indexOf(mx, G)
}

func maxArray(a []int) int {
    ans := a[0]
    for i := 1; i < len(a); i ++ {
        if a[i] > ans {
            ans = a[i]
        }
    }
    return ans
}

func indexOf(element int, data []int) (int) {
   for k, v := range data {
       if element == v {
           return k
       }
   }
   return -1
}

PHP

class Solution {

    /**
     * @param Integer[] $edges
     * @return Integer
     */
    function edgeScore($edges) {
        $n = count($edges);
        $G = array();
        $mx = 0;
        for ($i = 0; $i < $n; ++ $i) {
            $v = $edges[$i];
            $G[$v] = isset($G[$v]) ? $G[$v] : 0;
            $G[$v] += $i;
            $mx = max($mx, $G[$v]);
        }
        for ($i = 0; $i < $n; ++ $i) {
            if ($G[$i] == $mx) {
                return $i;
            }
        }
    }
}

C++

class Solution {
public:
    int edgeScore(vector<int>& edges) {
        int n = static_cast<int>(edges.size());
        vector<long long> G(n, 0);
        for (int i = 0; i < n; ++ i) {
            G[edges[i]] += i;            
        }
        long long mx = *max_element(begin(G), end(G));
        return distance(begin(G), find(begin(G), end(G), mx));
    }
};

Java

class Solution {
    public int edgeScore(int[] edges) {
        var n = edges.length;
        var G = new long[n];
        long mx = 0;
        for (var i = 0; i < n; ++ i) {
            G[edges[i]] += i;
            mx = Long.max(mx, G[edges[i]]);
        }
        final long mmx = mx;
        return IntStream.range(0, n).
            filter(i -> mmx == G[i]).
            findFirst().
            orElse(-1);
    }
}

Steem to the Moon!

You can swap the STEEM/SBD to USDT (TRC-20) via Steem2USDT

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!