Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
602 B
2
Indexable
Never
from collections import defaultdict

class Solution:
    def maximumImportance(self, n: int, roads: List[List[int]]) -> int:
        degree = defaultdict(int)
        for a, b in roads:
            degree[a] += 1
            degree[b] += 1
        
        items = sorted(degree.items(), key = lambda x: x[1], reverse = True)
        counter = n
        assignment = {}

        for item, _ in items:
            assignment[item] = n
            n -= 1
        
        ans = 0
        for a, b in roads:
            ans += assignment[a] + assignment[b]
        return ans
        
Leave a Comment