Bài đăng

#30daychallenge (14) (15) | Perform String Shift & Product of Array Except Self

Hôm qua bận không thể đăng, dù biết không có ai xem nhưng vẫn cảm thấy có lỗi với bản thân. Dạo này tôi thấy hiệu suất không được cao lắm. (Social network nhiều:))) Perform String Shifts You are given a string  s  containing lowercase English letters, and a matrix  shift , where  shift[i] = [direction, amount] : direction  can be  0  (for left shift) or  1  (for right shift).  amount  is the amount by which string  s  is to be shifted. A left shift by 1 means remove the first character of  s  and append it to the end. Similarly, a right shift by 1 means remove the last character of  s  and add it to the beginning. Return the final string after all operations. Example 1: Input: s = "abc", shift = [[0,1],[1,2]] Output: "cab" Explanation:   [0,1] means shift to left by 1. "abc" -> "bca" [1,2] means shift to right by 2. "bca" -> "cab" Example 2: Input: s = "abcdefg", shift...

-1 chia 3 dư mấy?

Modulo, hay được hiểu là số dư của phép chia. Năm 1801, Gauss xuất bản một cuốn sách bao gồm modulo arithmetics. Sau đó, một định nghĩa được chấp nhận rộng rãi đưa ra bởi thiên tài  Donald Knuth  người viết những quyển sách thách thức cả thế này. mod(a, n) = a - n * floor(a / n) Hạn chế của nó ? Trong lập trình, modulo thường được sử dụng để giới hạn một index trong một cấu trúc dữ liệu có giới hạn. (Hiểu đơn giản là tránh một chỉ mục nằm ngoài khoảng không gian cho phép). values = [ 3, 4, 5 ] index = 100 value_at_index = values[ index % values.length ] . Như ví dụ trên, để tránh các lỗi không mong muốn, dù index là bao nhiêu nó cũng sẽ trả về giá trị thuộc mảng [3,4,5]. Nhưng tình huống thực sự hay gặp phải là gì ? Điều gì sẽ xảy ra nếu bạn giá trị a của bạn âm ? Hóa ra lại phụ thuộc vào ngôn ngữ mà bạn đang lập trình, nhìn vào bảng dưới đây ta có thể thấy ngôn ngữ lập trình về vấn đề này chia làm hai phe khác nhau: Language 13 mod 3 -13 mod 3 13 mod -3 -1...

#30daychallenge (13) | Contiguous Array

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Example 1: Input: [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1. Example 2: Input: [0,1,0] Output: 2 Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. Note:  The length of the given binary array will not exceed 50,000. Bài này trông thế thôi chứ ý tưởng đơn giản lắm, giả sử cái mảng [0,1,0,0,1,1,0] đi. Nếu count = 0. Chạy trên mảng, nếu arr[i] = 1 thì count+=1, arr[i]=-1 thì count-=1. Ta sẽ có: [-1,0,-1,-2,-1,0,-1]. Thấy thế nào, kết quả là i=1->6. Ta có hashMap<count,i>. Ban đầu ta sẽ put vào (0,-1) cho trường hợp đầu tiên. 0  [-1,0,-1,-2,-1,0,-1], ta sẽ tìm vị trí bắt đầu khi nó có cùng giá trị, đoạn đấy sẽ luôn luôn bằng nhau. Xong rồi nhé, nay up bài hơi muộn vì hình như tui càng ngày càng lười :(( Hết rồi, hẹn gặp Leetcode vào ngày mai. Cảm ...

ZigZag Conversion

Bài toán này nếu không có Leetcode hiện case mỗi lần commit sai thì không biết tôi có AC nổi không. Vậy sau này đi phỏng vấn thì thế nào :(( The string  "PAYPALISHIRING"  is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R nd then read line by line:  "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string s, int numRows); Example 1: Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR" Example 2: Input: s = "PAYPALISHIRING", numRows = 4 Output:  "PINALSIGYAHRPI" Explanation: P I N A L S I G Y A H R P I Giả sử trong ví dụ 2, tôi chuyển những đường ZigZag chéo sang cột, thì nhìn sẽ như thế này:                                   ...

#30daychallenge (12) | Last Stone Weight

We have a collection of stones, each stone has a positive integer weight. Each turn, we choose the two  heaviest  stones and smash them together.  Suppose the stones have weights  x  and  y  with  x <= y .  The result of this smash is: If  x == y , both stones are totally destroyed; If  x != y , the stone of weight  x  is totally destroyed, and the stone of weight  y  has new weight  y-x . At the end, there is at most 1 stone left.  Return the weight of this stone (or 0 if there are no stones left.) Input: [2,7,4,1,8,1] Output: 1 Explanation: We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then, we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then, we combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone. Bài này dùng tôi Priority Queue, lấy ra 2...

Longest Palindromic Substring

Hồi cấp 3 tôi dùng quy hoạch động để giải bài này, bây giờ với tôi tạo một mảng hai chiều N*N sẽ là một giải pháp sau cùng. Xâu đối xứng có một tính chất quan trọng, nó có thể quy về hai dạng là: abba và aba. Tôi sẽ dùng một phương pháp mở rộng xâu từ giữa. Ta cài đặt một hàm mở rộng expand(start,end). Như vậy nó sẽ có hai loại mở rộng là expand(i,i) (aba) và expand(i,i+1) abba. Đến đây thì bài toán đã được giải quyết để tìm được độ dài xâu đối xứng dài nhất. Giờ để đưa ra được substring, ta có hai biến start=0 và end = 0. Ta sẽ cặp nhật lại hai biến này khi len > start - end . start = i - (len-1)/2 . (len-1 để không bỏ xót trường hợp i =0). end = i+len/2. Đây là code trên Java: Cảm ơn bạn !

#30daychallege (11) | Diameter of Binary Tree

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the  longest  path between any two nodes in a tree. This path may or may not pass through the root. Example: Given a binary tree 1 / \ 2 3 / \ 4 5 Return  3 , which is the length of the path [4,2,1,3] or [5,2,1,3]. Note:  The length of path between two nodes is represented by the number of edges between them. Đây là một bài toán khó với tôi, dù tôi đã từng làm và từng đọc 1 lần rồi. Khi quan sát, ta thấy tất cả các path đều có chung 1 đặc điểm là nó bắt đầu từ một nút nào đó, và chỉ đi xuống các nút con. Nếu ta biết mỗi node có chiều dài là L và R. Thì diameter của cây nhị phân sẽ là: L+R+1. Đường dài nhất là đường đi từ L sang Node gốc và đi qua R. Thế độ xâu của một Node nào đó được tính thế nào ? Gọi maxDepth(Node) là hàm tính độ sâu, thì : maxDepth(node) = max(maxDe...