1727. 重新排列后的最大子矩阵
This commit is contained in:
parent
143ff15f87
commit
6c8fa06ce1
61
src/main.rs
61
src/main.rs
|
|
@ -1,58 +1,35 @@
|
|||
use std::cmp::max;
|
||||
|
||||
struct Solution;
|
||||
mod arr;
|
||||
impl Solution {
|
||||
fn get_count(grid: &Vec<Vec<i32>>, x: usize, y: usize, i: usize) -> i32 {
|
||||
if i == 0 {
|
||||
return grid[y][x];
|
||||
}
|
||||
let mut cnt = 0;
|
||||
|
||||
for t in 0..i {
|
||||
cnt += grid[y + t][x + t];
|
||||
cnt += grid[y + i + t][x + i - t];
|
||||
cnt += grid[y + 2 * i - t][x - t];
|
||||
cnt += grid[y + i - t][x - i + t];
|
||||
}
|
||||
cnt
|
||||
}
|
||||
pub fn get_biggest_three(grid: Vec<Vec<i32>>) -> Vec<i32> {
|
||||
let (mut m1, mut m2, mut m3) = (0, 0, 0);
|
||||
|
||||
for i in 0..grid.len() {
|
||||
let path = i * 2 + 1;
|
||||
if path > grid.len() || path > grid[0].len() {
|
||||
break;
|
||||
}
|
||||
for j in i..grid[0].len() - i {
|
||||
for k in 0..grid.len() - 2 * i {
|
||||
let cnt = Solution::get_count(&grid, j, k, i);
|
||||
if cnt == m1 || cnt == m2 || cnt == m3 {
|
||||
continue;
|
||||
}
|
||||
if cnt > m1 {
|
||||
(m1, m2, m3) = (cnt, m1, m2);
|
||||
} else if cnt > m2 {
|
||||
(m2, m3) = (cnt, m2);
|
||||
} else if cnt > m3 {
|
||||
m3 = cnt;
|
||||
pub fn largest_submatrix(mut matrix: Vec<Vec<i32>>) -> i32 {
|
||||
for i in 1..matrix.len() {
|
||||
for j in 0..matrix[i].len() {
|
||||
if matrix[i][j] == 1 {
|
||||
matrix[i][j] = 1 + matrix[i - 1][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut ans = 0;
|
||||
let mut arr = Vec::with_capacity(matrix[0].len());
|
||||
for i in 0..matrix.len() {
|
||||
for j in 0..matrix[i].len() {
|
||||
arr.push(matrix[i][j]);
|
||||
}
|
||||
let mut ret = vec![m1];
|
||||
if m2 != 0 {
|
||||
ret.push(m2);
|
||||
if m3 != 0 {
|
||||
ret.push(m3);
|
||||
arr.sort_unstable_by(|a, b| b.cmp(a));
|
||||
for j in 0..matrix[i].len() {
|
||||
ans = max(ans, arr[j] * (j + 1) as i32);
|
||||
}
|
||||
arr.clear()
|
||||
}
|
||||
ret
|
||||
ans
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let result = Solution::get_biggest_three(arr::make_matrix(
|
||||
"[[6,7,7,7,7]]",
|
||||
let result = Solution::largest_submatrix(arr::make_matrix(
|
||||
"[[0,0,1],[1,1,1],[1,0,1]]",
|
||||
));
|
||||
println!("{:?}", result);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue