diff --git a/src/main.rs b/src/main.rs index 62cfd32..a1ccd04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,61 +1,58 @@ - struct Solution; mod arr; impl Solution { - pub fn construct(s: &mut Vec, 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>, 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); - break; - } - if x[j-1] == 'b'{ - x[j] = 'c'; - }else { - x[j] = 'b'; - } - 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; - } - } + pub fn get_biggest_three(grid: Vec>) -> Vec { + 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 !have_ans { - return String::new(); + 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; + } + } } } - x.into_iter().collect() + let mut ret = vec![m1]; + if m2 != 0 { + ret.push(m2); + if m3 != 0 { + ret.push(m3); + } + } + 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); }