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.
Bài này sao AC thấp thế nhỉ? constrain cũng thấp nữa
Python:
class Solution:
    def areSentencesSimilar(self, sentence1: str, sentence2: str) -> bool:
        if len(sentence1) == len(sentence2):
            return sentence1 == sentence2
        if len(sentence1) > len(sentence2):
            return self.areSentencesSimilar(sentence2, sentence1)
            
        s1 = sentence1.split(" ")
        s2 = sentence2.split(" ")
        left, right = 0, len(s1) - 1
        pLeft, pRight = 0, len(s2) - 1
        while left < len(s1) and s1[left] == s2[pLeft]:
            left += 1
            pLeft += 1
            
        while right >= left and s1[right] == s2[pRight]:
            right -= 1
            pRight-=1
        return right < left
 
@Cố Trường Ca sáng mai có làm contest ko thế. nếu có thì join chung lụm rating nào
w8lLDat.png
Fen có vào k
 
Java:
class Solution {
    public boolean areSentencesSimilar(String sentence1, String sentence2) {
        int l =0;
        int r = 0;
        String[] s1 = sentence1.split(" ");
        String[] s2 = sentence2.split(" ");
        int n = s1.length;
        int m = s2.length;
      
        if(n>=m){
            while(l<m){
                if(s1[l].equals(s2[l]))l++;
                else break;
            }
            while(r<m){
                if(s1[n-1-r].equals(s2[m-1-r]))r++;
                else break;
            }
        }else{
           while(l<n){
                if(s1[l].equals(s2[l]))l++;
                else break;
            }
            while(r<n){
                if(s1[n-1-r].equals(s2[m-1-r]))r++;
                else break;
            }
        }
        
        return n>=m? l+r>=m: l+r>=n;
    }
}
 
C#:
public class Solution
{
    public bool AreSentencesSimilar(string sentence1, string sentence2)
    {
        LinkedList<string> s1 = new(sentence1.Split(' '));
        LinkedList<string> s2 = new(sentence2.Split(' '));

        while (s1.Count > 0 && s2.Count > 0 && s1.First.Value == s2.First.Value)
        {
            s1.RemoveFirst();
            s2.RemoveFirst();
        }
        while (s1.Count > 0 && s2.Count > 0 && s1.Last.Value == s2.Last.Value)
        {
            s1.RemoveLast();
            s2.RemoveLast();
        }

        return s1.Count == 0 || s2.Count == 0;
    }
}
 
Python:
class Solution:
    def areSentencesSimilar(self, sentence1: str, sentence2: str) -> bool:
        s1, s2 = deque(sentence1.split()), deque(sentence2.split())
       
        while s1 and s2:
            if s1[0] != s2[0]:
                break
            s1.popleft()
            s2.popleft()
        while s1 and s2:
            if s1[-1] != s2[-1]:
                break
            s1.pop()
            s2.pop()
       
        result = len(s1) == 0 or len(s2) == 0
       
        return result
 
Swift:
class Solution {
    func areSentencesSimilar(_ sentence1: String, _ sentence2: String) -> Bool {
        let (sen1, sen2) = sentence1.count <= sentence2.count ? (sentence1, sentence2) : (sentence2, sentence1)
        var s1 = sen1.components(separatedBy:" ")
        let s2 = sen2.components(separatedBy:" ")
        //
        var l1 = 0
        var r1 = s1.count-1
        var l2 = 0
        var r2 = s2.count-1
        while s1[l1] == s2[l2] {
            l1 += 1
            l2 += 1
            if l1 == s1.count { break }
        }
        while s1[r1] == s2[r2] {
            r1 -= 1
            r2 -= 1
            if r1 < 0 { break }
        }
        return l1 > r1
    }
}
 
C-like:
impl Solution {
    pub fn are_sentences_similar(sentence1: String, sentence2: String) -> bool {
        let (s1, s2) =
            if (sentence1.len() < sentence2.len()) {
                (sentence1, sentence2)
            } else {
                (sentence2, sentence1)
            };

        let wcount1 = s1.split(' ').count();
        let (mut forward_count, mut backward_count) = (0, 0);
        for (w1, w2) in s1.split(' ').zip(s2.split(' ')) {
            if w1 != w2 {
                break;
            }

            forward_count += 1;
        }

        for (w1, w2) in s1.split(' ').rev().zip(s2.split(' ').rev()) {
            if w1 != w2 {
                break;
            }

            backward_count += 1;
        }

        forward_count + backward_count >= wcount1
    }
}
 
Sửa lần cuối:
JavaScript:
function areSentencesSimilar(sentence1: string, sentence2: string): boolean {
    const words1 = sentence1.split(' ');
    const words2 = sentence2.split(' ');
    let commomPrefixLength = 0;
    for (let i = 0; i < words2.length; i++) {
        if (words1[i] !== words2[i]) {
            break;
        }
        commomPrefixLength += 1
    }
    let commomSuffixLength = 0;
    for (let i1 = words1.length - 1, i2 = words2.length - 1; i2 >= 0 && i1 >= 0; i1--, i2--) {
        if (words1[i1] !== words2[i2]) {
            break;
        }
        commomSuffixLength += 1
    }

    const totalCommonLength = commomSuffixLength + commomPrefixLength;
    return totalCommonLength >= words1.length || totalCommonLength >= words2.length

};
 
