LmaoSuVuong
Senior Member
nãy mik thấy pn @LmaoSuVuong nói gì chưa kịp đọc đã xóa rùi![]()
nãy mik thấy pn @LmaoSuVuong nói gì chưa kịp đọc đã xóa rùi![]()
public ac leetcode xemgiả đâu mà giả, gái thiệt mà
Xem tệp đính kèm 2704543
mk mới hc thuipublic ac leetcode xem![]()
yêu Nhi nhiều nắmtheo 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

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
Ko phải mình đâu, hôm nay còn chuâ làm daily nữa
không cô đơn như negav, anh gặp ngay my babyboo @ThaoNhi99không cô đơn như negav, anh gặp ngay my babyboo @ThaoNhi99
phải chăng chúng ta đang chứng kiến một tình yêu vượt lên định kiến?không cô đơn như negav, anh gặp ngay my babyboo @ThaoNhi99
leak acc ra thì đổi ava khác chứ lộ thông tin sao pn

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
}
}
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
}
}

bên java ko có kiểu dữ liệu nào mà vừa ordered mà vừa có indexbài Calendar III có cách dùng BTreeMap + prefix sum mà vẫn chưa ngấm lắm![]()
Bạn có thể dùng 1 ordered list rồi dùng thêm 1 dict mà, thông minh lênbên java ko có kiểu dữ liệu nào mà vừa ordered mà vừa có indexvẽ ra viễn cảnh rất đẹp nhưng ko tìm dc cái nào để xài![]()

bên java ko có kiểu dữ liệu nào mà vừa ordered mà vừa có indexvẽ ra viễn cảnh rất đẹp nhưng ko tìm dc cái nào để xài![]()
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
}
}
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
}
}