thảo luận [Học Tập] Topic thuật toán

bạn trên dùng F# mà, tôi khá chắc luôn.

hai thằng này syntax gần như tương tự, nhưng F# thì có name convention của C# là function là pascal case :v

p/s: thực ra cũng chả có gì to tát, cơ mà vấn đề là bảo sai language paste vào nó không chạy :)
Nhưng mà mấy cái này không phổ biến lắm nhỉ, k biết thim kia học làm gì
 
Nhưng mà mấy cái này không phổ biến lắm nhỉ, k biết thim kia học làm gì
ocaml thì đúng là không phổ biến, nhưng F# thì đang lên ngon, thi thoảng vẫn leo vào top 10.

vì F# là cùng ecosystem với C#, mấy cái .NET nó dùng chung dc hết, code functional với nhiều người là lựa chọn sướng hơn.
 
Last edited:
Nhưng mà mấy cái này không phổ biến lắm nhỉ, k biết thim kia học làm gì
FP code ngắn hơn OOP ~50%
Capture.PNG

F# đơn giản hơn C#
F# is built for simplicity. F# currently knows 71 keywords, C# has above 110.
Mà code cái gì cảm thấy vui là được, quan trọng gì.
 
1.png


Bài này độ phức tạp có được tính là O(n) ko mấy fen. Nếu ko phải thì có cách nào giải đc với O(n) ko. Thêm nữa là cách giải trên hơi cheat vì làm thay đổi array nums. Có cách nào làm với độ phức tạp O(n) mà ko cheat ko nhỉ.

Đề bài tìm số lớn nhất thứ 3 nếu ko có thì return ra số lớn nhất
ví dụ
[3,2,1,0] thì out put là 1
[3,2,2,2] thì out put là 3
 
View attachment 530369

Bài này độ phức tạp có được tính là O(n) ko mấy fen. Nếu ko phải thì có cách nào giải đc với O(n) ko. Thêm nữa là cách giải trên hơi cheat vì làm thay đổi array nums. Có cách nào làm với độ phức tạp O(n) mà ko cheat ko nhỉ.

Đề bài tìm số lớn nhất thứ 3 nếu ko có thì return ra số lớn nhất
ví dụ
[3,2,1,0] thì out put là 1
[3,2,2,2] thì out put là 3
xài cái hàm sort thì O gì nữa phen, ông có biết ở dưới hàm sort đó nó implement cái thuật toán sort nào đâu. bài này O(n) được. Duyệt từng phần tử rồi add vào một cái mảng có 3 phần tử. nếu mảng full rồi thì so sánh với 3 phần tử(result) đó rồi thay thế số vừa so sánh cho phần tử nhỏ nhất. lúc return thì if result[1] = result[2] return result[0] else return result[2].
 
Last edited:
xài cái hàm sort thì O gì nữa phen, ông có biết ở dưới hàm sort đó nó implement cái thuật toán sort nào đâu. bài này O(n) được. Duyệt từng phần tử rồi add vào một cái mảng có 3 phần tử. nếu mảng full rồi thì so sánh với 3 phần tử(result) đó rồi thay thế số vừa so sánh cho phần tử nhỏ nhất. lúc return thì if result[1] = result[2] return result[0] else return result[2].
Mới xong phần learn Array 101. Thôi tui quay về với chính đạo đây. Chiều giờ muốn banh não :beat_shot:
 
View attachment 530369

Bài này độ phức tạp có được tính là O(n) ko mấy fen. Nếu ko phải thì có cách nào giải đc với O(n) ko. Thêm nữa là cách giải trên hơi cheat vì làm thay đổi array nums. Có cách nào làm với độ phức tạp O(n) mà ko cheat ko nhỉ.

Đề bài tìm số lớn nhất thứ 3 nếu ko có thì return ra số lớn nhất
ví dụ
[3,2,1,0] thì out put là 1
[3,2,2,2] thì out put là 3
dễ thế này cũng hỏi à fen

bước 1:

duyệt mảng: tìm max, oánh dấu vị trí đó phát True đi, gán res = x_i

bước 2

duyệt mảng: tìm max, gặp True dùng từ khóa continue, oánh dấu True, gán res=min(res,x_k)

bước 3

dduyệt mảng: tìm max, gặp True dùng từ khóa continue, oánh dấu True, gán res=min(res,x_j)

bước 4: return res
 
Thấy không ai nhận nên mình thử làm cho vui (Bảo đảm chỉ đúng với 2 test cases trong bài :LOL:, anh em chém nhè nhẹ @Nipin ):

Python:
from collections import defaultdict


def make_rooted_tree(list_of_lists):
    element_dict = defaultdict(list)
    root = 0
    ret = []
    print("Input: {}".format(list_of_lists))
    for i in range(0, len(list_of_lists)):
        element_dict[list_of_lists[i][0]].append(list_of_lists[i][1])

    # find root and candidates to delete
    # find root node
    parents = list(element_dict.keys())
    children = list(element_dict.values())
    children = [item for sublist in children for item in sublist]
    children = list(set(children))
    print("List of parent nodes: {}".format(parents))
    print("List of children nodes: {}".format(children))
    # case A: if node in parents but not in children, it's a root
    for value in parents:
        if value not in children:
            root = value
            break
    # case B: if root cannot be found above, choose the node with the most children
    if root == 0:
        max_children = 0
        for key in element_dict:
            number_of_chidlren = len(element_dict[key])
            if number_of_chidlren > max_children:
                max_children = number_of_chidlren
                root = key
    print("Root node is {}".format(root))
    # find candidate edges to delete
    children_of_root = element_dict[root]
    print(children_of_root)
    # if both nodes of an edge are children of root, then it should be removed
    for key in element_dict:
        if key in children_of_root:
            list_of_key_children = element_dict[key]
            for child in list_of_key_children:
                if child in children_of_root:
                    ret.append([key, child])
    # if root is a child of a node, edge [node,root] should be removed
    for key in element_dict:
        if root in element_dict[key]:
            ret.append([key, root])
    return ret


