thảo luận Leetcode mỗi ngày

  • Người tạo chủ đề Người tạo chủ đề _Gia_Cat_Luong_
  • Ngày bắt đầu Ngày bắt đầu
Trạng thái
Không mở để trả lời thêm.
hcmus à
V092S5K.gif
k bao nhiêu đây
mình ở HN fen ơi :doubt:
 
Brute force, hên là làm được, ban đầu đọc đề cứ nghĩ lại phải đi đọc giải tiếp.

Python:
class Solution:
    def maximumImportance(self, n: int, roads: List[List[int]]) -> int:
        edge_count = {}

        for x, y in roads:
            if x not in edge_count:
                edge_count[x] = 0
            edge_count[x] += 1
            if y not in edge_count:
                edge_count[y] = 0
            edge_count[y] += 1
        
        h = []
        for c, count in edge_count.items():
            heapq.heappush(h, (-count, c))

        importance_map = {}
        importance = n
        while len(h):
            city =  heapq.heappop(h)[1]
            importance_map[city] = importance
            importance -=1

        res = 0
        for x, y in roads:
            res += importance_map[x] + importance_map[y]
        return res
 
Mã:
impl Solution {
    pub fn maximum_importance(n: i32, roads: Vec<Vec<i32>>) -> i64 {
        let mut vertex_freq = vec![0; n as usize];

        for edge in roads {
            vertex_freq[edge[0] as usize] += 1;
            vertex_freq[edge[1] as usize] += 1;
        }

        vertex_freq.sort_unstable();

        let result =
            vertex_freq.into_iter().enumerate().fold(0, |sum, (i, freq)| {
                sum + (i + 1) * freq
            });

        result as i64
    }
}
 
@billy_don :smile:
C#:
public class Solution {
    public long MaximumImportance(int n, int[][] roads) {
        long[] nodes = new long[n];
        for(int i = 0; i<roads.Length; i++)
        {
            for(int j = 0; j<2; j++)
            {
                nodes[roads[i][j]]++;
            }
        }
        Array.Sort(nodes);
        long result = 0;
        for(int i = n-1; i>=0; i--)
        {
            result += (i+1) * nodes[i];
        }
        return result;
    }
}
 
JavaScript:
var maximumImportance = function(n, roads) {
    const connected = new Array(n).fill(0);
    for (const [a, b] of roads) {
        connected[a]++;
        connected[b]++;
    }

    connected.sort((a, b) => b - a);

    let res = 0;
    let i = n;
    for (const c of connected) {
        res += c * i--;
    }

    return res;
};

@danghieu1709 đâu rồi chưa up nữa :rolleyes:
 
@billy_don :smile:
C#:
public class Solution {
    public long MaximumImportance(int n, int[][] roads) {
        long[] nodes = new long[n];
        for(int i = 0; i<roads.Length; i++)
        {
            for(int j = 0; j<2; j++)
            {
                nodes[roads[i][j]]++;
            }
        }
        Array.Sort(nodes);
        long result = 0;
        for(int i = n-1; i>=0; i--)
        {
            result += (i+1) * nodes[i];
        }
        return result;
    }
}
Với constant <= 2 thì truy thẳng index luôn khỏi for cho dài dòng. Code đỡ dài hơn rồi đó :D
 
In-degree. Nhạt như nước ốc ao bèo 😌
JavaScript:
function maximumImportance(n: number, roads: number[][]): number {
    const arr = new Array(n).fill(0);
    for (const [from, to] of roads) {
        arr[from]++;
        arr[to]++;
    }
    arr.sort((a,b) => a - b);
    let val = 1, res = 0;
    for (const item of arr) {
        res+= val * item;
        val++
    }
    return res;
};
 
Trạng thái
Không mở để trả lời thêm.

Thống kê chủ đề

Ngày tạo
_Gia_Cat_Luong_,
Người trả lời cuối
Vipluckystar,
Trả lời
17.755
Lượt xem
1.212.569
Quay lại
Lên đầu trang