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

https://www.geeksforgeeks.org/minimum-time-write-characters-using-insert-delete-copy-operation/
thằng này nó làm giống tui nè bác.
9iJzwOO.png
ừ nhưng mà nó chạy i=2 chứ đâu phải từ 1. Mà chỉ ghi lời giải chứ chả giải thích chứng minh gì. Tuy g4g bài nào cũng có lời giải nhưng code thì toàn rác, ví dụ như bài này xài VLA trong C++ là code rác
4RJD3gO.png
nó xài vla rồi đi memset về 0 sao ko xài vector<int> dp(N+1) luôn có phải khỏe hơn ko
 
Time:
  • Visit O(1)
  • Back O(n), best case O(1)
  • Forward O(n), best case O(1)
Space: O(n)

Use Doubly Linked List and store the size of the list, head, and tail, for best case scenario
Python:
class BrowserHistory:
    def __init__(self, homepage: str):
        self.current = Node(homepage, 0)
        self.length = 1
        self.head = self.current
        self.tail = self.current

    def visit(self, url: str) -> None:
        self.current.next = Node(url, self.current.pos + 1)
        self.current.next.prev = self.current
        self.current = self.current.next
        self.length = self.current.pos + 1
        self.tail = self.current

    def back(self, steps: int) -> str:
        if steps >= self.current.pos:
            self.current = self.head
        else:   
            for i in range(steps):
                self.current = self.current.prev
                
        return self.current.url

    def forward(self, steps: int) -> str:
        if steps + self.current.pos >= self.length:
            self.current = self.tail
        else:
            for i in range(steps):
                self.current = self.current.next
                
        return self.current.url
        
class Node:
    def __init__(self, url, pos=0):
        self.url = url
        self.pos = pos
        self.prev = None
        self.next = None
 
Java:
class BrowserHistory {

    private Node head;
    private Node curr;

    public BrowserHistory(String homepage) {
        head = new Node(homepage);
        curr = head;
    }

    public void visit(String url) {
        // clear forward history
        if (curr.next != null) {
            Node next = curr.next;
            next.prev = null;
            curr.next = null;
        }
        Node page = new Node(url);
        curr.next = page;
        page.prev = curr;
        curr = curr.next;
    }

    public String back(int steps) {
        for(int i = 0; i < steps; i++) {
            if (curr.prev != null) {
                curr = curr.prev;
            }
            else {
                break;
            }
        }
        return curr.url;
    }

    public String forward(int steps) {
        for(int i = 0; i < steps; i++) {
            if (curr.next != null) {
                curr = curr.next;
            }
            else {
                break;
            }
        }
        return curr.url;
    }

    static class Node {
        String url;
        Node next;
        Node prev;
        public Node(String url) {
            this.url = url;
            next = null;
            prev = null;
        }
    }
}
 
LL :whistle:
Python:
class Node:
    def __init__(self, url):
        self.url = url
        self.next = None
        self.prev = None


class BrowserHistory:

    def __init__(self, homepage: str):
        self.head = Node("head")
        self.tail = Node("tail")
        self.curr = self.head
        self.visit(homepage)

    def visit(self, url: str) -> None:
        node = Node(url)
        self.curr.next = node
        node.prev = self.curr
        self.curr = node
        self.curr.next = self.tail
        self.tail.prev = self.curr
        temp = self.head

    def back(self, steps: int) -> str:
        while self.curr != self.head and steps > 0:
            self.curr = self.curr.prev
            steps -= 1

        if self.curr == self.head:
            self.curr = self.curr.next
        return self.curr.url

    def forward(self, steps: int) -> str:
        while self.curr != self.tail and steps > 0:
            self.curr = self.curr.next
            steps -= 1

        if self.curr == self.tail:
            self.curr = self.curr.prev
        return self.curr.ur
 
JavaScript:
class BrowserHistory {
    constructor(homepage) {
        this.stack = [homepage];
        this.position = 0;
    }

    visit(url) {
        this.stack = this.stack.slice(0, ++this.position).concat([url]);
    }

    back(steps) {
        return this.stack[this.position -= Math.min(this.position, steps)];
    }

    forward (steps) {
        return this.stack[this.position += Math.min(this.stack.length - this.position - 1, steps)];
    }
}
 
Code:
class BrowserHistory {

    Map<Integer, String> map;
    int cur;
    public BrowserHistory(String homepage) {
        map = new HashMap<>();
        cur = 0;
        map.put(cur, homepage);
    }
    
    public void visit(String url) {
        cur++;
        map.put(cur, url);
        
        for (int i = cur + 1; map.get(i) != null; i++) {
            map.remove(i);
        }
        // System.out.println(map);
    }
    
    public String back(int steps) {
        cur = Math.max(cur - steps, 0);
        // System.out.println(cur);
        return map.get(cur);
    }
    
    public String forward(int steps) {
        for (int i = 1; i <= steps; i++) {
            if (map.get(cur + 1) == null) {
                break;
            }
            cur++;
        }
        
        // cur += steps;
        // System.out.println(map);
        // System.out.println(cur);
        return map.get(cur);
    }
}

