From 003eb24c777365cc67b231d214e3424e994844b0 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Fri, 20 Jun 2025 11:27:53 +0800 Subject: [PATCH] update --- leetcode_py/3200.py | 28 +++++ leetcode_rs/Cargo.toml | 2 +- leetcode_rs/problems/p110/Cargo.toml | 6 + leetcode_rs/problems/p110/src/main.rs | 153 ++++++++++++++++++++++++++ leetcode_rs/problems/p383/Cargo.toml | 6 + leetcode_rs/problems/p383/src/main.rs | 44 ++++++++ 6 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 leetcode_py/3200.py create mode 100644 leetcode_rs/problems/p110/Cargo.toml create mode 100644 leetcode_rs/problems/p110/src/main.rs create mode 100644 leetcode_rs/problems/p383/Cargo.toml create mode 100644 leetcode_rs/problems/p383/src/main.rs diff --git a/leetcode_py/3200.py b/leetcode_py/3200.py new file mode 100644 index 0000000..4915831 --- /dev/null +++ b/leetcode_py/3200.py @@ -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 diff --git a/leetcode_rs/Cargo.toml b/leetcode_rs/Cargo.toml index 4af9e9f..e9ce782 100644 --- a/leetcode_rs/Cargo.toml +++ b/leetcode_rs/Cargo.toml @@ -4,6 +4,6 @@ version = "0.1.0" edition = "2021" [workspace] -members = ["problems/*"] +members = [ "problems/*"] [dependencies] diff --git a/leetcode_rs/problems/p110/Cargo.toml b/leetcode_rs/problems/p110/Cargo.toml new file mode 100644 index 0000000..c88a91a --- /dev/null +++ b/leetcode_rs/problems/p110/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "p110" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/leetcode_rs/problems/p110/src/main.rs b/leetcode_rs/problems/p110/src/main.rs new file mode 100644 index 0000000..11bc11a --- /dev/null +++ b/leetcode_rs/problems/p110/src/main.rs @@ -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>>, + pub right: Option>>, +} + +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>>) -> bool { + // 返回 (是否平衡, 高度) + Self::dfs(&root).0 + } + + // 返回元组:(是否平衡, 高度) + fn dfs(node: &Option>>) -> (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>> { + Some(Rc::new(RefCell::new(TreeNode::new(val)))) +} + +// 辅助函数:构建树 +fn build_tree(values: &[Option], index: usize) -> Option>> { + 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 +} diff --git a/leetcode_rs/problems/p383/Cargo.toml b/leetcode_rs/problems/p383/Cargo.toml new file mode 100644 index 0000000..01b891f --- /dev/null +++ b/leetcode_rs/problems/p383/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "p383" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/leetcode_rs/problems/p383/src/main.rs b/leetcode_rs/problems/p383/src/main.rs new file mode 100644 index 0000000..2b89dc5 --- /dev/null +++ b/leetcode_rs/problems/p383/src/main.rs @@ -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 + ); + } +}