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 countConsistentStrings(self, allowed: str, words: List[str]) -> int:
        hashset = set()

        for i in allowed:
            hashset.add(i)

        res = len(words)
        for w in words:
            for l in w:
                if l not in hashset:
                    res -= 1
                    break
        
        return res

giờ mới biết set(allowed) cũng được hehe ai biết đâu
 
Java:
class Solution {
    public int countConsistentStrings(String allowed, String[] words) {
        int set =0;
        int cnt = words.length;
        for(char c:allowed.toCharArray()){
            set = set|(1<<(c-'a'));//set (c-'a')th bit to 1
        }
        for(String word:words){
            for(char c: word.toCharArray()){
                if((set&(1<<(c-'a')))==0)//check (c-'a')th bit {
                    cnt--;
                    break;
                }
            }
        }
        return cnt;
    }
}
bí thuật gì đây:eek:
 
bí thuật gì đây:eek:
01 sinh 10, 10 sinh 11, 1 0 sinh vạn vật, mọi vật trong máy tính đều từ 1 0 mà ra
1726113379215-png.2679114
 
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:
Python:
class Solution:
    def minimumLength(self, s: str) -> int:
        hashmap = {}
        res = len(s)

        for i in s:
            count_i = hashmap.get(i)
            if not count_i:
                hashmap[i] = 1
            
            if count_i == 2:
                res -= 2
                hashmap[i] = 1
                continue
            
            if count_i is not None:
                hashmap[i] += 1
        
        return res
 
C-like:
impl Solution {
    pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
        let bitset =
            allowed.as_bytes().into_iter().
                fold(0, |acc, &bc| acc | 1 << (bc - b'a') as u32);

        let mut inconsistent_count = 0;

        for word in &words {
            let bytes = word.as_bytes();
            let mut word_bitset = bitset;

            for &bc in bytes {
                word_bitset |= 1 << (bc - b'a') as u32;

                if bitset != word_bitset {
                    inconsistent_count += 1;
                    break;
                }
            }
        }

        (words.len() - inconsistent_count) as i32
    }
}

C-like:
impl Solution {
    pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
        let bitset =
            allowed.as_bytes().into_iter().
                fold(0, |acc, &bc| acc | 1 << (bc - b'a') as u32);

        let mut result =
            words.iter().
                filter(|&word| {
                    let (word_bytes, mut word_bitset) = (word.as_bytes(), bitset);

                    for &bc in word_bytes {
                        word_bitset |= 1 << (bc - b'a') as u32;

                        if word_bitset != bitset {
                            return false
                        }
                    }

                    true
                }).
                count();

        result as i32
    }
}
 
Sửa lần cuối:
Cơm thêm có tính nền tảng khác ngoài leetcode không các bác. Em có một số bài hay nhưng lại rải rác ở một số nền tảng như codeforces, vnoi, vjudge, katis, ...
 
C++:
class Solution {
public:
    int numberOfWeakCharacters(vector<vector<int>>& properties) {
        sort(properties.begin(), properties.end(), [] (auto &a, auto &b) {
            return a[0] == b[0] ? a[1] > b[1] : a[0] < b[0]; // sort by increase attack, decrease defense
        });
        int ret = 0, max_defense = properties.back().back();
        for (int i = properties.size() - 2; i >= 0; --i) {
            if (max_defense > properties[i][1]) ret += 1;
            else max_defense = properties[i][1];
        }
        return ret;
    }
};
 
C++:
class Solution {
public:
    int numberOfWeakCharacters(vector<vector<int>>& properties) {
        sort(properties.begin(), properties.end(), [] (auto &a, auto &b) {
            return a[0] == b[0] ? a[1] > b[1] : a[0] < b[0]; // sort by increase attack, decrease defense
        });
        int ret = 0, max_defense = properties.back().back();
        for (int i = properties.size() - 2; i >= 0; --i) {
            if (max_defense > properties[i][1]) ret += 1;
            else max_defense = properties[i][1];
        }
        return ret;
    }
};
không chơi monotonic stack à
 
Rating khởi đầu là 1k5 mà fence
Làm thêm ít cơm thêm đi các fence :ah:
Python:
class Solution:
    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
        dx = min(ax2, bx2) - max(ax1, bx1)
        dy = min(ay2, by2) - max(ay1, by1)
        sd = dx*dy
       
        if dx < 0 or dy < 0:
            sd = 0

        s1 = abs(ax2 - ax1)*abs(ay2 - ay1)
        s2 = abs(bx2 - bx1)*abs(by2 - by1)

        return s1 + s2 - sd

ngón tay lên cơ luôn

# updated
 
Sửa lần cuối:
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
Có cách tìm overlapsed area dễ hơn là if else tay to thế này đó fence

via theNEXTvoz for iPhone
 
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.235
Quay lại
Lên đầu trang