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.
nãy mik thấy pn @LmaoSuVuong nói gì chưa kịp đọc đã xóa rùi
JCFtpJo.png
mzyidY1.png
cố ca bớt giả gái giùm, muốn fake ít ra phải lấy ac chính đăng 1 code, ac phụ đăng 1 code khác hoàn toàn may ra chư tin
 
theo rule mới này thì intersections vẫn được maintain trong tree nên intersections interval có thể xuất hiện ở bất cứ đâu nơi mà start của nó bé hơn start đang xét ấy, nên code của bn chưa đúng ấy, ý kiến của Nhi là vậy.
Nếu chỉ xét 1 2 điểm thôi hk ăn thua đâu. Phải quét hết tree, mà làm vậy thì thui merge interval cho lẹ pn ak
yêu Nhi nhiều nắm :beauty:
 
theo rule mới này thì intersections vẫn được maintain trong tree nên intersections interval có thể xuất hiện ở bất cứ đâu nơi mà start của nó bé hơn start đang xét ấy, nên code của bn chưa đúng ấy, ý kiến của Nhi là vậy.
Nếu chỉ xét 1 2 điểm thôi hk ăn thua đâu. Phải quét hết tree, mà làm vậy thì thui merge interval cho lẹ pn ak

của bạn dài bao nhiêu?
 
C-like:
use std::collections::BTreeMap;

struct MyCalendarTwo {
    entry_exit: BTreeMap<i32, i32>
}

impl MyCalendarTwo {

    fn new() -> Self {
        Self {
            entry_exit: BTreeMap::new()
        }
    }

    fn book(&mut self, start: i32, end: i32) -> bool {
        self.entry_exit.entry(start).
            and_modify(|freq_delta| *freq_delta += 1).or_insert(1);

        self.entry_exit.entry(end).
            and_modify(|freq_delta| *freq_delta -= 1).or_insert(-1);

        let (mut max_count, mut count) = (0, 0);

        for (&point, &delta) in self.entry_exit.iter() {
            count += delta;
            max_count = max_count.max(count);

            if max_count > 2 {
                self.entry_exit.entry(start).
                    and_modify(|freq_delta| *freq_delta -= 1);

                self.entry_exit.entry(end).
                    and_modify(|freq_delta| *freq_delta += 1);

                return false;
            }
        }

        true
    }
}

C-like:
struct MyCalendarTwo {
    once: Vec<(i32, i32)>,
    twice: Vec<(i32, i32)>
}

impl MyCalendarTwo {
    fn new() -> Self {
        Self {
            once: vec![],
            twice: vec![]
        }
    }

    fn book(&mut self, start: i32, end: i32) -> bool {
        for (other_start, other_end) in self.twice.iter().copied() {
            if other_start.max(start) < other_end.min(end) {
                return false;
            }
        }

        // find intersections of overlapping intervals
        for (other_start, other_end) in self.once.iter().copied() {
            let new_start = other_start.max(start);
            let new_end = other_end.min(end);

            if new_start < new_end {
                self.twice.push((new_start, new_end));
            }
        }

        self.once.push((start, end));

        true
    }
}
 
Sửa lần cuối:
bên java ko có kiểu dữ liệu nào mà vừa ordered mà vừa có index
uwooUzw.gif
vẽ ra viễn cảnh rất đẹp nhưng ko tìm dc cái nào để xài

Bạn có thể dùng 1 ordered list rồi dùng thêm 1 dict mà, thông minh lên :sad:

via theNEXTvoz for iPhone

code trong runtime sample, sửa lại một chút cho dễ nhìn:

C-like:
use std::collections::BTreeMap;

use std::iter::{FromIterator, once};

struct MyCalendarThree {
    data: BTreeMap<i32, i32>,
    max: i32
}

impl MyCalendarThree {
    fn new() -> Self {
        MyCalendarThree {
            // BTreeMap with one key-value pair (0, 0) at the start
            data: BTreeMap::from_iter(once((0, 0))),
            max: 0i32,
        }
    }

    fn book(&mut self, start: i32, end: i32) -> i32 {
        // find the value of the closest key that is smaller than `start`
        // sentinel key-value pair (0, 0) ensures that this call will always be successful
        let (_, &(mut current)) =
            self.data.range(..=start).next_back().unwrap();

        // insert if not present
        self.data.entry(start).or_insert(current);

        // iterate over all ranges with their starts falling within the half-open interval `[start, end)`
        for (_, count) in self.data.range_mut(start..end) {
            current = *count;
            *count += 1;

            self.max = self.max.max(current + 1);
        }

        // insert if not present
        self.data.entry(end).or_insert(current);

        self.max
    }
}
 
C-like:
struct MyCalendarTwo {
    time: std::collections::BTreeMap<i32, i32>,
}

impl MyCalendarTwo {
    fn new() -> Self {
        Self { time: Default::default() }   
    }
    
    fn book(&mut self, start: i32, end: i32) -> bool {
        self.time.entry(start).and_modify(|t| *t += 1).or_insert(1);
        self.time.entry(end).and_modify(|t| *t -= 1).or_insert(-1);

        let mut count = 0;
        for &t in self.time.values() {
            count += t;
            if count >= 3 {
                self.time.entry(start).and_modify(|t| *t -= 1);
                self.time.entry(end).and_modify(|t| *t += 1);
                return false;
            }
        }

        true
    }
}
 
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.683
Quay lại
Lên đầu trang