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.
thấy ma trận mà còn đi viết dfs, mấy em nằm xuống bảnh cho 3 gậy sau nhớ
tHQi21r.png
Qcg0oqw.jpg
Có mấy bài bạch trạch trên matrix trư viết kiểu dfs vẫn ăn như thường mà
 
Java:
class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        Map<Integer, Integer> map = new HashMap<>();

        for (int[] trip: trips) {
            map.put(trip[1], map.getOrDefault(trip[1], 0) + trip[0]);
            map.put(trip[2], map.getOrDefault(trip[2], 0) - trip[0]);
        }

        int passengers = 0;

        for (int i = 0; i <= 1000; i++) {
            if (map.containsKey(i)) {
                passengers += map.get(i);
            }

            if (passengers > capacity) return false;
        }

        return true;
    }
}
Java:
class Solution {
    public int[] fullBloomFlowers(int[][] flowers, int[] people) {
        int[] ppl = Arrays.copyOf(people, people.length);
        Arrays.sort(ppl);
        Arrays.sort(flowers, (a, b) -> Arrays.compare(a, b));
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        Map<Integer, Integer> map = new HashMap<>();

        int idx = 0;

        for (int p: ppl) {
            while (idx < flowers.length && flowers[idx][0] <= p) {
                heap.offer(flowers[idx][1]);
                idx++;
            }

            while (!heap.isEmpty() && heap.peek() < p) {
                heap.poll();
            }

            map.put(p, heap.size());
        }

        int[] ans = new int[people.length];

        for (int i = 0; i < people.length; i++) {
            ans[i] = map.get(people[i]);
        }

        return ans;
    }
}
 
Java:
class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        Map<Integer, Integer> map = new HashMap<>();

        for (int[] trip: trips) {
            map.put(trip[1], map.getOrDefault(trip[1], 0) + trip[0]);
            map.put(trip[2], map.getOrDefault(trip[2], 0) - trip[0]);
        }

        int passengers = 0;

        for (int i = 0; i <= 1000; i++) {
            if (map.containsKey(i)) {
                passengers += map.get(i);
            }

            if (passengers > capacity) return false;
        }

        return true;
    }
}
Java:
class Solution {
    public int[] fullBloomFlowers(int[][] flowers, int[] people) {
        int[] ppl = Arrays.copyOf(people, people.length);
        Arrays.sort(ppl);
        Arrays.sort(flowers, (a, b) -> Arrays.compare(a, b));
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        Map<Integer, Integer> map = new HashMap<>();

        int idx = 0;

        for (int p: ppl) {
            while (idx < flowers.length && flowers[idx][0] <= p) {
                heap.offer(flowers[idx][1]);
                idx++;
            }

            while (!heap.isEmpty() && heap.peek() < p) {
                heap.poll();
            }

            map.put(p, heap.size());
        }

        int[] ans = new int[people.length];

        for (int i = 0; i < people.length; i++) {
            ans[i] = map.get(people[i]);
        }

        return ans;
    }
}
Bài 2 dùng sweep line đi
osCpCsi.gif


via theNEXTvoz for iPhone
 
Python:
class Solution:
    def findTheLongestSubstring(self, s: str) -> int:
        mask = 0
        appearance = {0 : -1}
        vowels = {'a': 1, 'e': 2, 'i': 3, 'o': 4, 'u': 5}
        ans = 0
        for i, char in enumerate(s):
            if char in vowels:
                mask ^= 1 << vowels[char]

            if not mask in appearance:
                appearance[mask] = i
            else:
                ans = max(ans, i - appearance[mask])

        return ans
 
Học được xor trick từ editorial.
C-like:
impl Solution {
    pub fn find_the_longest_substring(s: String) -> i32 {
        let encoding = &mut vec![0; 26];
        let encoding = {
            for (i, c) in [b'a', b'e', b'i', b'o', b'u'].iter().enumerate() {
                encoding[(c - b'a') as usize] = 1 << i;
            }
            encoding.as_slice()
        };
        let mut earliest_pos = vec![-1; 32];
        let (_, longest_len) = s.as_bytes().iter().enumerate().fold((0, 0), |(prefix_xor, longest_len), (i, &c)| {
            let i = i as i32;
            let prefix_xor = prefix_xor ^ encoding[(c - b'a') as usize];
            let longest_len = match prefix_xor {
                0 => i + 1,
                _ =>
                    if earliest_pos[prefix_xor] == -1 {
                        earliest_pos[prefix_xor] = i;
                        longest_len
                    } else {
                        core::cmp::max(longest_len, i  - earliest_pos[prefix_xor])
                    }
            };
            (prefix_xor, longest_len)
        });
        longest_len
    }
}
 
Sửa lần cuối:
JavaScript:
var findTheLongestSubstring = function (s) {
    const m = { 0: -1 }, n = s.length, k = 'aeoui';
    let t = 0, ans = 0;
    for (let i = 0; i < n; i++) {
        const ch = s[i];
    
        if (k.indexOf(ch) >= 0) {
            t = t ^ (1 << k.indexOf(ch));
        }
    
        if (t in m) {
            ans = Math.max(ans, i - m[t]);
        } else {
            m[t] = i;
        }
    }
    return ans;
};
 
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.215.669
Quay lại
Lên đầu trang