mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
java: add «2373. Largest Local Values in a Matrix»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
4a3c33cda8
commit
0f5f897414
1 changed files with 32 additions and 0 deletions
32
java/largest-local-values-in-a-matrix.java
Normal file
32
java/largest-local-values-in-a-matrix.java
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
class Solution {
|
||||||
|
private boolean isValid(int n, int y, int x) {
|
||||||
|
return y >= 0 && y < n && x >= 0 && x < n;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addLocal(int[][] grid, int[][] result, int y, int x) {
|
||||||
|
for (int d = 0; d < 9; ++d) {
|
||||||
|
int dy = (d / 3) - 1;
|
||||||
|
int dx = (d % 3) - 1;
|
||||||
|
|
||||||
|
if (!isValid(grid.length - 2, y - 1 + dy, x - 1 + dx)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
result[y - 1 + dy][x - 1 + dx] = Math.max(result[y - 1 + dy][x - 1 + dx], grid[y][x]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[][] largestLocal(int[][] grid) {
|
||||||
|
int N = grid.length;
|
||||||
|
int[][] result = new int[N - 2][N - 2];
|
||||||
|
|
||||||
|
for (int i = 0; i < N * N; ++i) {
|
||||||
|
int y = i / N;
|
||||||
|
int x = i % N;
|
||||||
|
|
||||||
|
addLocal(grid, result, y, x);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue