836. 矩形重叠

This commit is contained in:
2026-09-14 15:54:08 +08:00
parent e48dc20484
commit ec2d2c9be2
+13 -75
View File
@@ -1,84 +1,22 @@
use crate::arr::make_matrix;
use std::cmp::Reverse;
use std::collections::BTreeMap;
mod arr;
mod list;
mod tree;
struct MinStack {
heap: BTreeMap<Reverse<i32>, i32>,
stack: Vec<i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MinStack {
fn new() -> Self {
MinStack {
heap: BTreeMap::new(),
stack: Vec::new(),
struct Solution {}
impl Solution {
pub fn is_rectangle_overlap(rec1: Vec<i32>, rec2: Vec<i32>) -> bool {
// [1] -- [3], [1] -- [3]
let a = !(rec1[2] <= rec2[0] || rec2[2] <= rec1[0]);
let b = !(rec1[1] >= rec2[3] || rec2[1] >= rec1[3]);
if a && b {
return true;
}
}
fn push(&mut self, value: i32) {
self.stack.push(value);
let entry = self.heap.entry(Reverse(value)).or_insert(0);
*entry += 1;
}
fn pop(&mut self) {
let ele = self.stack.pop();
let entry = self.heap.get_mut(&Reverse(ele.unwrap())).unwrap();
if *entry == 1 {
self.heap.remove_entry(&Reverse(ele.unwrap()));
} else {
*entry -= 1;
}
}
fn top(&self) -> i32 {
self.stack.last().cloned().unwrap()
}
fn get_min(&self) -> i32 {
self.heap.last_key_value().unwrap().0.0
false
}
}
/**
* Your MinStack object will be instantiated and called as such:
* let obj = MinStack::new();
* obj.push(value);
* obj.pop();
* let ret_3: i32 = obj.top();
* let ret_4: i32 = obj.get_min();
*/
fn main() {
let opers = [
"MinStack", "push", "push", "push", "getMin", "pop", "getMin", "pop", "getMin", "pop",
"push", "push", "push", "getMin", "pop", "top", "getMin", "pop", "getMin", "pop",
];
let data =
make_matrix("[[],[0],[1],[0],[],[],[],[],[],[],[-2],[-1],[-2],[],[],[],[],[],[],[]]");
let mut st = MinStack::new();
for i in 1..opers.len() {
match opers[i] {
"push" => {
st.push(data[i][0]);
}
"pop" => {
st.pop();
}
"top" => {
println!("{:?}", st.top());
}
"getMin" => {
println!("{:?}", st.get_min());
}
_ => {}
}
}
println!(
"{:?}",
Solution::is_rectangle_overlap(vec![0, 0, 2, 2], vec![1, 1, 3, 3])
);
}