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.
Java:
class Solution {
    int U = 0;
    int V = 1;
    public long maximumImportance(int n, int[][] roads) {
        int[] adjacentCounter = linkCounter(roads, n);
        Arrays.sort(adjacentCounter);

        long sum = 0;
        for (int i = n; i>= 1; i--) {
            sum += 1L * adjacentCounter[i - 1] * i;
        }

        return sum;
    }

    public int[] linkCounter(int[][] roads, int n) {
        int[] adjacentCounter = new int[n];
        for (int[] road : roads) {
            adjacentCounter[road[U]]++;
            adjacentCounter[road[V]]++;
        }

        return adjacentCounter;
    }
}
 
Java:
class Solution {
    public long maximumImportance(int n, int[][] roads) {
        long[] count = new long[n];
        long maxSum = 0;
        for(int[] road:roads){
            count[road[0]]++;
            count[road[1]]++;
        }

        Arrays.sort(count);
       
        for(int i = count.length -1; i>=0;i--){
            maxSum += count[i]*n;
            n--;
        }
        return maxSum;
    }
}
Đọc đề 15p mới hiểu là bài toán đánh số mà thấy comment ai cũng kêu dễ
V092S5K.gif
đọc đề chưa hiểu thì xuống xem testcase chạy như nào chứ fen ngồi ôm đề làm gì
1BW9Wj4.png
 
Python:
class Solution:
    def maximumImportance(self, n: int, roads: List[List[int]]) -> int:
        return sum([j * (n - i) for i, j in enumerate(sorted(Counter([x for y in roads for x in (y[0], y[1])]).values(), reverse = True))])
 
Sửa lần cuối:
Python:
class Solution:
    def maximumImportance(self, n: int, roads: List[List[int]]) -> int:
        return sum([j * (n - i) for i, j in enumerate(sorted(Counter([x for y in roads for x in (y[0], y[1])]).values(), reverse = True))])
Có chắc là O(n) không vậy thím? Em thấy có sorted mà nhỉ?
 
Mã:
class Solution:
    def maximumImportance(self, n: int, roads: List[List[int]]) -> int:
        mp = {}
        ans = 0

        for [s , e] in roads:
            mp[s] = mp.get(s , 0) + 1
            mp[e] = mp.get(e , 0) + 1
        
        pq = []
        for v in mp.values(): heapq.heappush(pq , -v)

        for i in range(n , 0 , -1):
            a = 0
            if pq: a = heappop(pq)
            ans += -1 * a * i       

        return ans
 
Mang ngay đề contest chính thức đến đây. Mang ngay graph của leetcode đến đây. Anh em 2Q Gang và thí sinh tự do của chúng tôi đã sẵn sàng hết rồi. Mang ngay những câu khó nhất đến đây, khó hơn codeforce cho chúng tôi. Tổ chức thi luôn đi. Anh em đâu, xung phong
wryvDSH.png
đúng hcmus có khác
 
Bài này khá intuitive, ý tưởng đơn giản là tham lam gán trọng số giảm dần theo đỉnh có degree (số lượng connection) từ lớn đến bé.

Python:
class Solution:
    def maximumImportance(self, n: int, roads: List[List[int]]) -> int:
        d = defaultdict(int)
        w = [0] * n

        for road in roads:
            [u, v] = road
            d[u] += 1
            d[v] += 1
        
        sorted_d = sorted([(v, i) for i, v in d.items()], reverse=True)

        for j, e in enumerate(sorted_d):
            (v, i) = e
            w[i] = n - j

        ans = 0
        for road in roads:
            [u, v] = road
            ans += (w[u] + w[v])
        return ans
 
Hên quá còn kịp điểm danh :) Dạo này 1 bài daily tới mấy page luôn à, ae siêng hay thất nghiệp tập thể vậy
C#:
public class Solution
{
    public long MaximumImportance(int n, int[][] roads)
    {
        int[] cities = new int[n];
        for (int i = 0; i < roads.Length; i++)
        {
            int u = roads[i][0];
            int v = roads[i][1];
            cities[u]++;
            cities[v]++;
        }

        Array.Sort(cities);
        long result = 0;
        while (n > 0)
        {
            result += cities[n - 1] * (long)n;
            n--;
        }

        return result;
    }
}
 
Hên quá còn kịp điểm danh :) Dạo này 1 bài daily tới mấy page luôn à, ae siêng hay thất nghiệp tập thể vậy
C#:
public class Solution
{
    public long MaximumImportance(int n, int[][] roads)
    {
        int[] cities = new int[n];
        for (int i = 0; i < roads.Length; i++)
        {
            int u = roads[i][0];
            int v = roads[i][1];
            cities[u]++;
            cities[v]++;
        }

        Array.Sort(cities);
        long result = 0;
        while (n > 0)
        {
            result += cities[n - 1] * (long)n;
            n--;
        }

        return result;
    }
}
bai de thi the thoi
TGDQ7cT.png
 
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.566
Quay lại
Lên đầu trang