if __name__ == "__main__":
    a = [[1, 2], [1, 3], [2, 3]]
    b = [[1, 2], [2, 3], [3, 4], [4, 1], [1, 5]]
    deleted_edge_a = make_rooted_tree(a)
    print("Edge to be deleted: {} \n ==========================================================".format(deleted_edge_a))
    deleted_edge_b = make_rooted_tree(b)
    print("Edge to be deleted: {} \n ==========================================================".format(deleted_edge_b))
 
Dùng Disjoint Set:
Python:
class Solution:
    def findRedundantConnection(self, edges):
        arr = [i for i in range(len(edges))]
        for a, b in edges:
            if self.find(arr, a-1, b-1):
                return [a, b]
            self.union(arr, a-1, b-1)
        return "lmao"

    def root(self, arr, a):
        while arr[a] != a:
            a = arr[a]
        return a
    
    def union(self, arr, a, b):
        arr[self.root(arr, a)] = arr[self.root(arr, b)]
        
    def find(self, arr, a, b):
        return self.root(arr, a) == self.root(arr, b)
 
Dùng Disjoint Set:
Python:
class Solution:
    def findRedundantConnection(self, edges):
        arr = [i for i in range(len(edges))]
        for a, b in edges:
            if self.find(arr, a-1, b-1):
                return [a, b]
            self.union(arr, a-1, b-1)
        return "lmao"

    def root(self, arr, a):
        while arr[a] != a:
            a = arr[a]
        return a
   
    def union(self, arr, a, b):
        arr[self.root(arr, a)] = arr[self.root(arr, b)]
       
    def find(self, arr, a, b):
        return self.root(arr, a) == self.root(arr, b)
Thank you. Hôm nay học thêm được 1 cái data structure mới :love:
 
Dùng Disjoint Set:
Python:
class Solution:
    def findRedundantConnection(self, edges):
        arr = [i for i in range(len(edges))]
        for a, b in edges:
            if self.find(arr, a-1, b-1):
                return [a, b]
            self.union(arr, a-1, b-1)
        return "lmao"

    def root(self, arr, a):
        while arr[a] != a:
            a = arr[a]
        return a
  
    def union(self, arr, a, b):
        arr[self.root(arr, a)] = arr[self.root(arr, b)]
      
    def find(self, arr, a, b):
        return self.root(arr, a) == self.root(arr, b)
thực sự là đọc code này tui ko hiểu gì :ah::ah:
 
Dùng Disjoint Set:
Python:
class Solution:
    def findRedundantConnection(self, edges):
        arr = [i for i in range(len(edges))]
        for a, b in edges:
            if self.find(arr, a-1, b-1):
                return [a, b]
            self.union(arr, a-1, b-1)
        return "lmao"

    def root(self, arr, a):
        while arr[a] != a:
            a = arr[a]
        return a

    def union(self, arr, a, b):
        arr[self.root(arr, a)] = arr[self.root(arr, b)]
    
    def find(self, arr, a, b):
        return self.root(arr, a) == self.root(arr, b)
Cách này sai nhé, bài này là directed graph, phản ví dụ: [[2,3],[3,4],[1,4],[1,2]]
Link đề gốc ở leetcode: https://leetcode.com/problems/redundant-connection-ii/

Lần sau đồng chí nào đưa bài thì đưa cả constraint nhé.
 
Cách này sai nhé, bài này là directed graph, phản ví dụ: [[2,3],[3,4],[1,4],[1,2]]
Link đề gốc ở leetcode: https://leetcode.com/problems/redundant-connection-ii/

Lần sau đồng chí nào đưa bài thì đưa cả constraint nhé.
Tôi nghĩ vẫn áp dụng được cho DAG, khi duyệt các cạnh của đồ thị ví dụ cạnh e: u->v. nếu u và v chưa trong một cây thì việc chọn root cho một DisjoinSet sẽ là u mà không phải v. Như vậy là thỏa mãn.

còn thông thường đồ thị ko hướng ng ta hay join cây nhỏ vào cây to, và path compress để tăng tốc
 
Last edited:
Tôi nghĩ vẫn áp dụng được cho DAG, khi duyệt các cạnh của đồ thị ví dụ cạnh e: u->v. nếu u và v chưa trong một cây thì việc chọn root cho một DisjoinSet sẽ là u mà không phải v. Như vậy là thỏa mãn.

còn thông thường đồ thị ko hướng ng ta hay join cây nhỏ vào cây to, và path compress để tăng tốc
Cái ví dụ [[2,3],[3,4],[1,4],[1,2]] là DAG chứ j nữa?
 
Back
Top