From 47586e5280f2527e22312cdd446d252c7257aa2b Mon Sep 17 00:00:00 2001 From: wavty Date: Tue, 5 Sep 2023 00:29:56 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.0279 No.0279.Perfect Squares --- .../0200-0299/0279.Perfect Squares/README.md | 37 +++++++++++++++++++ .../0279.Perfect Squares/README_EN.md | 37 +++++++++++++++++++ .../0279.Perfect Squares/Solution.rs | 16 ++++++++ 3 files changed, 90 insertions(+) create mode 100644 solution/0200-0299/0279.Perfect Squares/Solution.rs diff --git a/solution/0200-0299/0279.Perfect Squares/README.md b/solution/0200-0299/0279.Perfect Squares/README.md index 147bae985c4fa..cc83c6abee105 100644 --- a/solution/0200-0299/0279.Perfect Squares/README.md +++ b/solution/0200-0299/0279.Perfect Squares/README.md @@ -279,6 +279,43 @@ function numSquares(n: number): number { } ``` +### **Rust** + +```rust +impl Solution { + pub fn num_squares(n: i32) -> i32 { + let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize); + let mut dp = vec![vec![i32::MAX; col + 1]; row + 1]; + dp[0][0] = 0; + for i in 1..=row { + for j in 0..=col { + dp[i][j] = dp[i - 1][j]; + if j >= i * i { + dp[i][j] = std::cmp::min(dp[i][j], dp[i][j - i * i] + 1); + } + } + } + dp[row][col] + } +} +``` + +```rust +impl Solution { + pub fn num_squares(n: i32) -> i32 { + let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize); + let mut dp = vec![i32::MAX; col + 1]; + dp[0] = 0; + for i in 1..=row { + for j in i * i..=col { + dp[j] = std::cmp::min(dp[j], dp[j - i * i] + 1); + } + } + dp[col] + } +} +``` + ### **...** ``` diff --git a/solution/0200-0299/0279.Perfect Squares/README_EN.md b/solution/0200-0299/0279.Perfect Squares/README_EN.md index f295a5210828d..23d250959bb36 100644 --- a/solution/0200-0299/0279.Perfect Squares/README_EN.md +++ b/solution/0200-0299/0279.Perfect Squares/README_EN.md @@ -239,6 +239,43 @@ function numSquares(n: number): number { } ``` +### **Rust** + +```rust +impl Solution { + pub fn num_squares(n: i32) -> i32 { + let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize); + let mut dp = vec![vec![i32::MAX; col + 1]; row + 1]; + dp[0][0] = 0; + for i in 1..=row { + for j in 0..=col { + dp[i][j] = dp[i - 1][j]; + if j >= i * i { + dp[i][j] = std::cmp::min(dp[i][j], dp[i][j - i * i] + 1); + } + } + } + dp[row][col] + } +} +``` + +```rust +impl Solution { + pub fn num_squares(n: i32) -> i32 { + let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize); + let mut dp = vec![i32::MAX; col + 1]; + dp[0] = 0; + for i in 1..=row { + for j in i * i..=col { + dp[j] = std::cmp::min(dp[j], dp[j - i * i] + 1); + } + } + dp[col] + } +} +``` + ### **...** ``` diff --git a/solution/0200-0299/0279.Perfect Squares/Solution.rs b/solution/0200-0299/0279.Perfect Squares/Solution.rs new file mode 100644 index 0000000000000..9beb1ee3ab15b --- /dev/null +++ b/solution/0200-0299/0279.Perfect Squares/Solution.rs @@ -0,0 +1,16 @@ +impl Solution { + pub fn num_squares(n: i32) -> i32 { + let (row, col) = ((n as f32).sqrt().floor() as usize, n as usize); + let mut dp = vec![vec![i32::MAX; col + 1]; row + 1]; + dp[0][0] = 0; + for i in 1..=row { + for j in 0..=col { + dp[i][j] = dp[i - 1][j]; + if j >= i * i { + dp[i][j] = std::cmp::min(dp[i][j], dp[i][j - i * i] + 1); + } + } + } + dp[row][col] + } +} \ No newline at end of file