Skip to content

Commit

Permalink
feat: add maximal_square
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed Dec 27, 2021
1 parent 5eaa836 commit 1cb0f6f
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 111 deletions.
14 changes: 0 additions & 14 deletions .test_repo/src/1025.lib.rs

This file was deleted.

14 changes: 0 additions & 14 deletions .test_repo/src/120.lib.rs

This file was deleted.

12 changes: 0 additions & 12 deletions .test_repo/src/1646.lib.rs

This file was deleted.

24 changes: 24 additions & 0 deletions .test_repo/src/221.lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* @lc app=leetcode.cn id=221 lang=rust
*
* [221] 最大正方形
*/

// @lc code=start
impl Solution {
pub fn maximal_square(matrix: Vec<Vec<char>>) -> i32 {
let mut res = 0;
let mut val = 0;
let row = matrix.len();
let column = matrix[0].len();
for i in 0..row {
for j in 0..column {
let current_val = matrix[i][j];
let right = j + 1;
let down = i + 1;
while right < column && down < row {}
}
}
}
}
// @lc code=end
14 changes: 0 additions & 14 deletions .test_repo/src/413.lib.rs

This file was deleted.

14 changes: 0 additions & 14 deletions .test_repo/src/509.lib.rs

This file was deleted.

14 changes: 14 additions & 0 deletions .test_repo/src/825.lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* @lc app=leetcode.cn id=825 lang=rust
*
* [825] 适龄的朋友
*/

// @lc code=start
impl Solution {
pub fn num_friend_requests(ages: Vec<i32>) -> i32 {

}
}
// @lc code=end

14 changes: 0 additions & 14 deletions .test_repo/src/97.lib.rs

This file was deleted.

98 changes: 69 additions & 29 deletions .test_repo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,89 @@
// 一维数组
/*
* @lc app=leetcode.cn id=413 lang=rust
* @lc app=leetcode.cn id=221 lang=rust
*
* [413] 等差数列划分
* [221] 最大正方形
*/
struct Solution {}
// @lc code=start
impl Solution {
pub fn number_of_arithmetic_slices(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut dp = vec![false; n];
pub fn maximal_square(matrix: Vec<Vec<char>>) -> i32 {
let row = matrix.len();
let column = matrix[0].len();
let mut dp = vec![vec![0; column]; row];
let mut res = 0;
dp[0] = true;
for i in 0..n {
for j in (i + 1)..n {
if j - i <= 1 {
dp[j] = true;
continue;
}
dp[j] = if dp[j - 1] && nums[j] - nums[j - 1] == nums[j - 1] - nums[j - 2] {
true
} else {
false
};
if dp[j] && j >= 2 {
res = res + 1;
for i in 0..row {
for j in 0..column {
if matrix[i][j] == '1' {
if i == 0 || j == 0 {
dp[i][j] = 1;
} else {
dp[i][j] = dp[i - 1][j].min(dp[i][j - 1].min(dp[i - 1][j - 1])) + 1
}
}
res = res.max(dp[i][j])
}
}
res
res * res
}
}
// @lc code=end
// pub fn maximal_square(matrix: Vec<Vec<char>>) -> i32 {
// let mut res = 0;
// let row = matrix.len();
// let column = matrix[0].len();
// for i in 0..row {
// for j in 0..column {
// if matrix[i][j] != '1' {
// continue;
// }
// let mut right = j + 1;
// let mut down = i + 1;

// while right < column && down < row {
// if Solution::check(&matrix, (i, j, right, down)) {
// res = res.max((down - i + 1) * (right - j + 1));
// right = right + 1;
// down = down + 1;
// } else {
// break;
// }
// }
// res = res.max(1)
// }
// }
// res as i32
// }
// fn check(matrix: &Vec<Vec<char>>, coords: (usize, usize, usize, usize)) -> bool {
// let (top, left, right, down) = coords;
// if matrix[down][right] != '1' {
// return false;
// }
// for i in left..right {
// if matrix[down][i] != '1' {
// return false;
// }
// }
// for i in top..down {
// if matrix[i][right] != '1' {
// return false;
// }
// }
// true
// }
}
// @lc code=end
#[cfg(test)]

mod tests {
use super::*;

#[test]
fn tests() {
fn name() {
println!(
"{:?}",
Solution::number_of_arithmetic_slices(vec![1, 2, 3, 4])
);
println!("{:?}", Solution::number_of_arithmetic_slices(vec![1]))
"{}",
Solution::maximal_square(vec![
vec!['1', '1', '0'],
vec!['1', '1', '1'],
vec!['1', '1', '1'],
vec!['1', '1', '0']
])
)
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ Rust 构造树需要使用 [Rc<T>引用计数智能指针](https://kaisery.githu
[55跳跃游戏](./dynamic-programing/medium/can_jump/src/lib.rs)
[45跳跃游戏||](./dynamic-programing/medium/jump/src/lib.rs)
[413等差数列划分](./dynamic-programing/medium/number_of_arithmetic_slices/src/lib.rs)
[221最大正方形](./dynamic-programing/medium/maximal_square/src/lib.rs)

### HOT100🔥

Expand Down
5 changes: 5 additions & 0 deletions dynamic-programing/medium/maximal_square/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions dynamic-programing/medium/maximal_square/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "leetcodebyrust"
version = "0.1.0"
authors = ["zhangyuang <yuuang>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
49 changes: 49 additions & 0 deletions dynamic-programing/medium/maximal_square/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 一维数组
/*
* @lc app=leetcode.cn id=413 lang=rust
*
* [413] 等差数列划分
*/
struct Solution {}
// @lc code=start
impl Solution {
pub fn number_of_arithmetic_slices(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut dp = vec![false; n];
let mut res = 0;
dp[0] = true;
for i in 0..n {
for j in (i + 1)..n {
if j - i <= 1 {
dp[j] = true;
continue;
}
dp[j] = if dp[j - 1] && nums[j] - nums[j - 1] == nums[j - 1] - nums[j - 2] {
true
} else {
false
};
if dp[j] && j >= 2 {
res = res + 1;
}
}
}
res
}
}
// @lc code=end

// @lc code=end
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn tests() {
println!(
"{:?}",
Solution::number_of_arithmetic_slices(vec![1, 2, 3, 4])
);
println!("{:?}", Solution::number_of_arithmetic_slices(vec![1]))
}
}

0 comments on commit 1cb0f6f

Please sign in to comment.