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.
Python:
class Solution:
    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
        dx = 0
        if bx1 >= ax1:
            if bx1 <= ax2 and bx2 >= ax2:
                dx = ax2 - bx1
            if bx1 <= ax2 and bx2 <= ax2:
                dx = bx2 - bx1
        else:
            if ax1 <= bx2 and ax2 >= bx2:
                dx = bx2 - ax1
            if ax1 <= bx2 and ax2 <= bx2:
                dx = ax2 - ax1
        dy = 0
        if by1 >= ay1:
            if by1 <= ay2 and by2 >= ay2:
                dy = ay2 - by1
            if by1 <= ay2 and by2 <= ay2:
                dy = by2 - by1
        else:
            if ay1 <= by2 and ay2 >= by2:
                dy = by2 - ay1
            if ay1 <= by2 and ay2 <= by2:
                dy = ay2 - ay1
       
        s1 = abs(ax2 - ax1)*abs(ay2 - ay1)
        s2 = abs(bx2 - bx1)*abs(by2 - by1)
        sd = dx*dy

        return s1 + s2 - sd

ngón tay lên cơ luôn
Ăn thua gì đâu, ngón tay của @freedom.9 sau khi giải spiral nè
1726119080459.png
 
Cơm thêm
Xin ít cơm thêm Medium ae :ah: làm hard riết mất não chán nản quá :ah:
Java:
class Solution {
    public int minimumLength(String s) {
        int[] freq = new int[26];
        for (char c : s.toCharArray()) {
            freq[c - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if (freq[i] != 0) {
                if (freq[i] <= 2) continue;
                while (freq[i] > 2) {
                    freq[i] -= 2;
                }
            }
        }
        return Arrays.stream(freq).sum();
    }
}
Java:
class Solution {
    public int minimumLength(String s) {
        int[] freq = new int[26];
        int ans = 0;
        for (char c : s.toCharArray()) {
            freq[c - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if (freq[i] <= 2) ans += freq[i];
            else ans += (freq[i] % 2 == 1 ? 1 : 2);
        }
        return ans;
    }
}
Tranh thủ đá bát cơm trưa :sleep::sleep::sleep:
 
Sửa lần cuối:
Cơm thêm
Xin ít cơm thêm Medium ae :ah: làm hard riết mất não chán nản quá :ah:
Java:
class Solution {
    public int minimumLength(String s) {
        int[] freq = new int[26];
        for(char c:s.toCharArray()){
            freq[c-'a']++;
        }
        int res =0;
        for(int i= 0;i<26;i++ ){
            if(freq[i]!=0){
                res+=freq[i]%2==1?1:2;
            }
        }
        return res;
    }
}

Java:
class Solution {
    public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        int x1= ax2-ax1;
        int y1 = ay2-ay1;

        int x2= bx2-bx1;
        int y2= by2-by1;

        int maxX = Math.max(ax2,bx2);
        int minX = Math.min(ax1, bx1);

        int maxY = Math.max(ay2, by2);
        int minY = Math.min(ay1, by1);
        int res =  x1*y1 + x2*y2;
        if((x1+x2 - (maxX-minX)) > 0 && (y1+y2 - (maxY -minY))>0) {
            res -= (x1+x2 - (maxX-minX)) * (y1+y2 - (maxY -minY));
        }
        return res;
    }
}
 
Sửa lần cuối:
ngồi nhà với làm test tâm lý khác nhau mà, chủ yếu tò mò thôi bác. làm daily với lúc làm contest thôi là thấy ngợp r
Xong rồi mấy thím, 2 câu:
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ -> như câu này mà trả về cả substring

Câu 2 Thì java thread đọc nhiều files, có câu mở rộng

Run quá nên câu 1 impl lủng, lúc đầu còn sai đề in ra length.
 
Python:
class Solution:
    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
        wordSet = set(allowed)
        count = 0
        for w in words:
            count+=1
            for c in w:
                if c not in wordSet:
                    count-=1
                    break
        return count
 
C-like:
impl Solution {
    pub fn length_of_longest_substring(s: String) -> i32 {
        let (mut bitset, mut left, bytes, mut result) = (0, 0, s.as_bytes(), 0);

        for (i, &bc) in bytes.iter().enumerate() {
            let mask = 1u128 << bc;

            if mask & bitset == 0 {
                bitset |= mask;
                result = result.max(i - left + 1);

                continue;
            }

            while (bytes[left] != bc) {
                let left_mask = 1u128 << bytes[left];
                bitset ^= left_mask;
                left += 1;
            }

            left += 1;
        }

        result as i32
    }
}
 
Học mót bit trick bằng cách xem lời giải.

C-like:
impl Solution {
    pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
        let allowed = allowed.as_bytes().iter().fold(0, |bitmask, b| {
            bitmask | (1 << b - b'a')
        });
        words.into_iter().fold(0, |count, word|{
            count + word.as_bytes().iter().all(|b| {
                (allowed >> (b - b'a')) & 1 != 0
            }) as i32
        })
    }
}
 
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.998
Quay lại
Lên đầu trang