From cc36203e4378c7ae217a35549f33539a46bc4449 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 13 Mar 2024 10:08:32 +0800 Subject: [PATCH 1/3] 20240313 Finished --- src/problem/mod.rs | 3 +- .../p2864_maximum_odd_binary_number.rs | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2864_maximum_odd_binary_number.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 60dd272..5eb218c 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -71,4 +71,5 @@ mod p2834_find_the_minimum_possible_sum_of_a_beautiful_array; mod p2386_find_the_k_sum_of_an_array; mod p299_bulls_and_cows; mod p2129_capitalize_the_title; -mod p1261_find_elements_in_a_contaminated_binary_tree; \ No newline at end of file +mod p1261_find_elements_in_a_contaminated_binary_tree; +mod p2864_maximum_odd_binary_number; \ No newline at end of file diff --git a/src/problem/p2864_maximum_odd_binary_number.rs b/src/problem/p2864_maximum_odd_binary_number.rs new file mode 100644 index 0000000..249e953 --- /dev/null +++ b/src/problem/p2864_maximum_odd_binary_number.rs @@ -0,0 +1,52 @@ +/** + * [2864] Maximum Odd Binary Number + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn maximum_odd_binary_number(s: String) -> String { + let nums: Vec = s.chars() + .map(|c| c.to_digit(10).unwrap()) + .collect(); + + let mut one_count = 0; + let mut zero_count = 0; + + for num in &nums { + if *num == 1 { + one_count += 1; + } else if *num == 0 { + zero_count += 1; + } + } + + let mut result: Vec = Vec::with_capacity(nums.len()); + + for _ in 1..one_count { + result.push('1'); + } + + for _ in 0..zero_count { + result.push('0'); + } + + result.push('1'); + result.iter().collect() + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2864() { + assert_eq!("001", Solution::maximum_odd_binary_number("010".to_owned())); + assert_eq!("1001", Solution::maximum_odd_binary_number("0101".to_owned())); + } +} From 53b66058fc041d667cca0d32ad64dea8345bf2f1 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 14 Mar 2024 10:01:52 +0800 Subject: [PATCH 2/3] 20240314 Finished --- src/problem/mod.rs | 3 +- ...ment_in_an_array_after_merge_operations.rs | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2789_largest_element_in_an_array_after_merge_operations.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 5eb218c..b1b910a 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -72,4 +72,5 @@ mod p2386_find_the_k_sum_of_an_array; mod p299_bulls_and_cows; mod p2129_capitalize_the_title; mod p1261_find_elements_in_a_contaminated_binary_tree; -mod p2864_maximum_odd_binary_number; \ No newline at end of file +mod p2864_maximum_odd_binary_number; +mod p2789_largest_element_in_an_array_after_merge_operations; \ No newline at end of file diff --git a/src/problem/p2789_largest_element_in_an_array_after_merge_operations.rs b/src/problem/p2789_largest_element_in_an_array_after_merge_operations.rs new file mode 100644 index 0000000..59ee64c --- /dev/null +++ b/src/problem/p2789_largest_element_in_an_array_after_merge_operations.rs @@ -0,0 +1,39 @@ +/** + * [2789] Largest Element in an Array after Merge Operations + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn max_array_value(nums: Vec) -> i64 { + let mut nums: Vec = nums.iter() + .map(|x| *x as i64) + .collect(); + + let mut result = nums[0]; + for i in (0..(nums.len() - 1)).rev() { + if nums[i] <= nums[i + 1] { + nums[i] = nums[i] + nums[i + 1]; + + result = result.max(nums[i]); + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2789() { + assert_eq!(21, Solution::max_array_value(vec![2,3,7,9,3])); + assert_eq!(11, Solution::max_array_value(vec![5,3,3])); + } +} From d4a8fcb0ca007ab6e971bf31d15c8b7f8c493f38 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 15 Mar 2024 08:42:59 +0800 Subject: [PATCH 3/3] 20240315 Finished --- src/problem/mod.rs | 3 +- src/problem/p2312_selling_pieces_of_wood.rs | 67 +++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2312_selling_pieces_of_wood.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index b1b910a..65a4c97 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -73,4 +73,5 @@ mod p299_bulls_and_cows; mod p2129_capitalize_the_title; mod p1261_find_elements_in_a_contaminated_binary_tree; mod p2864_maximum_odd_binary_number; -mod p2789_largest_element_in_an_array_after_merge_operations; \ No newline at end of file +mod p2789_largest_element_in_an_array_after_merge_operations; +mod p2312_selling_pieces_of_wood; \ No newline at end of file diff --git a/src/problem/p2312_selling_pieces_of_wood.rs b/src/problem/p2312_selling_pieces_of_wood.rs new file mode 100644 index 0000000..8c9c2ce --- /dev/null +++ b/src/problem/p2312_selling_pieces_of_wood.rs @@ -0,0 +1,67 @@ +/** + * [2312] Selling Pieces of Wood + */ +pub struct Solution {} + + +// submission codes start here +use std::collections::HashMap; + +impl Solution { + pub fn selling_wood(m: i32, n: i32, prices: Vec>) -> i64 { + let mut prices_map = HashMap::with_capacity(prices.len()); + + for price in prices { + prices_map.insert(Solution::hash(price[0] as usize, price[1] as usize), + price[2] as i64); + } + + let (m, n) = (m as usize, n as usize); + + let mut dp = vec![vec![-1;n + 1];m + 1]; + + + Solution::dfs(m, n, &mut dp, &prices_map) + } + + fn hash(x: usize, y: usize)-> usize { + return x * 1000 + y; + } + + fn dfs(x: usize, y: usize, dp: &mut Vec>, prices_map: &HashMap) -> i64 { + if dp[x][y] != -1 { + return dp[x][y]; + } + + let mut result = *prices_map.get(&Solution::hash(x, y)) + .unwrap_or_else(|| &0); + + if x > 1 { + for i in 1..x { + result = result.max(Solution::dfs(i, y, dp, prices_map) + + Solution::dfs(x - i, y, dp, prices_map)); + } + } + + if y > 1 { + for j in 1..y { + result = result.max(Solution::dfs(x, j, dp, prices_map) + + Solution::dfs(x, y - j, dp, prices_map)); + } + } + + dp[x][y] = result; + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2312() { + } +}