-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximal_rectangle.rs
82 lines (76 loc) · 2.1 KB
/
maximal_rectangle.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
///
/// Problem: Maximal Rectangle
///
/// Given a `rows x cols` binary matrix filled with `0s` and `1s`, find the area of the **largest rectangle** containing only `1s`.
///
/// **Example 1:**
/// ```plaintext
/// Input:
/// matrix =
/// [
/// ['1','0','1','0','0'],
/// ['1','0','1','1','1'],
/// ['1','1','1','1','1'],
/// ['1','0','0','1','0']
/// ]
/// Output: 6
/// Explanation: The largest rectangle is formed using `[1,1,1]` in row 2 and `[1,1,1]` in row 3 (area = `3*2=6`).
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input:
/// matrix = [
/// ['0']
/// ]
/// Output: 0
/// ```
///
/// **Constraints:**
/// - `1 <= rows, cols <= 200`
/// - `matrix[i][j]` is either `'0'` or `'1'`.
///
/// # Solution:
/// - **Time Complexity:** `O(rows * cols)`
/// - **Space Complexity:** `O(cols)`
impl Solution {
pub fn maximal_rectangle(matrix: Vec<Vec<char>>) -> i32 {
if matrix.is_empty() || matrix[0].is_empty() {
return 0;
}
let rows = matrix.len();
let cols = matrix[0].len();
let mut heights = vec![0; cols];
let mut max_area = 0;
for row in 0..rows {
for col in 0..cols {
if matrix[row][col] == '1' {
heights[col] += 1;
} else {
heights[col] = 0;
}
}
max_area = max_area.max(Self::largest_histogram(&heights));
}
max_area
}
fn largest_histogram(heights: &Vec<i32>) -> i32 {
let mut stack: Vec<usize> = Vec::new();
let mut max_area = 0;
let mut heights = heights.clone();
heights.push(0);
for i in 0..heights.len() {
while let Some(&top) = stack.last() {
if heights[i] >= heights[top] {
break;
}
stack.pop();
let height = heights[top];
let width = if stack.is_empty() { i } else { i - stack.last().unwrap() - 1 };
max_area = max_area.max(height * width as i32);
}
stack.push(i);
}
max_area
}
}