-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLargestLocalValuesInAMatrix.java
48 lines (42 loc) · 1.27 KB
/
LargestLocalValuesInAMatrix.java
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
/*https://leetcode.com/problems/largest-local-values-in-a-matrix/*/
class Solution {
public int[][] largestLocal(int[][] grid) {
int n = grid.length;
int [][] result = new int [n - 2][n - 2];
int max;
for (int i = 0; i < n - 2; ++i){
for (int j = 0; j < n - 2; ++j){
max = 1;
for (int a = 0; a < 3; ++a){
for (int b = 0; b < 3; ++b){
max = Math.max(max, grid[i + a][j + b]);
}
}
result[i][j] = max;
}
}
return result;
}
}
class Solution {
public int[][] largestLocal(int[][] grid) {
int i, j, k, n = grid.length;
int[][] ng = new int[n-2][n-2];
for (i = 1; i < n-1; ++i)
{
for (j = 1; j < n-1; ++j)
{
ng[i-1][j-1] = getMax(grid[i][j],grid[i-1][j],grid[i+1][j],grid[i][j-1],grid[i-1][j-1],grid[i+1][j-1],grid[i-1][j+1],grid[i+1][j+1],grid[i][j+1]);
}
}
return ng;
}
public int getMax(int... a)
{
int max = Integer.MIN_VALUE;
for (int i : a)
if (i > max)
max = i;
return max;
}
}