Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cd1c790

Browse files
solves #2500: Delete Greatest Value in Each Row in java
1 parent dc9d309 commit cd1c790

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@
787787
| 2490 | [Circular Sentence](https://leetcode.com/problems/circular-sentence) | [![Java](assets/java.png)](src/CircularSentence.java) | |
788788
| 2492 | [Minimum Score of a Path Between Two Cities](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities) | [![Python](assets/python.png)](python/minimum_score_of_a_path_between_two_cities.py) | |
789789
| 2496 | [Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array) | [![Java](assets/java.png)](src/MaximumValueOfAStringInAnArray.java) | |
790-
| 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row) | | |
790+
| 2500 | [Delete Greatest Value in Each Row](https://leetcode.com/problems/delete-greatest-value-in-each-row) | [![Java](assets/java.png)](src/DeleteGreatestValueInEachRow.java) | |
791791
| 2506 | [Count Pairs Of Similar Strings](https://leetcode.com/problems/count-pairs-of-similar-strings) | | |
792792
| 2511 | [Maximum Enemy Forts That Can Be Captured](https://leetcode.com/problems/maximum-enemy-forts-that-can-be-captured) | | |
793793
| 2515 | [Shortest Distance to Target String in a Circular Array](https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array) | | |

src/DeleteGreatestValueInEachRow.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// https://leetcode.com/problems/delete-greatest-value-in-each-row
2+
// T: O(mn * log(N))
3+
// S: O(log(n))
4+
5+
import java.util.Arrays;
6+
7+
public class DeleteGreatestValueInEachRow {
8+
public int deleteGreatestValue(int[][] grid) {
9+
final int columns = grid[0].length;
10+
int maxDeletions = 0;
11+
12+
sortAllRows(grid);
13+
14+
for (int column = 0 ; column < columns ; column++) {
15+
maxDeletions += columnMax(grid, column);
16+
}
17+
18+
return maxDeletions;
19+
}
20+
21+
private int columnMax(int[][] grid, int column) {
22+
int max = Integer.MIN_VALUE;
23+
for (int[] row : grid) {
24+
max = Math.max(max, row[column]);
25+
}
26+
return max;
27+
}
28+
29+
private void sortAllRows(int[][] grid) {
30+
for (int[] row : grid) {
31+
Arrays.sort(row);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)