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.
V092S5K.gif
ác quỉ cày coin, mốt có áo nhớ phát card nha minh chủ
 
Tụi nó mới fix có lại streak hay sao ấy, mà bài thì vẫn trùng ko đổi.
Lại ăn 1 cái badge tháng :ah:
 
làm dc bài med, nhưng vẫn phải mò xem hint có trùng ý tưởng ko mới dám implement, không dc như mấy bác gặp là múc, đụng là trụng
Java:
class Solution {
    public int minChanges(int[] nums, int k) {
        int n= nums.length;
        Map<Integer, Integer> freq = new HashMap();
        int[] maxDiff = new int[k+1];
        for(int i =0 ; i < n/2;i++){
            int diff = Math.abs(nums[i]-nums[n-1-i]);
            freq.put(diff, freq.getOrDefault(diff,0)+1);
            int greater = Math.max(nums[i],nums[n-1-i]);
            int smaller = nums[i]+nums[n-1-i] - greater;
            maxDiff[Math.max(greater - 0, k - smaller)]++;
        }

        for(int i = 1; i<=k;i++){
            maxDiff[i] +=maxDiff[i-1];
        }
        int res =n;
        for(int x:freq.keySet()){
            int changeBoth=x==0?0:maxDiff[x-1];
            int changeOne =n/2 - freq.get(x);
            res=Math.min(res,changeOne + changeBoth);
        }
        return res;
    }
}
 
C#:
public class Solution {
    public double MaxProbability(int n, int[][] edges, double[] succProb, int start_node, int end_node)
    {
        Dictionary<int, Node> graph = new();

        for (int i = 0; i < edges.Length; i++)
        {
            graph.TryGetValue(edges[i][0], out var node1);
            if (node1 == null)
            {
                node1 = new();
                node1.id = edges[i][0];
                graph.Add(node1.id, node1);
            }
            graph.TryGetValue(edges[i][1], out var node2);
            if (node2 == null)
            {
                node2 = new();
                node2.id = edges[i][1];
                graph.Add(node2.id, node2);
            }

            double rate = succProb[i];
            node1.neighbors.Add(node2.id);
            node1.pathCosts.Add(rate);
            node2.neighbors.Add(node1.id);
            node2.pathCosts.Add(rate);
        }

        PriorityQueue<int, double> q = new();
        Dictionary<int, double> distance = new();
        q.Enqueue(start_node, -1);
        distance.Add(start_node, 1);
        while (q.Count > 0)
        {
            int nodeId = q.Dequeue();
            graph.TryGetValue(nodeId, out var currentNode);
            if (currentNode == null)
            {
                return 0;
            }
            distance.TryGetValue(nodeId, out double currentCost);
            if (nodeId == end_node)
            {
                return currentCost;
            }
            for (int i = 0; i < currentNode.neighbors.Count; i++)
            {
                int neighborId = currentNode.neighbors[i];
                distance.TryGetValue(neighborId, out double recorded);
                double newCost = currentCost * currentNode.pathCosts[i];
                if (newCost <= recorded)
                {
                    continue;
                }

                distance[neighborId] = newCost;
                q.Enqueue(neighborId, -newCost);
            }
        }

        return 0;
    }

    public class Node
    {
        public int id;
        public List<int> neighbors = new();
        public List<double> pathCosts = new();
    }

}
 
C#:
public class Solution {
    public double MaxProbability(int n, int[][] edges, double[] succProb, int start_node, int end_node)
    {
        Dictionary<int, Node> graph = new();

        for (int i = 0; i < edges.Length; i++)
        {
            graph.TryGetValue(edges[i][0], out var node1);
            if (node1 == null)
            {
                node1 = new();
                node1.id = edges[i][0];
                graph.Add(node1.id, node1);
            }
            graph.TryGetValue(edges[i][1], out var node2);
            if (node2 == null)
            {
                node2 = new();
                node2.id = edges[i][1];
                graph.Add(node2.id, node2);
            }

            double rate = succProb[i];
            node1.neighbors.Add(node2.id);
            node1.pathCosts.Add(rate);
            node2.neighbors.Add(node1.id);
            node2.pathCosts.Add(rate);
        }

        PriorityQueue<int, double> q = new();
        Dictionary<int, double> distance = new();
        q.Enqueue(start_node, -1);
        distance.Add(start_node, 1);
        while (q.Count > 0)
        {
            int nodeId = q.Dequeue();
            graph.TryGetValue(nodeId, out var currentNode);
            if (currentNode == null)
            {
                return 0;
            }
            distance.TryGetValue(nodeId, out double currentCost);
            if (nodeId == end_node)
            {
                return currentCost;
            }
            for (int i = 0; i < currentNode.neighbors.Count; i++)
            {
                int neighborId = currentNode.neighbors[i];
                distance.TryGetValue(neighborId, out double recorded);
                double newCost = currentCost * currentNode.pathCosts[i];
                if (newCost <= recorded)
                {
                    continue;
                }

                distance[neighborId] = newCost;
                q.Enqueue(neighborId, -newCost);
            }
        }

        return 0;
    }

    public class Node
    {
        public int id;
        public List<int> neighbors = new();
        public List<double> pathCosts = new();
    }

}
câu này mới ra hôm t5 chứ đâu bác
rKvNKm2.png
 
ghXpJrI.png
Nhớ nhầm đối tượng à, mà không có ielts sao qua được mỹ đế
Tiếng Anh học dễ mà, ra trung tâm học kết hợp xem film.
Mà tiếng Anh tùy vô năng khiếu cmnr, cũng ít thấy mấy ông dev nói tiếng Anh ngon. Ko kiên trì làm Leetcode đc 1 2 năm thì khó học tiếng Anh lắm, ăn thua ở độ kiên trì thôi.
Bên Mỹ nhiều người Việt sống mấy chục năm ko biết tiếng Anh là bt.

via theNEXTvoz for iPhone
 
Python:
class Solution:
    def construct2DArray(self, original: List[int], m: int, n: int) -> List[List[int]]:
        if len(original) != m*n:
            return []
        return [[original[i*n + j] for j in range(n)] for i in range(m)]
 
Sửa lần cuối:
Mã:
class Solution {

    /**
     * @param Integer[] $original
     * @param Integer $m
     * @param Integer $n
     * @return Integer[][]
     */
    function construct2DArray($original, $m, $n) {
        if (count($original) != $m*$n) return [];

        $offset = 0;
        $ans = [];
        for ($r = 0; $r < $m; $r++) {
            $ans[] = array_slice($original, $offset, $n);
            $offset += $n;
        }

        return $ans;
    }
}
 
C-like:
impl Solution {
    pub fn construct2_d_array(original: Vec<i32>, m: i32, n: i32) -> Vec<Vec<i32>> {
        let (m, n) = (m as usize, n as usize);
        if original.len() != m * n {
            return Vec::new();
        }
        original.as_slice().chunks(n).map(|chunk| chunk.to_owned()).collect()
    }
}
 
điểm danh cái đã
Java:
class Solution {
    public int[][] construct2DArray(int[] original, int m, int n) {
        if(original.length != m*n) return new int[0][0];
        int[][] res = new int[m][n];
        int ind = 0;
        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                res[i][j] = original[ind++];
            }
        }
        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.974
Quay lại
Lên đầu trang