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.
topo sort nó có 2 kiểu giải dfs hoặc bfs mà
xjIzSG9.png
Em hỏi thử xem thím quen kiểu nào, hay cái nào cũng được không quan trọng, mà bfs cũng detect được cycle giống dfs
Java:
class Solution {
    int[] list;
    int idx;
    boolean hasCycle;
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        int n = numCourses;
        list = new int[n];
        idx = n - 1;
        hasCycle = false;
        List<Integer>[] G = new List[n];
        boolean[] visited = new boolean[n];

        for (int i = 0; i < numCourses; i++) {
            G[i] = new ArrayList<>();
        }

        for (int[] pre: prerequisites) {
            G[pre[1]].add(pre[0]);
        }

        for (int i = 0; i < numCourses; i++) {
            dfs(i, G, visited, new HashSet<>());
        }

        return hasCycle ? new int[0] : list;
    }

    private void dfs(int node, List<Integer>[] G, boolean[] visited, Set<Integer> set) {
        if (set.contains(node)) {
            hasCycle = true;
            return;
        }

        if (visited[node]) return;

        set.add(node);

        for (int adj: G[node]) {
            if (!visited[adj]) {
                dfs(adj, G, visited, set);
            }
        }

        visited[node] = true;
        list[idx] = node;
        idx--;
    }
}
contest vừa rồi vào trễ, còn 30' cũng ráng submit nhục quá :too_sad:
UKiCiKh.png

Xem tệp đính kèm 2669780
senior của thread mà rating trông bôi bác quá. Đề nghị chấn chỉnh lại gấp
JjcEGFL.gif
 
Tại trư quên chứ bộ
EcV5PPL.png
Trư vào test thử onsite interview trên leetcode làm vẫn full câu trước thời gian mà
Q8sGcLO.png

Đời người ai không có này nọ
MjfezZB.png

Để tuần này gỡ nè
EB2RUU6.gif

1725640423619.png
 
118 hard luôn kìa, gấp 3 toi rồi
Nhớ hồi 2023, định nhảy pv nên cày LC nhiều, bài hard cũng cố làm, có cái tháng phải quá nửa là bài hard cũng cố nhai.
q9IkI1E.png

Sau gần 2 năm, vẫn ngồi yên 1 chỗ, chả nhảy đi đâu
jIhcHFg.png

Giờ gặp mấy bài hard quá chắc toàn cop sol giữ streak xong kiếm bài med thế chỗ quá
uuv3zFk.png
 
Nhớ hồi 2023, định nhảy pv nên cày LC nhiều, bài hard cũng cố làm, có cái tháng phải quá nửa là bài hard cũng cố nhai.
q9IkI1E.png

Sau gần 2 năm, vẫn ngồi yên 1 chỗ, chả nhảy đi đâu
jIhcHFg.png

Giờ gặp mấy bài hard quá chắc toàn cop sol giữ streak xong kiếm bài med thế chỗ quá
uuv3zFk.png
Tháng 10 đẫm máu sắp tới rồi
yBBewst.gif


via theNEXTvoz for iPhone
 
Nhớ hồi 2023, định nhảy pv nên cày LC nhiều, bài hard cũng cố làm, có cái tháng phải quá nửa là bài hard cũng cố nhai.
q9IkI1E.png

Sau gần 2 năm, vẫn ngồi yên 1 chỗ, chả nhảy đi đâu
jIhcHFg.png

Giờ gặp mấy bài hard quá chắc toàn cop sol giữ streak xong kiếm bài med thế chỗ quá
uuv3zFk.png
osCpCsi.png
trong 800 câu đấy bao nhiêu câu cop khai mau
 
chủ yếu Hard chắc khoảng 10 20-30 câu thôi
JjcEGFL.gif
:beat_brick:
Java:
class Solution {
    int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    int m, n;

    public int longestIncreasingPath(int[][] matrix) {
        if (matrix == null || matrix.length == 0) return 0;

        m = matrix.length;
        n = matrix[0].length;
        int ans = 0;

        int[][] dp = new int[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                ans = Math.max(ans, dfs(i, j, matrix, dp));
            }
        }