/**
 * Your BrowserHistory object will be instantiated and called as such:
 * BrowserHistory obj = new BrowserHistory(homepage);
 * obj.visit(url);
 * String param_2 = obj.back(steps);
 * String param_3 = obj.forward(steps);
 */
 
Các bác đi trước cho e mấy nguồn học DSA với ạ giờ tìm hiểu mới biết được tầm quan trọng của nó, e học năm 2 cũng được học trên trường như C++, DSA rồi, kì nào cũng giải 200 bài trên hệ thống của trường rồi nhưng tự đánh giá là em chỉ giải được đến mức làm quen các thuật toán hay một số bài đặc trưng của thuật toán đấy cùng lắm thôi vì thường có slide hướng dẫn, mã giải, còn các bài khoai khoai thì chịu chết, đặc biệt mấy dạng đệ quy hay quy hoạch động thì ngu hẳn luôn. mấy bài trên leetcode thì may ra giải được vài ba câu easy mà cũng ngồi cả ngày, ngồi đọc solution nhiều khi cũng hiểu cả mấy bài medium. Nhưng để mà ngồi suy nghĩ triển khai từ đầu thì không thể. Giờ e phải chăm chỉ cày bài bản lại từ đầu thôi. Các bác có cách hay nguồn học ở đâu thì chỉ em để em cày với, ví dụ như sách nào, khóa học trên Udemy không và luyện tập như thế nào không . Bằng Java hay Python thì càng tốt ạ. MONG CÁC TIỀN BỐI ĐI TRƯỚC CHỈ ĐƯỜNG =((:cry:
 
Các bác đi trước cho e mấy nguồn học DSA với ạ giờ tìm hiểu mới biết được tầm quan trọng của nó, e học năm 2 cũng được học trên trường như C++, DSA rồi, kì nào cũng giải 200 bài trên hệ thống của trường rồi nhưng tự đánh giá là em chỉ giải được đến mức làm quen các thuật toán hay một số bài đặc trưng của thuật toán đấy cùng lắm thôi vì thường có slide hướng dẫn, mã giải, còn các bài khoai khoai thì chịu chết, đặc biệt mấy dạng đệ quy hay quy hoạch động thì ngu hẳn luôn. mấy bài trên leetcode thì may ra giải được vài ba câu easy mà cũng ngồi cả ngày, ngồi đọc solution nhiều khi cũng hiểu cả mấy bài medium. Nhưng để mà ngồi suy nghĩ triển khai từ đầu thì không thể. Giờ e phải chăm chỉ cày bài bản lại từ đầu thôi. Các bác có cách hay nguồn học ở đâu thì chỉ em để em cày với, ví dụ như sách nào, khóa học trên Udemy không và luyện tập như thế nào không . Bằng Java hay Python thì càng tốt ạ. MONG CÁC TIỀN BỐI ĐI TRƯỚC CHỈ ĐƯỜNG =((:cry:
Đây là kinh nghiệm của mình: Lên Coursera , đăng kí tk premium 300 USD/năm (tháng 1 thì nó hay promotion còn 199 USD) rồi apply mấy khoá Data Structure & Algorithm specialization:
  1. Saint Petersburg State University
  2. University of California San Diego
  3. Princeton University
Học lại căn bản các Data Structure thì mới làm Leetcode hiệu quả.
 
Đây là kinh nghiệm của mình: Lên Coursera , đăng kí tk premium 300 USD/năm (tháng 1 thì nó hay promotion còn 199 USD) rồi apply mấy khoá Data Structure & Algorithm specialization:
  1. Saint Petersburg State University
  2. University of California San Diego
  3. Princeton University
Học lại căn bản các Data Structure thì mới làm Leetcode hiệu quả.
co the xin hoc audit hoac financial aid. Mien phi
 
co the xin hoc audit hoac financial aid. Mien phi
Cá nhân Mình thì ko thích miễn phí. Cái gì miễn phí thì bạn sẽ không trân trọng. Cứ đánh vào tiền thì mới khá được.

Với lại bạn trên sẵn sàng bỏ tiền mua course Udemy thì lấy tiền đấy qua Coursera mua membership là được thôi. Học càng nhiều trong 1 năm thì càng có lợi. Đấy là 1 động lực khác.
 
Các bác đi trước cho e mấy nguồn học DSA với ạ giờ tìm hiểu mới biết được tầm quan trọng của nó, e học năm 2 cũng được học trên trường như C++, DSA rồi, kì nào cũng giải 200 bài trên hệ thống của trường rồi nhưng tự đánh giá là em chỉ giải được đến mức làm quen các thuật toán hay một số bài đặc trưng của thuật toán đấy cùng lắm thôi vì thường có slide hướng dẫn, mã giải, còn các bài khoai khoai thì chịu chết, đặc biệt mấy dạng đệ quy hay quy hoạch động thì ngu hẳn luôn. mấy bài trên leetcode thì may ra giải được vài ba câu easy mà cũng ngồi cả ngày, ngồi đọc solution nhiều khi cũng hiểu cả mấy bài medium. Nhưng để mà ngồi suy nghĩ triển khai từ đầu thì không thể. Giờ e phải chăm chỉ cày bài bản lại từ đầu thôi. Các bác có cách hay nguồn học ở đâu thì chỉ em để em cày với, ví dụ như sách nào, khóa học trên Udemy không và luyện tập như thế nào không . Bằng Java hay Python thì càng tốt ạ. MONG CÁC TIỀN BỐI ĐI TRƯỚC CHỈ ĐƯỜNG =((:cry:
Youtube: 28 tech. Chấm hết
 
cuối tuần đc bài làm tí xong kk
hkNtitg.png


JavaScript:
class BrowserHistory {
    map: Map<number, string>
    cur: number
    curMax: number

    constructor(homepage: string) {
        this.map = new Map();
        this.map.set(1, homepage)
        this.cur = 1;
        this.curMax = 1
    }

    visit(url: string): void {
        this.cur++;
        this.curMax = this.cur;
        this.map.set(this.cur, url)
    }

    back(steps: number): string {
        if (steps > this.cur - 1) {
            steps = this.cur - 1
        }
        this.cur = this.cur - steps;
        return this.map.has(this.cur) ? this.map.get(this.cur) : null
    }

    forward(steps: number): string {
        if (this.cur + steps > this.curMax) {
            this.cur = this.curMax;
        } else {
            this.cur+= steps;
        }
        return this.map.get(this.cur)
    }
}
 
hMR11KC.png
JavaScript backtracking mà chậm vãi tù.
EHVoibS.png
Mà bộ bọn JavaScript không có wrapper class hay đại loại gì như thế để pass reference vào recursive function nhỉ?
untitled-png.1457438
 
JavaScript:
class Node {
  constructor(val, next, prev) {
      this.val = val
      this.next = next
      this.prev = prev
  }
}

/**
* @param {string} homepage
*/
var BrowserHistory = function(homepage) {
  this.current = new Node(homepage, null, null);
};

/**
* @param {string} url
* @return {void}
*/
BrowserHistory.prototype.visit = function(url) {
  const node = new Node(url, null, null);
  node.prev = this.current;
  this.current.next = node;
  this.current = node
};

/**
* @param {number} steps
* @return {string}
*/
BrowserHistory.prototype.back = function(steps) {
  let back = 0;
  while(back !== steps && this.current.prev != null) {
      this.current = this.current.prev
      back++
  }
  return this.current.val
};

/**
* @param {number} steps
* @return {string}
*/
BrowserHistory.prototype.forward = function(steps) {
  let next = 0;
  while(next !== steps && this.current.next != null) {
    this.current = this.current.next
    next++
  }
  return this.current.val
};

Làm xong mới để ý dùng linked list để làm gì cho mệt :shame:
 
Python:
from collections import defaultdict
class TrieNode():
    def __init__(self):
        self.is_word = False
        self.children = defaultdict(list)

class WordDictionary:

    def __init__(self):
        self.root = TrieNode()

    def addWord(self, word: str) -> None:
        cur = self.root
        for c in word:
            if c not in cur.children:
                cur.children[c] = TrieNode()
            cur = cur.children[c]
        cur.is_word = True

    def search(self, word: str) -> bool:
        def helper(node, w):
            cur = node
            for i, c in enumerate(w):
                if c not in cur.children:
                    if c != ".":
                        return False
                    else:
                        if len(cur.children) == 0:
                            return False
                if c in cur.children:
                    cur = cur.children[c]
                else:
                    for child_node in cur.children.values():
                        if helper(child_node, w[i+1:]):
                            return True
                    return False
            return cur.is_word
        return helper(self.root, word)
 
addWord: Time O(n), Space O(n)
search :
- DFS: Time O(n * 26 ** 3), Space O(n)
- BFS: Time O(n * 26 ** 3), Space O(n * 26 ** 3)

Python:
class WordDictionary:
    def __init__(self):
        self.root = Node()

    def addWord(self, word: str) -> None:
       node = self.root
       for c in word:
            if c not in node.children:
                node.children[c] = Node()

            node = node.children[c]

       node.end = True

    def search(self, word: str) -> bool:
        # return self.bfs(self.root, word, 0)
        return self.bfs(self.root, word)
    
    def bfs(self, node, word):
        queue = deque([(node, 0)])
        n = len(word)
        while len(queue) > 0:
            size = len(queue)
            for _ in range(size):
                node, idx = queue.pop()
                
                if idx == n:
                    if node.end:
                        return True
                    else:
                        continue
            
                c = word[idx]
            
                if c != '.':
                    if c in node.children:
                        queue.append((node.children[c], idx + 1))
                else:
                    for key in node.children:
                        queue.append((node.children[key], idx + 1))
                
        return False

    def dfs(self, node, word, idx):
        if idx == len(word):
            return node.end

        c = word[idx]

        if c != '.':
            if c not in node.children:
                return False
            return self.dfs(node.children[c], word, idx+1)

        for key in node.children:
            res = self.dfs(node.children[key], word, idx + 1)
            if res:
                return res

        return False

class Node:
    def __init__(self):
        self.children = {}
        self.end = False
 
Back
Top