update
This commit is contained in:
parent
3b805a6422
commit
003eb24c77
28
leetcode_py/3200.py
Normal file
28
leetcode_py/3200.py
Normal file
@ -0,0 +1,28 @@
|
||||
class Solution:
|
||||
def maxHeightOfTriangle(self, red: int, blue: int) -> int:
|
||||
# Let's try starting with both colors and take the maximum result
|
||||
return max(self.tryHeight(red, blue), self.tryHeight(blue, red))
|
||||
|
||||
def tryHeight(self, color1: int, color2: int) -> int:
|
||||
height = 0
|
||||
row = 1
|
||||
|
||||
# Keep alternating colors
|
||||
while True:
|
||||
# Try to use color1 for odd rows
|
||||
if row % 2 == 1:
|
||||
if color1 >= row:
|
||||
color1 -= row
|
||||
height += 1
|
||||
else:
|
||||
break
|
||||
# Try to use color2 for even rows
|
||||
else:
|
||||
if color2 >= row:
|
||||
color2 -= row
|
||||
height += 1
|
||||
else:
|
||||
break
|
||||
row += 1
|
||||
|
||||
return height
|
@ -4,6 +4,6 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[workspace]
|
||||
members = ["problems/*"]
|
||||
members = [ "problems/*"]
|
||||
|
||||
[dependencies]
|
||||
|
6
leetcode_rs/problems/p110/Cargo.toml
Normal file
6
leetcode_rs/problems/p110/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "p110"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
153
leetcode_rs/problems/p110/src/main.rs
Normal file
153
leetcode_rs/problems/p110/src/main.rs
Normal file
@ -0,0 +1,153 @@
|
||||
use std::cell::RefCell;
|
||||
use std::cmp::max;
|
||||
use std::rc::Rc;
|
||||
|
||||
// Definition for a binary tree node.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct TreeNode {
|
||||
pub val: i32,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
#[inline]
|
||||
pub fn new(val: i32) -> Self {
|
||||
TreeNode {
|
||||
val,
|
||||
left: None,
|
||||
right: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
|
||||
// 返回 (是否平衡, 高度)
|
||||
Self::dfs(&root).0
|
||||
}
|
||||
|
||||
// 返回元组:(是否平衡, 高度)
|
||||
fn dfs(node: &Option<Rc<RefCell<TreeNode>>>) -> (bool, i32) {
|
||||
match node {
|
||||
None => (true, 0), // 空节点是平衡的,高度为0
|
||||
Some(node_rc) => {
|
||||
let node = node_rc.borrow();
|
||||
|
||||
// 递归检查左子树
|
||||
let (left_balanced, left_height) = Self::dfs(&node.left);
|
||||
if !left_balanced {
|
||||
return (false, 0); // 左子树不平衡,整棵树不平衡
|
||||
}
|
||||
|
||||
// 递归检查右子树
|
||||
let (right_balanced, right_height) = Self::dfs(&node.right);
|
||||
if !right_balanced {
|
||||
return (false, 0); // 右子树不平衡,整棵树不平衡
|
||||
}
|
||||
|
||||
// 检查当前节点是否平衡(左右子树高度差不超过1)
|
||||
let balanced = (left_height - right_height).abs() <= 1;
|
||||
|
||||
// 当前节点的高度 = max(左子树高度, 右子树高度) + 1
|
||||
let height = max(left_height, right_height) + 1;
|
||||
|
||||
(balanced, height)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 辅助函数:创建树节点
|
||||
fn create_node(val: i32) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
Some(Rc::new(RefCell::new(TreeNode::new(val))))
|
||||
}
|
||||
|
||||
// 辅助函数:构建树
|
||||
fn build_tree(values: &[Option<i32>], index: usize) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
if index >= values.len() || values[index].is_none() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let value = values[index].unwrap();
|
||||
let node = create_node(value);
|
||||
|
||||
// 计算左右子节点的索引
|
||||
let left_index = 2 * index + 1;
|
||||
let right_index = 2 * index + 2;
|
||||
|
||||
// 设置左子节点
|
||||
if left_index < values.len() && values[left_index].is_some() {
|
||||
let left_node = build_tree(values, left_index);
|
||||
node.as_ref().unwrap().borrow_mut().left = left_node;
|
||||
}
|
||||
|
||||
// 设置右子节点
|
||||
if right_index < values.len() && values[right_index].is_some() {
|
||||
let right_node = build_tree(values, right_index);
|
||||
node.as_ref().unwrap().borrow_mut().right = right_node;
|
||||
}
|
||||
|
||||
node
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// 测试用例1:平衡树 [3,9,20,null,null,15,7]
|
||||
// 3
|
||||
// / \
|
||||
// 9 20
|
||||
// / \
|
||||
// 15 7
|
||||
let values1 = vec![Some(3), Some(9), Some(20), None, None, Some(15), Some(7)];
|
||||
let tree1 = build_tree(&values1, 0);
|
||||
println!("测试用例1是否平衡: {}", Solution::is_balanced(tree1)); // 应该输出:true
|
||||
|
||||
// 测试用例2:不平衡树 [1,2,2,3,3,null,null,4,4]
|
||||
// 1
|
||||
// / \
|
||||
// 2 2
|
||||
// / \
|
||||
// 3 3
|
||||
// / \
|
||||
// 4 4
|
||||
let values2 = vec![
|
||||
Some(1),
|
||||
Some(2),
|
||||
Some(2),
|
||||
Some(3),
|
||||
Some(3),
|
||||
None,
|
||||
None,
|
||||
Some(4),
|
||||
Some(4),
|
||||
];
|
||||
let tree2 = build_tree(&values2, 0);
|
||||
println!("测试用例2是否平衡: {}", Solution::is_balanced(tree2)); // 应该输出:false
|
||||
|
||||
// 测试用例3:空树
|
||||
let tree3 = None;
|
||||
println!("测试用例3是否平衡: {}", Solution::is_balanced(tree3)); // 应该输出:true
|
||||
|
||||
// 测试用例4:单节点树
|
||||
let tree4 = create_node(1);
|
||||
println!("测试用例4是否平衡: {}", Solution::is_balanced(tree4)); // 应该输出:true
|
||||
|
||||
// 测试用例5:高度差为1的树,但仍然平衡
|
||||
// 1
|
||||
// / \
|
||||
// 2 3
|
||||
// /
|
||||
// 4
|
||||
let root = create_node(1);
|
||||
let node2 = create_node(2);
|
||||
let node3 = create_node(3);
|
||||
let node4 = create_node(4);
|
||||
|
||||
node2.as_ref().unwrap().borrow_mut().left = node4;
|
||||
root.as_ref().unwrap().borrow_mut().left = node2;
|
||||
root.as_ref().unwrap().borrow_mut().right = node3;
|
||||
|
||||
println!("测试用例5是否平衡: {}", Solution::is_balanced(root)); // 应该输出:true
|
||||
}
|
6
leetcode_rs/problems/p383/Cargo.toml
Normal file
6
leetcode_rs/problems/p383/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "p383"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
44
leetcode_rs/problems/p383/src/main.rs
Normal file
44
leetcode_rs/problems/p383/src/main.rs
Normal file
@ -0,0 +1,44 @@
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn can_construct(ransom_note: String, magazine: String) -> bool {
|
||||
// 提前判断
|
||||
if ransom_note.len() > magazine.len() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut char_counts = [0; 128];
|
||||
|
||||
// 统计magazine中每个字符的出现次数
|
||||
for &b in magazine.as_bytes() {
|
||||
char_counts[b as usize] += 1;
|
||||
}
|
||||
|
||||
// 减去ransom_note中的字符
|
||||
for &b in ransom_note.as_bytes() {
|
||||
char_counts[b as usize] -= 1;
|
||||
if char_counts[b as usize] < 0 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let test_cases = [
|
||||
("a", "b", false),
|
||||
("aa", "ab", false),
|
||||
("aa", "aab", true),
|
||||
("", "", true),
|
||||
];
|
||||
|
||||
for (note, mag, expected) in test_cases {
|
||||
let result = Solution::can_construct(note.to_string(), mag.to_string());
|
||||
println!(
|
||||
"Can \"{}\" be constructed from \"{}\"? {} (Expected: {})",
|
||||
note, mag, result, expected
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user