        return ans;
    }

    private int dfs(int r, int c, int[][] matrix, int[][] dp) {
        if (dp[r][c] > 0) return dp[r][c];

        int max = 1;

        for (int[] d : dirs) {
            int nextR = r + d[0];
            int nextC = c + d[1];

            if (nextR >= 0 && nextR < m && nextC >= 0 && nextC < n && matrix[nextR][nextC] > matrix[r][c]) {
                max = Math.max(max, 1 + dfs(nextR, nextC, matrix, dp));
            }
        }

        dp[r][c] = max;

        return dp[r][c];
    }
}
 
Python:
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
        if not root:  
            return False
        return self.dfs(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)
    def dfs(self,head: ListNode, root: TreeNode) -> bool:
            if not head:  
                return True
            if not root:  
                return False
            if root.val != head.val:  
                return False
            return self.dfs(head.next, root.left) or self.dfs(head.next, root.right)
 
Làm 1 DFS TLE là sao ta, để nghiên cứu tiếp =((
Python:
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
        def travelList(node, pointer):
            if not pointer:
                return True
            
            if not node or node.val != pointer.val:
                return False
            return travelList(node.left, pointer.next) or travelList(node.right, pointer.next)
        def travelNode(node):
            if node == None:
                return False
    
            if node.val == head.val and travelList(node, head):
                return True
            
            return travelNode(node.left) or travelNode(node.right)
        return travelNode(root)
 
Sửa lần cuối:
Java:
class Solution {
    public boolean isSubPath(ListNode head, TreeNode root) {
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode tNode = queue.poll();
            if(tNode.val == head.val){
                if(dfs(head, tNode)) return true;
            }
            if(tNode.left!= null) queue.offer(tNode.left);
            if(tNode.right!=null) queue.offer(tNode.right);
        }
        return false;
    }
    public boolean dfs(ListNode node, TreeNode tNode){
        if(node!=null && tNode == null) return false;
        else if(node==null) return true;
        else if(node.val == tNode.val){
            return dfs(node.next,tNode.left) || dfs(node.next, tNode.right);
        }
        else return false;
    }
}
AsBPJOY.png
dfs cả 2 cái cùng 1 lúc quá sức r. load ko nổi
Java:
class Solution {
    public boolean isSubPath(ListNode head, TreeNode root) {
        if(root==null) return false;
        if(root.val == head.val){
            if(dfs(head, root)) return true;
        }
        return isSubPath(head, root.left)|| isSubPath(head, root.right);
    }
    public boolean dfs(ListNode node, TreeNode tNode){
        if(node==null) return true;
        else if(node!=null && tNode == null) return false;
        else if(node.val == tNode.val){
            return dfs(node.next,tNode.left) || dfs(node.next, tNode.right);
        }
        else return false;
    }
}
 
Sửa lần cuối:
C-like:
impl Solution {
    fn has_path_from_root(head_of_path: &Option<Box<ListNode>>, root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
        match (head_of_path, root) {
            (None, _) => true,
            (_, None) => false,
            (Some(head), Some(root)) => {
                if head.val != root.borrow().val {
                    false
                } else {
                    Self::has_path_from_root(&head.next, &root.borrow().left)
                        || Self::has_path_from_root(&head.next, &root.borrow().right)
                }
            }
        }
    }

    fn is_sub_path_borrow(head: &Option<Box<ListNode>>, root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
        if Self::has_path_from_root(head, root) {
            true
        } else {
            match root {
                None => false,
                Some(root) => {
                    Self::is_sub_path_borrow(head, &root.borrow().left)
                        || Self::is_sub_path_borrow(head, &root.borrow().right)
                }
            }
        }
    }

    pub fn is_sub_path(head: Option<Box<ListNode>> root: Option<Rc<RefCell<TreeNode>>>) -> bool {
        Self::is_sub_path_borrow(&head, &root)
    }
}
 
1725677485310.png

1725677418114.png

cho e hoir, tại sao ko return luôn n=0 đi ta, tại =0 là thỏa điều kiện đặt được n vào trong flowerbed rồi mà. Câu easy gì mà khó vãi
4gmOAMB.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.213.179
Quay lại
Lên đầu trang