1878. 矩阵中最大的三个菱形和
This commit is contained in:
parent
d5eafc86f4
commit
143ff15f87
79
src/main.rs
79
src/main.rs
|
|
@ -1,61 +1,58 @@
|
|||
|
||||
struct Solution;
|
||||
mod arr;
|
||||
impl Solution {
|
||||
pub fn construct(s: &mut Vec<char>, n: usize) {
|
||||
for _ in s.len()..n {
|
||||
if s[s.len() - 1] == 'a' {
|
||||
s.push('b');
|
||||
} else {
|
||||
s.push('a');
|
||||
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_happy_string(n: i32, k: i32) -> String {
|
||||
let n = n as usize;
|
||||
let mut x = vec!['a'];
|
||||
Self::construct(&mut x,n);
|
||||
for _ in 1..k {
|
||||
let mut have_ans = false;
|
||||
for j in (0..n).rev() {
|
||||
if x[j] == 'a' {
|
||||
have_ans = true;
|
||||
if j == 0{
|
||||
x = vec!['b'];
|
||||
Self::construct(&mut x,n);
|
||||
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;
|
||||
}
|
||||
if x[j-1] == 'b'{
|
||||
x[j] = 'c';
|
||||
}else {
|
||||
x[j] = 'b';
|
||||
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;
|
||||
}
|
||||
x = x[0..j+1].to_vec();
|
||||
Self::construct(&mut x,n);
|
||||
break;
|
||||
}
|
||||
if x[j] == 'b' {
|
||||
if j == 0 || x[j-1] != 'c' {
|
||||
x[j] = 'c';
|
||||
x = x[0..j+1].to_vec();
|
||||
Self::construct(&mut x,n);
|
||||
have_ans = true;
|
||||
break;
|
||||
if cnt > m1 {
|
||||
(m1, m2, m3) = (cnt, m1, m2);
|
||||
} else if cnt > m2 {
|
||||
(m2, m3) = (cnt, m2);
|
||||
} else if cnt > m3 {
|
||||
m3 = cnt;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !have_ans {
|
||||
return String::new();
|
||||
}
|
||||
let mut ret = vec![m1];
|
||||
if m2 != 0 {
|
||||
ret.push(m2);
|
||||
if m3 != 0 {
|
||||
ret.push(m3);
|
||||
}
|
||||
}
|
||||
x.into_iter().collect()
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let result = Solution::get_happy_string(
|
||||
10,
|
||||
100,
|
||||
);
|
||||
let result = Solution::get_biggest_three(arr::make_matrix(
|
||||
"[[6,7,7,7,7]]",
|
||||
));
|
||||
println!("{:?}", result);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue