From c599da7d0c28cf625257c0f2e0ed445f39390b40 Mon Sep 17 00:00:00 2001 From: Thane-Patrol Date: Thu, 23 Feb 2023 09:59:15 +1100 Subject: [PATCH 1/2] Added 0473-matchsticks-to-square.rs --- rust/0473-matchsticks-to-square.rs | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 rust/0473-matchsticks-to-square.rs diff --git a/rust/0473-matchsticks-to-square.rs b/rust/0473-matchsticks-to-square.rs new file mode 100644 index 000000000..daad51a11 --- /dev/null +++ b/rust/0473-matchsticks-to-square.rs @@ -0,0 +1,36 @@ +impl Solution { + pub fn makesquare(matchsticks: Vec) -> bool { + fn backtrack( + sides: &mut [i64; 4], + matches: &Vec, + idx: usize, + length: i64 + ) -> bool { + if idx >= matches.len() { + return true + } + + for i in 0..4 { + if sides[i] + matches[idx] as i64 <= length { + sides[i] += matches[idx] as i64; + if backtrack(sides, matches, idx + 1, length) { + return true + } + sides[i] -= matches[idx] as i64 + } + } + false + } + + let mut total = 0; + for mat in matchsticks.iter() { + total += *mat as i64; + } + let mut matchsticks = matchsticks; + matchsticks.sort_by_key(|e| std::cmp::Reverse(*w)); + + let side = total / 4; + + backtrack(&mut [0, 0, 0, 0], &matchsticks, 0, side) + } +} \ No newline at end of file From 94c41c95f301556b3968f86cc5754584c6afca1a Mon Sep 17 00:00:00 2001 From: Thane-Patrol Date: Thu, 23 Feb 2023 12:41:01 +1100 Subject: [PATCH 2/2] Added 0179-largest-number.rs --- rust/0179-largest-number.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 rust/0179-largest-number.rs diff --git a/rust/0179-largest-number.rs b/rust/0179-largest-number.rs new file mode 100644 index 000000000..d8aad0367 --- /dev/null +++ b/rust/0179-largest-number.rs @@ -0,0 +1,20 @@ +use std::cmp::Ordering; + +impl Solution { + pub fn largest_number(nums: Vec) -> String { + let mut ans = String::new(); + let mut nums = nums; + let mut strs = nums.into_iter().map(|e| e.to_string()).collect::>(); + strs.sort_by(|a, b| { + let a_first = a.clone() + &*b; + let b_first = b.clone() + &*a; + b_first.cmp(&a_first) + }); + for num in strs { + if ans == "0" && num == "0" {continue} + ans.push_str(&*num.to_string()) + } + + ans + } +} \ No newline at end of file