Skip to content

Commit 45f1041

Browse files
solves #2373: Largest Local Values in a Matrix in java
1 parent 15af33e commit 45f1041

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@
759759
| 2360 | [Longest Cycle in a Graph](https://leetcode.com/problems/longest-cycle-in-a-graph) | [![Python](assets/python.png)](python/longest_cycle_in_graph.py) | |
760760
| 2363 | [Merge Similar Items](https://leetcode.com/problems/merge-similar-items) | [![Java](assets/java.png)](src/MergeSimilarItems.java) | |
761761
| 2367 | [Number of Arithmetic Triplets](https://leetcode.com/problems/number-of-arithmetic-triplets) | [![Java](assets/java.png)](src/NumberOfArithmeticTriplets.java) | |
762-
| 2373 | [Largest Local Values in a Matrix](https://leetcode.com/problems/largest-local-values-in-a-matrix) | | |
762+
| 2373 | [Largest Local Values in a Matrix](https://leetcode.com/problems/largest-local-values-in-a-matrix) | [![Java](assets/java.png)](src/LargestLocalValuesInAMatrix.java) | |
763763
| 2379 | [Minimum Recolors to Get K Consecutive Black Blocks](https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks) | | |
764764
| 2383 | [Minimum Hours of Training to Win a Competition](https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition) | | |
765765
| 2389 | [Longest Subsequence With Limited Sum](https://leetcode.com/problems/longest-subsequence-with-limited-sum) | | |

src/LargestLocalValuesInAMatrix.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://leetcode.com/problems/largest-local-values-in-a-matrix
2+
// T: O(n ^ 2)
3+
// S: O(n ^ 2)
4+
5+
public class LargestLocalValuesInAMatrix {
6+
public int[][] largestLocal(int[][] grid) {
7+
final int n = grid.length;
8+
final int[][] result = new int[n - 2][n - 2];
9+
10+
for (int row = 1 ; row < n - 1 ; row++) {
11+
for (int column = 1 ; column < n - 1 ; column++) {
12+
result[row - 1][column - 1] = localMax(grid, row, column);
13+
}
14+
}
15+
16+
return result;
17+
}
18+
19+
private int localMax(int[][] grid, int row, int column) {
20+
int max = Integer.MIN_VALUE;
21+
for (int i = row - 1 ; i <= row + 1 ; i++) {
22+
for (int j = column - 1 ; j <= column + 1 ; j++) {
23+
max = Math.max(max, grid[i][j]);
24+
}
25+
}
26+
return max;
27+
}
28+
}

0 commit comments

Comments
 (0)