Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rust solution to lc problem: No.0085 #1528

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions solution/0000-0099/0085.Maximal Rectangle/README.md
Original file line number Diff line number Diff line change
@@ -194,6 +194,86 @@ public:
};
```

### **Rust**

```rust
impl Solution {
#[allow(dead_code)]
pub fn maximal_rectangle(matrix: Vec<Vec<char>>) -> i32 {
let n = matrix[0].len();
let mut heights = vec![0; n];
let mut ret = -1;

for row in &matrix {
Self::array_builder(row, &mut heights);
ret = std::cmp::max(ret, Self::largest_rectangle_area(heights.clone()));
}

ret
}

/// Helper function, build the heights array according to the input
#[allow(dead_code)]
fn array_builder(input: &Vec<char>, heights: &mut Vec<i32>) {
for (i, &c) in input.iter().enumerate() {
heights[i] += match c {
'1' => 1,
'0' => {
heights[i] = 0;
0
}
_ => panic!("This is impossible"),
};
}
}

/// Helper function, see: https://leetcode.com/problems/largest-rectangle-in-histogram/ for details
#[allow(dead_code)]
fn largest_rectangle_area(heights: Vec<i32>) -> i32 {
let n = heights.len();
let mut left = vec![-1; n];
let mut right = vec![-1; n];
let mut stack: Vec<(usize, i32)> = Vec::new();
let mut ret = -1;

// Build left vector
for (i, h) in heights.iter().enumerate() {
while !stack.is_empty() && stack.last().unwrap().1 >= *h {
stack.pop();
}
if stack.is_empty() {
left[i] = -1;
} else {
left[i] = stack.last().unwrap().0 as i32;
}
stack.push((i, *h));
}

stack.clear();

// Build right vector
for (i, h) in heights.iter().enumerate().rev() {
while !stack.is_empty() && stack.last().unwrap().1 >= *h {
stack.pop();
}
if stack.is_empty() {
right[i] = n as i32;
} else {
right[i] = stack.last().unwrap().0 as i32;
}
stack.push((i, *h));
}

// Calculate the max area
for (i, h) in heights.iter().enumerate() {
ret = std::cmp::max(ret, (right[i] - left[i] - 1) * *h);
}

ret
}
}
```

### **Go**

```go
80 changes: 80 additions & 0 deletions solution/0000-0099/0085.Maximal Rectangle/README_EN.md
Original file line number Diff line number Diff line change
@@ -164,6 +164,86 @@ public:
};
```

### **Rust**

```rust
impl Solution {
#[allow(dead_code)]
pub fn maximal_rectangle(matrix: Vec<Vec<char>>) -> i32 {
let n = matrix[0].len();
let mut heights = vec![0; n];
let mut ret = -1;

for row in &matrix {
Self::array_builder(row, &mut heights);
ret = std::cmp::max(ret, Self::largest_rectangle_area(heights.clone()));
}

ret
}

/// Helper function, build the heights array according to the input
#[allow(dead_code)]
fn array_builder(input: &Vec<char>, heights: &mut Vec<i32>) {
for (i, &c) in input.iter().enumerate() {
heights[i] += match c {
'1' => 1,
'0' => {
heights[i] = 0;
0
}
_ => panic!("This is impossible"),
};
}
}

/// Helper function, see: https://leetcode.com/problems/largest-rectangle-in-histogram/ for details
#[allow(dead_code)]
fn largest_rectangle_area(heights: Vec<i32>) -> i32 {
let n = heights.len();
let mut left = vec![-1; n];
let mut right = vec![-1; n];
let mut stack: Vec<(usize, i32)> = Vec::new();
let mut ret = -1;

// Build left vector
for (i, h) in heights.iter().enumerate() {
while !stack.is_empty() && stack.last().unwrap().1 >= *h {
stack.pop();
}
if stack.is_empty() {
left[i] = -1;
} else {
left[i] = stack.last().unwrap().0 as i32;
}
stack.push((i, *h));
}

stack.clear();

// Build right vector
for (i, h) in heights.iter().enumerate().rev() {
while !stack.is_empty() && stack.last().unwrap().1 >= *h {
stack.pop();
}
if stack.is_empty() {
right[i] = n as i32;
} else {
right[i] = stack.last().unwrap().0 as i32;
}
stack.push((i, *h));
}

// Calculate the max area
for (i, h) in heights.iter().enumerate() {
ret = std::cmp::max(ret, (right[i] - left[i] - 1) * *h);
}

ret
}
}
```

### **Go**

```go
75 changes: 75 additions & 0 deletions solution/0000-0099/0085.Maximal Rectangle/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
impl Solution {
#[allow(dead_code)]
pub fn maximal_rectangle(matrix: Vec<Vec<char>>) -> i32 {
let n = matrix[0].len();
let mut heights = vec![0; n];
let mut ret = -1;

for row in &matrix {
Self::array_builder(row, &mut heights);
ret = std::cmp::max(ret, Self::largest_rectangle_area(heights.clone()));
}

ret
}

/// Helper function, build the heights array according to the input
#[allow(dead_code)]
fn array_builder(input: &Vec<char>, heights: &mut Vec<i32>) {
for (i, &c) in input.iter().enumerate() {
heights[i] += match c {
'1' => 1,
'0' => {
heights[i] = 0;
0
}
_ => panic!("This is impossible"),
};
}
}

/// Helper function, see: https://leetcode.com/problems/largest-rectangle-in-histogram/ for details
#[allow(dead_code)]
fn largest_rectangle_area(heights: Vec<i32>) -> i32 {
let n = heights.len();
let mut left = vec![-1; n];
let mut right = vec![-1; n];
let mut stack: Vec<(usize, i32)> = Vec::new();
let mut ret = -1;

// Build left vector
for (i, h) in heights.iter().enumerate() {
while !stack.is_empty() && stack.last().unwrap().1 >= *h {
stack.pop();
}
if stack.is_empty() {
left[i] = -1;
} else {
left[i] = stack.last().unwrap().0 as i32;
}
stack.push((i, *h));
}

stack.clear();

// Build right vector
for (i, h) in heights.iter().enumerate().rev() {
while !stack.is_empty() && stack.last().unwrap().1 >= *h {
stack.pop();
}
if stack.is_empty() {
right[i] = n as i32;
} else {
right[i] = stack.last().unwrap().0 as i32;
}
stack.push((i, *h));
}

// Calculate the max area
for (i, h) in heights.iter().enumerate() {
ret = std::cmp::max(ret, (right[i] - left[i] - 1) * *h);
}

ret
}
}