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