Java:
class Solution {
    public boolean areSentencesSimilar(String sentence1, String sentence2) {
        String[] arr1 = sentence1.split(" ");
        String[] arr2 = sentence2.split(" ");
        int len1 = arr1.length;
        int len2 = arr2.length;
        int i=0,j=0;
        while(i<len1 && i<len2 && arr1[i].equals(arr2[i]))
            i++;
        while(j<(len1-i) && j<(len2-i) && arr1[len1-j-1].equals(arr2[len2-j-1]))
            j++;
        
        return i+j == len1 || i+j==len2;
    }
}
 
Nghe đâu trong này có cao nhân nào làm toán nhiều hơn mình làm toán cả đời, nên mình lên đây hỏi bài tập probability theory. Mấy cái này đứa sinh viên năm 3 năm 4 nào học xong Real Analysis I rồi đọc định nghĩa trong lectures là làm ez, ez, đưa cho người có thực lực thì chỉ chọt lét cái tôi của họ thôi, nhưng mình vẫn hỏi, để cho biết đá biết vàng.

Chủ yếu vì hôm nào mình choảng nhau với vị hiền tài nào trên cái diễn đàn này, thằng chả luyên thuyên cái gì đó về tensor gì đó mình cũng chưa ngấm lắm, nhưng hỏi vặn thì mới lòi ra là không biết chứng minh quy nạp hình dong nó ntn.

Trong file PDF này là solution của mình cho problem set 2 của khóa 6.436J / 15.085J Fundamentals of Probability, nhờ cao nhân giải giúp mình bài 2 với nếu có thời gian thì chấm luôn solution của mấy bài còn lại. Đa tạ, đa tạ.
Bài 2 sử dung trực tiếp bổ đề Borel-Cantelli. Cụ thể như sau:
equation1.png

Tức là:
equation2.png

Vậy thì:
equation3.png

Theo tính chất décroissance séquentielle (tiếng Việt dịch là gì?) của độ đo xác suất thì:
equation4.png

tương tự:
equation5.png

Chuyển bất đẳng thức phía trên qua giới hạn:
equation6.png

Mặt khác từ giả thiết:
equation7.png

nên theo bổ đề Borel-Cantelli:
equation8.png

và cũng theo giả thiết:
equation9.png

Do vậy thu được:
equation10.png

Đây chính là điều cần chứng minh.
 
JavaScript:
var areSentencesSimilar = function(sentence1, sentence2) {
    const arr1 = sentence1.split(' '), arr2 = sentence2.split(' ');
    for (const [u, v] of [[arr1, arr2], [arr1, arr2]]) {
        while (u.length && v.length && u[u.length-1] === v[v.length-1]) {
            u.pop();
            v.pop();
        }
        if (!u.length || !v.length) {
            return true;
        }
        u.reverse(); v.reverse();
    }
    return false;
};
 
Bài 2 sử dung trực tiếp bổ đề Borel-Cantelli. Cụ thể như sau: Xem tệp đính kèm 2718814
Tức là:
Xem tệp đính kèm 2718818
Vậy thì:
Xem tệp đính kèm 2718823
Theo tính chất décroissance séquentielle (tiếng Việt dịch là gì?) của độ đo xác suất thì:
Xem tệp đính kèm 2718840
tương tự:
Xem tệp đính kèm 2718855
Chuyển bất đẳng thức phía trên qua giới hạn:
Xem tệp đính kèm 2718872
Mặt khác từ giả thiết:
Xem tệp đính kèm 2718896
nên theo bổ đề Borel-Cantelli:
Xem tệp đính kèm 2718900
và cũng theo giả thiết:
Xem tệp đính kèm 2718901
Do vậy thu được:
Xem tệp đính kèm 2718911
Đây chính là điều cần chứng minh.
Má, bác làm em muốn quay lại học toán quá, hồi trước học toán toàn rơi vào trạng thái lúc học thì biết làm nhưng học xong thì chữ thầy trả thầy hết.
 
Java:
class Solution {
    public boolean areSentencesSimilar(String sentence1, String sentence2) {
        Deque<String> deque1 = new ArrayDeque<>();
        Deque<String> deque2 = new ArrayDeque<>();
        String[] s1 = sentence1.split("\\s");
        String[] s2 = sentence2.split("\\s");
        Arrays.stream(s1).forEach(s -> deque1.offer(s));
        Arrays.stream(s2).forEach(s -> deque2.offer(s));
        while (!deque1.isEmpty() && !deque2.isEmpty()) {
            String pre1 = deque1.peekFirst();
            String pre2 = deque2.peekFirst();
            String sub1 = deque1.peekLast();
            String sub2 = deque2.peekLast();
            if (pre1.equals(pre2)) {
                deque1.pollFirst();
                deque2.pollFirst();
                continue;
            }
            if (sub1.equals(sub2)) {
                deque1.pollLast();
                deque2.pollLast();
                continue;
            }
            return false;

        }
        return true;
    }
}
Hơi cồng kềnh tí
xjIzSG9.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.214.596
Quay lại
Lên đầu trang